Skip to content

Commit 89a7947

Browse files
committed
Cleanup of prints & useless racetrack module
1 parent 5c462d1 commit 89a7947

12 files changed

Lines changed: 189 additions & 392 deletions

2018/11-Chronal Charge.py

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,100 @@
44
test_data = {}
55

66
test = 1
7-
test_data[test] = {"input": 18,
8-
"expected": ['Unknown', 'Unknown'],
9-
}
10-
11-
test = 'real'
12-
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
13-
test_data[test] = {"input": 7165,
14-
"expected": ['(235, 20) with 31', '(237, 223, 14) with 83'],
15-
}
7+
test_data[test] = {
8+
"input": 18,
9+
"expected": ["Unknown", "Unknown"],
10+
}
11+
12+
test = "real"
13+
input_file = os.path.join(
14+
os.path.dirname(__file__),
15+
"Inputs",
16+
os.path.basename(__file__).replace(".py", ".txt"),
17+
)
18+
test_data[test] = {
19+
"input": 7165,
20+
"expected": ["(235, 20) with 31", "(237, 223, 14) with 83"],
21+
}
1622

1723
# -------------------------------- Control program execution -------------------------------- #
1824

19-
case_to_test = 'real'
25+
case_to_test = "real"
2026
part_to_test = 2
2127

2228
# -------------------------------- Initialize some variables -------------------------------- #
2329

24-
puzzle_input = test_data[case_to_test]['input']
25-
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
26-
puzzle_actual_result = 'Unknown'
30+
puzzle_input = test_data[case_to_test]["input"]
31+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
32+
puzzle_actual_result = "Unknown"
2733

2834

2935
# -------------------------------- Actual code execution -------------------------------- #
3036

3137

3238
if part_to_test == 1:
33-
grid_power = {(x, y): int(((((10+x)*y + puzzle_input) * (10+x)) // 100) % 10)-5 for x in range (1, 301) for y in range (1, 301)}
34-
35-
sum_power = {(x, y): sum(grid_power[x1, y1] for x1 in range (x, x+3) for y1 in range (y, y+3)) for x in range (1, 299) for y in range (1, 299)}
39+
grid_power = {
40+
(x, y): int(((((10 + x) * y + puzzle_input) * (10 + x)) // 100) % 10) - 5
41+
for x in range(1, 301)
42+
for y in range(1, 301)
43+
}
44+
45+
sum_power = {
46+
(x, y): sum(
47+
grid_power[x1, y1] for x1 in range(x, x + 3) for y1 in range(y, y + 3)
48+
)
49+
for x in range(1, 299)
50+
for y in range(1, 299)
51+
}
3652

3753
max_power = max(sum_power.values())
3854

39-
puzzle_actual_result = list(coord for coord in sum_power if sum_power[coord] == max_power)
55+
puzzle_actual_result = list(
56+
coord for coord in sum_power if sum_power[coord] == max_power
57+
)
4058

4159

4260
else:
43-
grid_power = {(x, y): int(((((10+x)*y + puzzle_input) * (10+x)) // 100) % 10)-5 for x in range (1, 301) for y in range (1, 301)}
61+
grid_power = {
62+
(x, y): int(((((10 + x) * y + puzzle_input) * (10 + x)) // 100) % 10) - 5
63+
for x in range(1, 301)
64+
for y in range(1, 301)
65+
}
4466

4567
max_power = 31
4668
sum_power = grid_power.copy()
47-
for size in range (2, 300):
48-
sum_power = {(x, y, size): sum(grid_power[x1, y1]
49-
for x1 in range (x, x+size)
50-
for y1 in range (y, y+size))
51-
for x in range (1, 301-size+1)
52-
for y in range (1, 301-size+1)}
69+
decreasing = False
70+
last_power = 0
71+
for size in range(2, 300):
72+
sum_power = {
73+
(x, y, size): sum(
74+
grid_power[x1, y1]
75+
for x1 in range(x, x + size)
76+
for y1 in range(y, y + size)
77+
)
78+
for x in range(1, 301 - size + 1)
79+
for y in range(1, 301 - size + 1)
80+
}
5381

5482
new_max = max(sum_power.values())
5583
if new_max > max_power:
84+
decreasing = False
5685
max_power = new_max
57-
puzzle_actual_result = list(coord + (size,) for coord in sum_power if sum_power[coord] == max_power)
86+
puzzle_actual_result = list(
87+
coord for coord in sum_power if sum_power[coord] == max_power
88+
)
5889

5990
# Basically, let it run until it decreases multiple times
60-
print (size, new_max, list(coord for coord in sum_power if sum_power[coord] == new_max))
91+
# print (size, new_max, list(coord for coord in sum_power if sum_power[coord] == new_max))
6192

93+
if not decreasing and new_max < last_power:
94+
decreasing = True
95+
elif decreasing and new_max < last_power:
96+
break
97+
last_power = new_max
6298

6399

64100
# -------------------------------- Outputs / results -------------------------------- #
65101

66-
print ('Expected result : ' + str(puzzle_expected_result))
67-
print ('Actual result : ' + str(puzzle_actual_result))
68-
69-
70-
71-
102+
print("Expected result : " + str(puzzle_expected_result))
103+
print("Actual result : " + str(puzzle_actual_result))

2018/12-Subterranean Sustainability.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
test_data = {}
55

66
test = 1
7-
test_data[test] = {"input": '''initial state: #..#.#..##......###...###
7+
test_data[test] = {
8+
"input": """initial state: #..#.#..##......###...###
89
910
...## => #
1011
..#.. => #
@@ -19,26 +20,31 @@
1920
##.## => #
2021
###.. => #
2122
###.# => #
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-
}
23+
####. => #""",
24+
"expected": ["325", "Unknown"],
25+
}
26+
27+
test = "real"
28+
input_file = os.path.join(
29+
os.path.dirname(__file__),
30+
"Inputs",
31+
os.path.basename(__file__).replace(".py", ".txt"),
32+
)
33+
test_data[test] = {
34+
"input": open(input_file, "r+").read().strip(),
35+
"expected": ["3890", "23743"],
36+
}
3137

3238
# -------------------------------- Control program execution -------------------------------- #
3339

34-
case_to_test = 'real'
40+
case_to_test = "real"
3541
part_to_test = 2
3642

3743
# -------------------------------- Initialize some variables -------------------------------- #
3844

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'
45+
puzzle_input = test_data[case_to_test]["input"]
46+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
47+
puzzle_actual_result = "Unknown"
4248

4349

4450
# -------------------------------- Actual code execution -------------------------------- #
@@ -53,35 +59,41 @@
5359

5460
initial_state = puzzle_input.splitlines()[0][15:]
5561

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))
62+
pots = np.full((len(initial_state) + 10 ** 6), ".")
63+
pots[5 * 10 ** 5 : 5 * 10 ** 5 + len(initial_state)] = np.fromiter(
64+
initial_state, dtype="S1", count=len(initial_state)
65+
)
5866

5967
rules = {}
6068
for string in puzzle_input.splitlines()[2:]:
61-
source, target = string.split(' => ')
69+
source, target = string.split(" => ")
6270
rules[source] = target
6371

64-
prev_sum = sum(np.where(pots == '#')[0]) - 5*10**5 * len(np.where(pots == '#')[0])
65-
for i in range (1, generations):
72+
prev_sum = sum(np.where(pots == "#")[0]) - 5 * 10 ** 5 * len(np.where(pots == "#")[0])
73+
for i in range(1, generations):
6674

6775
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])] = '.'
76+
for i in range(2, len(pots) - 3):
77+
if "".join(pots[i - 2 : i + 3]) not in rules:
78+
rules["".join(pots[i - 2 : i + 3])] = "."
7179

72-
min_x, max_x = min(np.where(pots == '#')[0]), max(np.where(pots == '#')[0])
80+
min_x, max_x = min(np.where(pots == "#")[0]), max(np.where(pots == "#")[0])
7381

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)]
82+
new_pots = np.full((len(initial_state) + 10 ** 6), ".")
83+
new_pots[min_x - 2 : max_x + 2] = [
84+
rules["".join(pots[i - 2 : i + 3])] for i in range(min_x - 2, max_x + 2)
85+
]
7686
pots = new_pots.copy()
7787

78-
sum_pots = sum(np.where(new_pots == '#')[0]) - 5*10**5 * len(np.where(new_pots == '#')[0])
88+
sum_pots = sum(np.where(new_pots == "#")[0]) - 5 * 10 ** 5 * len(
89+
np.where(new_pots == "#")[0]
90+
)
7991

80-
print (i, sum_pots, sum_pots - prev_sum)
92+
# print (i, sum_pots, sum_pots - prev_sum)
8193
prev_sum = sum_pots
8294

8395
if i == 200:
84-
puzzle_actual_result = sum_pots + 96 * (generations-200)
96+
puzzle_actual_result = sum_pots + 96 * (generations - 200)
8597
break
8698

8799
if part_to_test == 1:
@@ -90,9 +102,5 @@
90102

91103
# -------------------------------- Outputs / results -------------------------------- #
92104

93-
print ('Expected result : ' + str(puzzle_expected_result))
94-
print ('Actual result : ' + str(puzzle_actual_result))
95-
96-
97-
98-
105+
print("Expected result : " + str(puzzle_expected_result))
106+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)