-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08.py
50 lines (39 loc) · 1.52 KB
/
08.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
from itertools import combinations
from string import ascii_letters
import sys
import os
import numpy as np
def parse_input(argv):
with open(argv[1] if len(sys.argv) >= 2 else "inputs/test") as file:
grid = np.array(list(map(list, file.read().split())))
return grid
def part_one(grid):
antinodes = {(i, j): 0 for i, row in enumerate(grid) for j, c in enumerate(row)}
for c in list(ascii_letters) + [str(i) for i in range(10)]:
nodes = np.argwhere(grid == c)
if not len(nodes):
continue
for n1, n2 in combinations(nodes, r=2):
if (loc := tuple(2 * n1 - n2)) in antinodes:
antinodes[loc] = 1
if (loc := tuple(2 * n2 - n1)) in antinodes:
antinodes[loc] = 1
return sum(antinodes.values())
def part_two(grid):
antinodes = {(i, j): 0 for i, row in enumerate(grid) for j, c in enumerate(row)}
for c in list(ascii_letters) + [str(i) for i in range(10)]:
nodes = np.argwhere(grid == c)
if not len(nodes):
continue
for n1, n2 in combinations(nodes, r=2):
for sgn in [-1, 1]:
d = sgn * (n1 - n2)
loc = (1 + sgn) / 2 * n1 - (-1 + sgn) / 2 * n2
while tuple(loc) in antinodes:
antinodes[tuple(loc)] = 1
loc += d
return sum(antinodes.values())
if __name__ == "__main__":
inp = parse_input(sys.argv)
print(f"Part 1: {part_one(np.copy(inp))}")
print(f"Part 2: {part_two(inp)}")