Skip to content

Commit 29f2dd6

Browse files
committed
Added day 2018-12
1 parent bbedcfd commit 29f2dd6

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, numpy as np
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": '''initial state: #..#.#..##......###...###
8+
9+
...## => #
10+
..#.. => #
11+
.#... => #
12+
.#.#. => #
13+
.#.## => #
14+
.##.. => #
15+
.#### => #
16+
#.#.# => #
17+
#.### => #
18+
##.#. => #
19+
##.## => #
20+
###.. => #
21+
###.# => #
22+
####. => #''',
23+
"expected": ['325', 'Unknown'],
24+
}
25+
26+
test = 'real'
27+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
28+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
29+
"expected": ['3890', '23743'],
30+
}
31+
32+
# -------------------------------- Control program execution -------------------------------- #
33+
34+
case_to_test = 'real'
35+
part_to_test = 2
36+
37+
# -------------------------------- Initialize some variables -------------------------------- #
38+
39+
puzzle_input = test_data[case_to_test]['input']
40+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
41+
puzzle_actual_result = 'Unknown'
42+
43+
44+
# -------------------------------- Actual code execution -------------------------------- #
45+
46+
# Note: numpy was used to practice. Clearly not the best choice here.
47+
48+
if part_to_test == 1:
49+
generations = 20
50+
else:
51+
generations = 50000000000
52+
53+
54+
initial_state = puzzle_input.splitlines()[0][15:]
55+
56+
pots = np.full((len(initial_state) + 10**6), '.')
57+
pots[5*10**5:5*10**5+len(initial_state)] = np.fromiter(initial_state, dtype='S1', count=len(initial_state))
58+
59+
rules = {}
60+
for string in puzzle_input.splitlines()[2:]:
61+
source, target = string.split(' => ')
62+
rules[source] = target
63+
64+
prev_sum = sum(np.where(pots == '#')[0]) - 5*10**5 * len(np.where(pots == '#')[0])
65+
for i in range (1, generations):
66+
67+
if case_to_test == 1:
68+
for i in range (2, len(pots)-3):
69+
if ''.join(pots[i-2:i+3]) not in rules:
70+
rules[''.join(pots[i-2:i+3])] = '.'
71+
72+
min_x, max_x = min(np.where(pots == '#')[0]), max(np.where(pots == '#')[0])
73+
74+
new_pots = np.full((len(initial_state) + 10**6), '.')
75+
new_pots[min_x-2:max_x+2] = [rules[''.join(pots[i-2:i+3])] for i in range(min_x-2, max_x+2)]
76+
pots = new_pots.copy()
77+
78+
sum_pots = sum(np.where(new_pots == '#')[0]) - 5*10**5 * len(np.where(new_pots == '#')[0])
79+
80+
print (i, sum_pots, sum_pots - prev_sum)
81+
prev_sum = sum_pots
82+
83+
if i == 200:
84+
puzzle_actual_result = sum_pots + 96 * (generations-200)
85+
break
86+
87+
if part_to_test == 1:
88+
puzzle_actual_result = sum_pots
89+
90+
91+
# -------------------------------- Outputs / results -------------------------------- #
92+
93+
print ('Expected result : ' + str(puzzle_expected_result))
94+
print ('Actual result : ' + str(puzzle_actual_result))
95+
96+
97+
98+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy