Skip to content

Commit e73b591

Browse files
committed
Added day 2015-18
1 parent 905841b commit e73b591

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

2015/18-Like a GIF For Your Yard.py

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """.#.#.#
8+
...##.
9+
#....#
10+
..#...
11+
#.#..#
12+
####..""",
13+
"iterations": 5, # 4 for Part 1
14+
"expected": ['4', '17'],
15+
}
16+
17+
test += 1
18+
test_data[test] = {"input": """""",
19+
"expected": ['Unknown', 'Unknown'],
20+
}
21+
22+
test = 'real'
23+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
24+
test_data[test] = {"input": open(input_file, "r+").read(),
25+
"iterations": 100,
26+
"expected": ['821', '886'],
27+
}
28+
# 865 too low
29+
# -------------------------------- Control program execution -------------------------------- #
30+
31+
case_to_test = 'real'
32+
part_to_test = 2
33+
verbose_level = 1
34+
35+
# -------------------------------- Initialize some variables -------------------------------- #
36+
37+
puzzle_input = test_data[case_to_test]['input']
38+
puzzle_iterations = test_data[case_to_test]['iterations']
39+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
40+
puzzle_actual_result = 'Unknown'
41+
42+
43+
# -------------------------------- Actual code execution -------------------------------- #
44+
45+
lights = puzzle_input.split('\n')
46+
lights_width = len(lights[0])
47+
lights_height = len(lights)
48+
49+
# For Part 2 : updating the corners of initial situation for the calculation
50+
if part_to_test == 2:
51+
new_lights = ''
52+
for y in range (lights_height):
53+
for x in range (lights_width):
54+
if (y, x) in ((0, 0), (0, lights_width-1), (lights_height-1, 0), (lights_height-1, lights_width-1)):
55+
new_lights += '#'
56+
else:
57+
new_lights += lights[y][x]
58+
new_lights += '\n'
59+
60+
lights = new_lights.split('\n')
61+
62+
63+
for n in range(0, puzzle_iterations):
64+
new_lights = ''
65+
for y in range (lights_height):
66+
for x in range (lights_width):
67+
# For Part 2 : updating the corners for the calculation
68+
if part_to_test == 2 and (y, x) in ((0, 0), (0, lights_width-1), (lights_height-1, 0), (lights_height-1, lights_width-1)):
69+
new_lights += '#'
70+
continue
71+
neighbors_x = range(max(x-1,0), min(x+2, lights_width))
72+
neighbors_y = range(max(y-1,0), min(y+2, lights_height))
73+
74+
neighbors = [1 if lights[i][j] == '#' and (i, j) != (y, x) else 0 for i in neighbors_y for j in neighbors_x]
75+
count_on = sum(neighbors)
76+
if lights[y][x] == '#' and count_on in (2, 3):
77+
new_lights += '#'
78+
elif lights[y][x] == '#':
79+
new_lights += '.'
80+
elif lights[y][x] == '.' and count_on == 3:
81+
new_lights += '#'
82+
else:
83+
new_lights += '.'
84+
new_lights += '\n'
85+
lights = new_lights.split('\n')
86+
new_lights = ''
87+
puzzle_actual_result = sum([1 if lights[y][x] == '#' else 0 for x in range(lights_width) for y in range(lights_height)])
88+
89+
90+
91+
92+
# -------------------------------- Outputs / results -------------------------------- #
93+
94+
if verbose_level >= 3:
95+
print ('Input : ' + puzzle_input)
96+
print ('Expected result : ' + str(puzzle_expected_result))
97+
print ('Actual result : ' + str(puzzle_actual_result))
98+
99+
100+
101+

0 commit comments

Comments
 (0)