-
Notifications
You must be signed in to change notification settings - Fork 0
/
day-11.py
119 lines (100 loc) · 3.11 KB
/
day-11.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import re
import sys
# Get the day from the filename e.g. day-1.py
DAY = re.match(r"day\-(\d+)\.py", sys.argv[0]).groups()[0]
def part_1(path):
"""Part 1/Star 1"""
# Open the file
with open(f"{path}/day-{DAY}.txt", "r") as f:
values = [[int(i) for i in line.strip()] for line in f.readlines()]
# Complete the task
no_rows = len(values)
no_cols = len(values[0])
threshold = 10
def increase_neighbours(x, y):
if (
x < 0
or x >= no_cols
or y < 0
or y >= no_rows
or values[y][x] > threshold
):
return
else:
values[y][x] += 1
if values[y][x] == threshold:
for x_change in [-1, 0, 1]:
for y_change in [-1, 0, 1]:
if not (x_change == 0 and y_change == 0):
increase_neighbours(x + x_change, y + y_change)
answer = 0
for _ in range(100):
for x in range(no_cols):
for y in range(no_rows):
increase_neighbours(x, y)
answer += len([x for y in values for x in y if x >= threshold])
values = [[i if i < threshold else 0 for i in line] for line in values]
# Print out the response
print(f"Task 1 Answer: {answer}")
def part_2(path):
"""Part 2/Star 2"""
with open(f"{path}/day-{DAY}.txt", "r") as f:
values = [[int(i) for i in line.strip()] for line in f.readlines()]
# Complete the task
no_rows = len(values)
no_cols = len(values[0])
threshold = 10
def increase_neighbours(x, y):
if (
x < 0
or x >= no_cols
or y < 0
or y >= no_rows
or values[y][x] > threshold
):
return
else:
values[y][x] += 1
if values[y][x] == threshold:
for x_change in [-1, 0, 1]:
for y_change in [-1, 0, 1]:
if not (x_change == 0 and y_change == 0):
increase_neighbours(x + x_change, y + y_change)
answer = 0
while True:
answer += 1
for x in range(no_cols):
for y in range(no_rows):
increase_neighbours(x, y)
if (
len([x for y in values for x in y if x >= threshold])
== no_rows * no_cols
):
break
values = [[i if i < threshold else 0 for i in line] for line in values]
# Print out the response
print(f"Task 2 Answer: {answer}")
if __name__ == "__main__":
"""
Run using e.g.
`python day-1.py -test`
`python day-1.py`
`python day-1.py -test -2`
`python day-1.py -2`
`python day-1.py -test -both`
`python day-1.py -both`
"""
# Identify the folder that the input is in
test = "-test" in sys.argv
if test:
path = "input-tests"
else:
path = "inputs"
# Identify which one to run - 1 is default
if "-2" in sys.argv:
part_2(path)
elif "-both" in sys.argv:
part_1(path)
part_2(path)
else:
part_1(path)