-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
67 lines (51 loc) · 2.01 KB
/
day10.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
import time
def parse_input(filename: str) -> list[str]:
with open(filename) as f:
return [line.strip() for line in f.readlines()]
def dfs(start, strings, visited):
stack = []
def push_next_to_stack(start, strings, stack, visited):
dirs = [(1, 0), (-1, 0), (0, -1), (0, 1)]
for dir in dirs:
cur_dir = (start[0] + dir[0], start[1] + dir[1])
if cur_dir[0] < 0 or cur_dir[0] >= len(strings) or cur_dir[1] < 0 or cur_dir[1] >= len(strings[0]):
continue
if visited is None or (visited is not None and cur_dir not in visited):
if strings[cur_dir[0]][cur_dir[1]] == str(int(strings[start[0]][start[1]]) + 1):
stack.append(cur_dir)
continue
push_next_to_stack(start, strings, stack, visited)
count = 0
while len(stack) > 0:
next_elem = stack.pop()
if strings[next_elem[0]][next_elem[1]] == '9':
count += 1
if visited is not None:
visited.add(next_elem)
push_next_to_stack(next_elem, strings, stack, visited)
return count
def part_one(strings: list[str]) -> list[tuple[int, int]]:
zeroes = [(i, j) for i, string in enumerate(strings) for j, letter in enumerate(string) if letter == '0']
count = 0
visited = set()
for zero in zeroes:
count += dfs(zero, strings, visited)
visited = set()
print(count)
return zeroes
def part_two(strings: list[str], zeroes: list[tuple[int, int]]) -> None:
count = 0
for zero in zeroes:
count += dfs(zero, strings, None)
print(count)
def main(input_filename: str):
inp = parse_input(input_filename)
start_part_one = time.time()
zeroes = part_one(inp)
start_part_two = time.time()
part_two(inp, zeroes)
end_time = time.time()
print(f"Part one took {start_part_two - start_part_one:.2f} seconds")
print(f"Part two took {end_time - start_part_two:.2f} seconds")
if __name__ == "__main__":
main("input.txt")