Skip to content

Commit 6fa48bf

Browse files
committed
Added days 2018-05, 2018-06, 2018-07 and update on pathfinding lib
1 parent 6d33d35 commit 6fa48bf

File tree

4 files changed

+814
-0
lines changed

4 files changed

+814
-0
lines changed

2018/05-Alchemical Reduction.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """dabAcCaCBAcCcaDA""",
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": open(input_file, "r+").read().strip(),
14+
"expected": ['9390', '5898'],
15+
}
16+
17+
# -------------------------------- Control program execution -------------------------------- #
18+
19+
case_to_test = 'real'
20+
part_to_test = 2
21+
verbose_level = 1
22+
23+
# -------------------------------- Initialize some variables -------------------------------- #
24+
25+
puzzle_input = test_data[case_to_test]['input']
26+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
27+
puzzle_actual_result = 'Unknown'
28+
29+
30+
# -------------------------------- Actual code execution -------------------------------- #
31+
32+
if part_to_test == 1:
33+
string = puzzle_input
34+
prev_len = 0
35+
while prev_len != len(string):
36+
prev_len = len(string)
37+
for letter in 'azertyuiopmlkjhgfdsqwxcvbn':
38+
string = string.replace(letter + letter.upper(), '')
39+
string = string.replace(letter.upper() + letter, '')
40+
41+
puzzle_actual_result = len(string)
42+
43+
44+
else:
45+
shortest_len = 10**6
46+
for letter in 'azertyuiopmlkjhgfdsqwxcvbn':
47+
48+
string = puzzle_input.replace(letter, '').replace(letter.upper(), '')
49+
prev_len = 0
50+
while prev_len != len(string):
51+
prev_len = len(string)
52+
for letter in 'azertyuiopmlkjhgfdsqwxcvbn':
53+
string = string.replace(letter + letter.upper(), '')
54+
string = string.replace(letter.upper() + letter, '')
55+
56+
shortest_len = min(shortest_len, len(string))
57+
58+
puzzle_actual_result = shortest_len
59+
60+
61+
# -------------------------------- Outputs / results -------------------------------- #
62+
63+
if verbose_level >= 3:
64+
print ('Input : ' + puzzle_input)
65+
print ('Expected result : ' + str(puzzle_expected_result))
66+
print ('Actual result : ' + str(puzzle_actual_result))
67+
68+
69+
70+

2018/06-Chronal Coordinates.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os, numpy as np
3+
from collections import Counter
4+
5+
test_data = {}
6+
7+
test = 1
8+
test_data[test] = {"input": """1, 1
9+
1, 6
10+
8, 3
11+
3, 4
12+
5, 5
13+
8, 9""",
14+
"expected": ['17', '16'],
15+
}
16+
17+
test = 'real'
18+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
19+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
20+
"expected": ['4060', '36136'],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test = 'real'
26+
part_to_test = 2
27+
verbose_level = 1
28+
29+
# -------------------------------- Initialize some variables -------------------------------- #
30+
31+
puzzle_input = test_data[case_to_test]['input']
32+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
33+
puzzle_actual_result = 'Unknown'
34+
35+
36+
# -------------------------------- Actual code execution -------------------------------- #
37+
38+
if part_to_test == 1:
39+
dots = []
40+
for string in puzzle_input.split('\n'):
41+
if string == '':
42+
continue
43+
44+
x, y = map(int, string.split(', '))
45+
46+
dots.append((x, y))
47+
48+
grid = {}
49+
min_x, max_x = min(dots)[0], max(dots)[0]
50+
min_y, max_y = min(dots, key=lambda d: d[1])[1], max(dots, key=lambda d: d[1])[1]
51+
for x in range (min_x - 1, max_x + 1):
52+
for y in range (min_y - 1, max_y + 1):
53+
min_distance = min([abs(x-dot[0])+abs(y-dot[1]) for dot in dots])
54+
for i, dot in enumerate(dots):
55+
if abs(x-dot[0])+abs(y-dot[1]) == min_distance:
56+
if grid.get((x, y), -1) != -1:
57+
grid[(x, y)] = -1
58+
break
59+
grid[(x, y)] = i
60+
61+
corners = set([-1])
62+
corners = corners.union(grid[x, min_y] for x in range(min_x - 1, max_x + 1))
63+
corners = corners.union(grid[x, max_y] for x in range(min_x - 1, max_x + 1))
64+
corners = corners.union(grid[min_x, y] for y in range(min_y - 1, max_y + 1))
65+
corners = corners.union(grid[max_x, y] for y in range(min_y - 1, max_y + 1))
66+
67+
puzzle_actual_result = next(x[1] for x in Counter(grid.values()).most_common() if x[0] not in corners)
68+
69+
70+
71+
72+
else:
73+
dots = []
74+
for string in puzzle_input.split('\n'):
75+
if string == '':
76+
continue
77+
78+
x, y = map(int, string.split(', '))
79+
80+
dots.append((x, y))
81+
82+
grid = {}
83+
min_x, max_x = min(dots)[0], max(dots)[0]
84+
min_y, max_y = min(dots, key=lambda d: d[1])[1], max(dots, key=lambda d: d[1])[1]
85+
for x in range (min_x - 1, max_x + 1):
86+
for y in range (min_y - 1, max_y + 1):
87+
for dot in dots:
88+
grid[(x, y)] = grid.get((x, y), 0) + abs(x-dot[0])+abs(y-dot[1])
89+
90+
puzzle_actual_result = sum(1 for x in grid if grid[x] < 10000)
91+
92+
93+
94+
# -------------------------------- Outputs / results -------------------------------- #
95+
96+
if verbose_level >= 3:
97+
print ('Input : ' + puzzle_input)
98+
print ('Expected result : ' + str(puzzle_expected_result))
99+
print ('Actual result : ' + str(puzzle_actual_result))
100+
101+
102+
103+

2018/07-The Sum of Its Parts.py

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# -------------------------------- Input data -------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {"input": """Step C must be finished before step A can begin.
8+
Step C must be finished before step F can begin.
9+
Step A must be finished before step B can begin.
10+
Step A must be finished before step D can begin.
11+
Step B must be finished before step E can begin.
12+
Step D must be finished before step E can begin.
13+
Step F must be finished before step E can begin.""",
14+
"expected": ['CABDFE', 'CABFDE'],
15+
}
16+
17+
test = 'real'
18+
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
19+
test_data[test] = {"input": open(input_file, "r+").read().strip(),
20+
"expected": ['OVXCKZBDEHINPFSTJLUYRWGAMQ', '955'],
21+
}
22+
23+
# -------------------------------- Control program execution -------------------------------- #
24+
25+
case_to_test = 1
26+
part_to_test = 1
27+
verbose_level = 1
28+
29+
# -------------------------------- Initialize some variables -------------------------------- #
30+
31+
puzzle_input = test_data[case_to_test]['input']
32+
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
33+
puzzle_actual_result = 'Unknown'
34+
35+
36+
# -------------------------------- Actual code execution -------------------------------- #
37+
38+
def list_remove (remove_list, element):
39+
try:
40+
remove_list.remove(element)
41+
return remove_list
42+
except ValueError:
43+
return remove_list
44+
45+
if part_to_test == 1:
46+
predecessors = {}
47+
dots = []
48+
for string in puzzle_input.split('\n'):
49+
_, source, _, _, _, _, _, target, *_ = string.split(' ')
50+
if not target in predecessors:
51+
predecessors[target] = [source]
52+
else:
53+
predecessors[target].append(source)
54+
55+
dots.append(target)
56+
dots.append(source)
57+
58+
dots = set(dots)
59+
60+
path = ''
61+
while len(path) != len(dots):
62+
next_dot = sorted(x for x in dots if x not in predecessors and x not in path)[0]
63+
path += next_dot
64+
predecessors = {x:list_remove(predecessors[x], next_dot) for x in predecessors}
65+
predecessors = {x:predecessors[x] for x in predecessors if len(predecessors[x])}
66+
67+
puzzle_actual_result = path
68+
69+
70+
71+
72+
else:
73+
predecessors = {}
74+
dots = []
75+
for string in puzzle_input.split('\n'):
76+
_, source, _, _, _, _, _, target, *_ = string.split(' ')
77+
if not target in predecessors:
78+
predecessors[target] = [source]
79+
else:
80+
predecessors[target].append(source)
81+
82+
dots.append(target)
83+
dots.append(source)
84+
85+
dots = set(dots)
86+
87+
88+
path = ''
89+
construction = []
90+
tick = 0
91+
while len(path) != len(dots):
92+
tick = 0 if len(construction) == 0 else min(x[2] for x in construction)
93+
finished = [x for x in construction if x[2] == tick]
94+
path += ''.join(x[0] for x in sorted(finished))
95+
predecessors = {x:list(set(predecessors[x]) - set(path)) for x in predecessors}
96+
predecessors = {x:predecessors[x] for x in predecessors if len(predecessors[x])}
97+
98+
construction = list(set(construction) - set(finished))
99+
in_construction = [x[0] for x in construction]
100+
101+
next_dots = sorted(x for x in dots if x not in predecessors and x not in path and x not in in_construction)
102+
workers_busy = sum(1 for worker in construction if worker[1] <= tick and worker[2] >= tick)
103+
104+
if len(next_dots) and workers_busy < 5:
105+
next_dots = sorted(next_dots)[:5-workers_busy]
106+
construction += [(next_dot, tick, tick + ord(next_dot) - ord('A') + 60 + 1) for next_dot in next_dots]
107+
108+
109+
110+
puzzle_actual_result = tick
111+
112+
113+
114+
# -------------------------------- Outputs / results -------------------------------- #
115+
116+
if verbose_level >= 3:
117+
print ('Input : ' + puzzle_input)
118+
print ('Expected result : ' + str(puzzle_expected_result))
119+
print ('Actual result : ' + str(puzzle_actual_result))
120+
121+
122+
123+

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