adventofcode/2021/5/2.py

51 lines
1.4 KiB
Python
Raw Normal View History

2022-12-01 21:13:55 +01:00
#!/usr/bin/env python3
import re
def main():
board_size = 1000
content = open('input', 'r').readlines()
content = list(map(str.strip, content))
board = [[0 for _ in range(board_size)] for _ in range(board_size)]
for line in content:
m = re.match('^(\d+),(\d+) -> (\d+),(\d+)', line)
x1, y1, x2, y2 = tuple(map(int, m.groups(1)))
if x1 == x2: # move in y
if y2 < y1:
y1, y2 = y2, y1
assert(y1 < y2)
print(f'marking from {x1},{y1} to {x2},{y2}')
for y in range(y1, y2+1):
board[y][x1] += 1
elif y1 == y2: # move in x
if x2 < x1:
x1, x2 = x2, x1
print(f'marking from {x1},{y1} to {x2},{y2}')
assert(x1 < x2)
for x in range(x1, x2+1):
board[y1][x] += 1
else: # move diagonal
#
offset_x = x2 - x1
offset_y = y2 - y1
if offset_x > 0: x_step = 1
else: x_step = -1
if offset_y > 0: y_step = 1
else: y_step = -1
for offset in range(abs(x2-x1)+1):
board[y1+(offset*y_step)][x1+(offset*x_step)] += 1
print(f'diagonal: going from {x1},{y1} to {x2},{y2} -> ')
hotspots = 0
for line in board:
for cell in line:
if cell > 1:
hotspots += 1
print(hotspots)
if __name__ == "__main__":
main()