mirror of
https://github.com/Xevion/advent-of-code.git
synced 2025-12-05 23:14:16 -06:00
day 3 part 2
This commit is contained in:
@@ -4,12 +4,12 @@ import os, sys, pyperclip, re
|
||||
def parse(string):
|
||||
match = re.search(pattern, string)
|
||||
# return int(match[1]), int(match[2]), int(match[3]), int(match[4])
|
||||
return list(map(int, [match[i] for i in range(1, 5)]))
|
||||
return list(map(int, [match[i] for i in range(1, 6)]))
|
||||
|
||||
# I only later figured out that the dimensions were simply 1000 x 1000
|
||||
# but this now allows it to be of any dimension
|
||||
def dimensions(data):
|
||||
def bottomright(x, y, width, height):
|
||||
def bottomright(id, x, y, width, height):
|
||||
return (x + width, height + y)
|
||||
bottomrights = list(map(lambda dat : bottomright(*dat), data))
|
||||
matrix_x = max(bottomrights, key=lambda item : item[0])[0]
|
||||
@@ -18,18 +18,35 @@ def dimensions(data):
|
||||
|
||||
path = os.path.join(sys.path[0], '..', 'input')
|
||||
data = open(path, 'r').read().split('\n')
|
||||
pattern = r'#\d+ @ (\d+),(\d+): (\d+)x(\d+)'
|
||||
pattern = r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)'
|
||||
data = list(map(parse, data))
|
||||
WIDTH, HEIGHT = dimensions(data)
|
||||
matrix = [[0 for y in range(WIDTH)] for x in range(HEIGHT)]
|
||||
matrix = [[[set(), 0] for y in range(WIDTH)] for x in range(HEIGHT)]
|
||||
|
||||
for offset_x, offset_y, width, height in data:
|
||||
for id, offset_x, offset_y, width, height in data:
|
||||
for x in range(width):
|
||||
for y in range(height):
|
||||
# matrix[offset_x + x][offset_y + y] = min(2, 1 + matrix[offset_x + x][offset_y + y])
|
||||
matrix[offset_x + x][offset_y + y] += 1
|
||||
|
||||
# count = sum(map(lambda x : x.count(2), matrix))
|
||||
count = sum(map(lambda item : len(list(filter(lambda i : i >= 2, item))), matrix))
|
||||
newX, newY = offset_x + x, offset_y + y
|
||||
matrix[newX][newY][1] += 1
|
||||
matrix[newX][newY][0].add(id)
|
||||
|
||||
# Checks whether a specific span has any overlaps
|
||||
def check(offset_x, offset_y, width, height):
|
||||
for x in range(width):
|
||||
for y in range(height):
|
||||
newX, newY = offset_x + x, offset_y + y
|
||||
# Quit as soon as the span has been invalidated
|
||||
if len(matrix[newX][newY][0]) > 1:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Count all the points in the matrix
|
||||
count = sum(map(lambda item : len(list(filter(lambda i : i[1] >= 2, item))), matrix))
|
||||
print(count)
|
||||
pyperclip.copy(count)
|
||||
|
||||
# Checks every id
|
||||
for dat in data:
|
||||
if check(*dat[1:]):
|
||||
print(dat[0])
|
||||
break
|
||||
Reference in New Issue
Block a user