Skip to content

Commit 63af5e1

Browse files
committed
Started 2020, added day 2020-01 and various utilities
1 parent d737eb6 commit 63af5e1

File tree

8 files changed

+1699
-38
lines changed

8 files changed

+1699
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ Inputs/
22
template.py
33
__pycache__
44
parse/
5+
download.py

2016/24-Air Duct Spelunking.py

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,50 +4,54 @@
44
test_data = {}
55

66
test = 1
7-
test_data[test] = {"input": """###########
7+
test_data[test] = {
8+
"input": """###########
89
#0.1.....2#
910
#.#######.#
1011
#4.......3#
1112
###########""",
12-
"expected": ['Unknown', 'Unknown'],
13-
}
14-
15-
test += 1
16-
test_data[test] = {"input": """""",
17-
"expected": ['Unknown', 'Unknown'],
18-
}
19-
20-
test = 'real'
21-
input_file = os.path.join(os.path.dirname(__file__), 'Inputs', os.path.basename(__file__).replace('.py', '.txt'))
22-
test_data[test] = {"input": open(input_file, "r+").read().strip(),
23-
"expected": ['442', 'Unknown'],
24-
}
13+
"expected": ["Unknown", "Unknown"],
14+
}
15+
16+
test = "real"
17+
input_file = os.path.join(
18+
os.path.dirname(__file__),
19+
"Inputs",
20+
os.path.basename(__file__).replace(".py", ".txt"),
21+
)
22+
test_data[test] = {
23+
"input": open(input_file, "r+").read().strip(),
24+
"expected": ["442", "660"],
25+
}
2526

2627
# -------------------------------- Control program execution -------------------------------- #
2728

28-
case_to_test = 'real'
29-
part_to_test = 2
29+
case_to_test = "real"
30+
part_to_test = 2
3031
verbose_level = 1
3132

3233
# -------------------------------- Initialize some variables -------------------------------- #
3334

34-
puzzle_input = test_data[case_to_test]['input']
35-
puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1]
36-
puzzle_actual_result = 'Unknown'
35+
puzzle_input = test_data[case_to_test]["input"]
36+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
37+
puzzle_actual_result = "Unknown"
3738

3839

3940
# -------------------------------- Actual code execution -------------------------------- #
4041

4142
grid = puzzle_input
42-
graph = pathfinding.WeightedGraph ()
43-
graph.grid_to_vertices(re.sub('[0-9]', '.', puzzle_input))
43+
graph = pathfinding.WeightedGraph()
44+
graph.grid_to_vertices(re.sub("[0-9]", ".", puzzle_input))
4445

4546
waypoints = {}
46-
for i in range (10):
47+
for i in range(10):
4748
if str(i) in grid:
48-
waypoints[i] = (grid.find(str(i)) % (len(grid.split('\n')[0])+1), grid.find(str(i)) // (len(grid.split('\n')[0])+1))
49+
waypoints[i] = (
50+
grid.find(str(i)) % (len(grid.split("\n")[0]) + 1),
51+
grid.find(str(i)) // (len(grid.split("\n")[0]) + 1),
52+
)
4953

50-
edges = {waypoints[x]:{} for x in waypoints}
54+
edges = {waypoints[x]: {} for x in waypoints}
5155
for a in waypoints:
5256
for b in waypoints:
5357
if waypoints[a] <= waypoints[b]:
@@ -59,7 +63,7 @@
5963
edges[waypoints[b]][waypoints[a]] = graph.distance_from_start[waypoints[b]]
6064
graph.reset_search()
6165

62-
min_length = 10**6
66+
min_length = 10 ** 6
6367
for order in itertools.permutations([waypoints[x] for x in waypoints if x != 0]):
6468
length = 0
6569
current_waypoint = waypoints[0]
@@ -74,19 +78,9 @@
7478
puzzle_actual_result = min_length
7579

7680

77-
78-
79-
80-
81-
82-
8381
# -------------------------------- Outputs / results -------------------------------- #
8482

8583
if verbose_level >= 3:
86-
print ('Input : ' + puzzle_input)
87-
print ('Expected result : ' + str(puzzle_expected_result))
88-
print ('Actual result : ' + str(puzzle_actual_result))
89-
90-
91-
92-
84+
print("Input : " + puzzle_input)
85+
print("Expected result : " + str(puzzle_expected_result))
86+
print("Actual result : " + str(puzzle_actual_result))

2020/01-Report Repair.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, math
3+
4+
from compass import *
5+
6+
7+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
8+
def lmap(func, *iterables):
9+
return list(map(func, *iterables))
10+
11+
12+
def ints(s: str):
13+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
14+
15+
16+
def positive_ints(s: str):
17+
return lmap(int, re.findall(r"\d+", s)) # t hanks mserrano!
18+
19+
20+
def floats(s: str):
21+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
22+
23+
24+
def positive_floats(s: str):
25+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
26+
27+
28+
def words(s: str):
29+
return re.findall(r"[a-zA-Z]+", s)
30+
31+
32+
test_data = {}
33+
34+
test = 1
35+
test_data[test] = {
36+
"input": """1721
37+
979
38+
366
39+
299
40+
675
41+
1456""",
42+
"expected": ["514579", "241861950"],
43+
}
44+
45+
test = "real"
46+
input_file = os.path.join(
47+
os.path.dirname(__file__),
48+
"Inputs",
49+
os.path.basename(__file__).replace(".py", ".txt"),
50+
)
51+
test_data[test] = {
52+
"input": open(input_file, "r+").read(),
53+
"expected": ["997899", "131248694"],
54+
}
55+
56+
# -------------------------------- Control program execution ------------------------- #
57+
58+
case_to_test = "real"
59+
part_to_test = 2
60+
61+
# -------------------------------- Initialize some variables ------------------------- #
62+
63+
puzzle_input = test_data[case_to_test]["input"]
64+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
65+
puzzle_actual_result = "Unknown"
66+
67+
68+
# -------------------------------- Actual code execution ----------------------------- #
69+
70+
values = sorted(ints(puzzle_input))
71+
72+
for a in itertools.combinations(values, part_to_test + 1):
73+
if sum(a) == 2020:
74+
puzzle_actual_result = math.prod(a)
75+
break
76+
77+
78+
# -------------------------------- Outputs / results --------------------------------- #
79+
80+
print("Case :", case_to_test, "- Part", part_to_test)
81+
print("Expected result : " + str(puzzle_expected_result))
82+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)