adventofcode/2021/5/1.py

40 lines
1.1 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 x2 < x1:
x1, x2 = x2, x1
if y2 < y1:
y1, y2 = y2, y1
if x1 == x2: # move in y
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
print(f'marking from {x1},{y1} to {x2},{y2}')
assert(x1 < x2)
for x in range(x1, x2+1):
board[y1][x] += 1
else:
print(f'skipped: 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()