-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay05.py
60 lines (42 loc) · 1.05 KB
/
Day05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from sys import argv, stdin
import re
NUM_RE = re.compile(r"(\d+),(\d+) -> (\d+),(\d+)")
nums = [
tuple(int(g) for g in m.groups())
for l in stdin.readlines()
if (m := NUM_RE.match(l))
]
BOARD_SIZE = 1000
board = []
for i in range(BOARD_SIZE):
board.append([0] * BOARD_SIZE)
def hv_lines(l):
x1, y1, x2, y2 = l
return x1 == x2 or y1 == y2
def sign(n):
if n > 0:
return 1
elif n < 0:
return -1
else:
return 0
def fill_board(line_criteria):
for x1, y1, x2, y2 in filter(line_criteria, nums):
if x1 == x2 and y1 == y2:
board[y1][x1] += 1
else:
stepx = sign(x2 - x1)
stepy = sign(y2 - y1)
x = x1
y = y1
while not (x == x2 and y == y2):
board[y][x] += 1
x += stepx
y += stepy
board[y][x] += 1
if argv[1] == "1":
fill_board(hv_lines)
else:
# Part two, use all lines
fill_board(lambda _: True)
print(sum(x > 1 for r in board for x in r))