-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday14.py
executable file
·93 lines (76 loc) · 2.49 KB
/
day14.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
from aoc.day10 import knot_hash
def hexify(string):
return bin(int(string, 16))[2:]
def part1(magic):
return magic_to_count(magic)
def part2(magic):
grid = magic_to_grid(magic)
rf = RegionFinder(grid)
count = rf.find_regions()
return count
class RegionFinder:
def __init__(self, grid):
self.grid = grid
def gen_neighbors(self, coord):
""" Given a tuple coordinate, return a generator iterating over
its 4 direct neighbors """
(x, y) = coord
yield (x + 1, y)
yield (x - 1, y)
yield (x, y + 1)
yield (x, y - 1)
def find_regions(self):
count = 1
# Not that robust: Assuming grid has (x, y) keys with range 0-127
for y in range(128):
for x in range(128):
val = self.grid[(x, y)]
if val == "x":
self.dfs((x, y), count)
count += 1
return count - 1
def dfs(self, coord, region_num):
self.grid[coord] = region_num
for n_coord in self.gen_neighbors(coord):
if n_coord not in self.grid:
continue
if self.grid[n_coord] == "x":
self.dfs(n_coord, region_num)
def magic_to_grid(magic):
""" Converts a magic number to a 128x128 grid as
defined by the problem.
The grid is represented as a map with (x, y) keys.
Values are either 0 if blank, or "x" if filled in, but unassigned
to a region. Values are this weird mix of types so we can use
1-99+ as region identifiers. """
grid = {}
for y in range(128):
key = magic + "-" + str(y)
list_of_strings_hash = [
hexify(letter).zfill(4) for letter in list(knot_hash(key))
]
x = 0
for string in list_of_strings_hash:
for char in string:
location = (x, y)
grid[location] = 0 if char == "0" else "x"
x += 1
return grid
def magic_to_count(magic):
count = 0
for i in range(128):
key = magic + "-" + str(i)
list_of_strings_hash = [
hexify(letter).zfill(4) for letter in list(knot_hash(key))
]
for string in list_of_strings_hash:
count += sum(1 for letter in list(string) if letter == "1")
return count
if __name__ == "__main__":
magic = "flqrgnkx" # Example
magic = "hfdlxzhv" # Mine
print("Part1: ")
print(part1(magic))
print("Part2: ")
print(part2(magic))