-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday14.py
More file actions
63 lines (55 loc) · 1.75 KB
/
Copy pathday14.py
File metadata and controls
63 lines (55 loc) · 1.75 KB
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
import time
from day10 import part2 as day10_part2
def part1(parsed):
result = 0
for row in range(128):
text = f"{parsed}-{row}"
hash = day10_part2(text)
b = bytes.fromhex(hash)
result += sum([byte.bit_count() for byte in b])
return result
def part2(parsed):
grid = []
for row in range(128):
text = f"{parsed}-{row}"
hash = day10_part2(text)
b = bytes.fromhex(hash)
grid.append("".join(format(byte, '08b') for byte in b))
coordinates = [(x, y) for x in range(128) for y in range(128) if grid[y][x] == "1"]
regions = 0
while len(coordinates) > 0:
regions += 1
visited = set()
frontier = [coordinates.pop(0)]
while len(frontier) > 0:
node = frontier.pop(0)
if node in visited:
continue
visited.add(node)
neighbours = [
(node[0] + x, node[1] + y)
for (x, y) in [(1, 0), (-1, 0), (0, 1), (0, -1)]
]
for neighbour in neighbours:
if neighbour in coordinates:
coordinates.remove(neighbour)
frontier.append(neighbour)
return regions
def test_part1():
test_result = part1("flqrgnkx")
assert test_result == 8108
def test_part2():
assert part2("flqrgnkx") == 1242
filename = "day0.txt"
if __name__ == '__main__':
parsed = "ffayrhll"
test_part1()
part1_start = time.time()
print("Part 1:", part1(parsed))
part1_end = time.time()
print(f"Part 1 took {part1_end - part1_start} seconds")
test_part2()
part2_start = time.time()
print("Part 2:", part2(parsed))
part2_end = time.time()
print(f"Part 2 took {part2_end - part2_start} seconds")