Skip to content

Commit 7dd9487

Browse files
committed
Added days 2021-14 and 2021-15
1 parent 1f84a2a commit 7dd9487

File tree

2 files changed

+264
-0
lines changed

2 files changed

+264
-0
lines changed

2021/14-Extended Polymerization.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """NNCB
38+
39+
CH -> B
40+
HH -> N
41+
CB -> H
42+
NH -> C
43+
HB -> C
44+
HC -> B
45+
HN -> C
46+
NN -> C
47+
BH -> H
48+
NC -> B
49+
NB -> B
50+
BN -> B
51+
BB -> N
52+
BC -> B
53+
CC -> N
54+
CN -> C""",
55+
"expected": ["1588", "2188189693529"],
56+
}
57+
58+
test = "real"
59+
input_file = os.path.join(
60+
os.path.dirname(__file__),
61+
"Inputs",
62+
os.path.basename(__file__).replace(".py", ".txt"),
63+
)
64+
test_data[test] = {
65+
"input": open(input_file, "r+").read(),
66+
"expected": ["3259", "3459174981021"],
67+
}
68+
69+
70+
# -------------------------------- Control program execution ------------------------- #
71+
72+
case_to_test = "real"
73+
part_to_test = 2
74+
75+
# -------------------------------- Initialize some variables ------------------------- #
76+
77+
puzzle_input = test_data[case_to_test]["input"]
78+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
79+
puzzle_actual_result = "Unknown"
80+
81+
82+
# -------------------------------- Actual code execution ----------------------------- #
83+
84+
# Conver integer to 36-character binary
85+
# str_value = "{0:>036b}".format(value)
86+
# Convert binary string to number
87+
# value = int(str_value, 2)
88+
89+
nb_counts = 10 if part_to_test == 1 else 40
90+
91+
92+
# This was the first, obvious solution
93+
# Works well for part 1, not for part 2
94+
# source = puzzle_input.split("\n\n")[0]
95+
# maps = puzzle_input.split("\n\n")[1]
96+
# mapping = {}
97+
# for string in maps.split("\n"):
98+
# mapping[string.split(' -> ')[0]] = string.split(' -> ')[1] + string[1]
99+
100+
# word = source
101+
# for j in range(nb_counts):
102+
# target = word[0]
103+
# target += ''.join([mapping[word[i:i+2]] if word[i:i+2] in mapping else word[i+1] for i in range(len(word)-1)])
104+
105+
# word = target
106+
107+
108+
# occurrences = Counter(word)
109+
# print (occurrences)
110+
# puzzle_actual_result = max(occurrences.values()) - min(occurrences.values())
111+
112+
113+
source = puzzle_input.split("\n\n")[0]
114+
maps = puzzle_input.split("\n\n")[1]
115+
mapping = {}
116+
for string in maps.split("\n"):
117+
mapping[string.split(" -> ")[0]] = string.split(" -> ")[1]
118+
119+
elem_count = Counter(source)
120+
pair_count = defaultdict(int)
121+
for i in range(len(source) - 1):
122+
pair_count[source[i : i + 2]] += 1
123+
124+
print(elem_count, pair_count)
125+
126+
for j in range(nb_counts):
127+
for pair, nb_pair in pair_count.copy().items():
128+
pair_count[pair] -= nb_pair
129+
new_elem = mapping[pair]
130+
pair_count[pair[0] + new_elem] += nb_pair
131+
pair_count[new_elem + pair[1]] += nb_pair
132+
elem_count[new_elem] += nb_pair
133+
134+
135+
puzzle_actual_result = max(elem_count.values()) - min(elem_count.values())
136+
137+
# -------------------------------- Outputs / results --------------------------------- #
138+
139+
print("Case :", case_to_test, "- Part", part_to_test)
140+
print("Expected result : " + str(puzzle_expected_result))
141+
print("Actual result : " + str(puzzle_actual_result))
142+
# Date created: 2021-12-14 08:37:51.348152
143+
# Part 1: 2021-12-14 08:42:56
144+
# Part 2: 2021-12-14 08:56:13

2021/15-Chiton.py

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, copy
3+
from collections import Counter, deque, defaultdict
4+
5+
from compass import *
6+
7+
8+
# This functions come from https://github.com/mcpower/adventofcode - Thanks!
9+
def lmap(func, *iterables):
10+
return list(map(func, *iterables))
11+
12+
13+
def ints(s: str):
14+
return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano!
15+
16+
17+
def positive_ints(s: str):
18+
return lmap(int, re.findall(r"\d+", s)) # thanks mserrano!
19+
20+
21+
def floats(s: str):
22+
return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s))
23+
24+
25+
def positive_floats(s: str):
26+
return lmap(float, re.findall(r"\d+(?:\.\d+)?", s))
27+
28+
29+
def words(s: str):
30+
return re.findall(r"[a-zA-Z]+", s)
31+
32+
33+
test_data = {}
34+
35+
test = 1
36+
test_data[test] = {
37+
"input": """1163751742
38+
1381373672
39+
2136511328
40+
3694931569
41+
7463417111
42+
1319128137
43+
1359912421
44+
3125421639
45+
1293138521
46+
2311944581""",
47+
"expected": ["40", "315"],
48+
}
49+
50+
test = "real"
51+
input_file = os.path.join(
52+
os.path.dirname(__file__),
53+
"Inputs",
54+
os.path.basename(__file__).replace(".py", ".txt"),
55+
)
56+
test_data[test] = {
57+
"input": open(input_file, "r+").read(),
58+
"expected": ["769", "2963"],
59+
}
60+
61+
62+
# -------------------------------- Control program execution ------------------------- #
63+
64+
case_to_test = "real"
65+
part_to_test = 2
66+
67+
# -------------------------------- Initialize some variables ------------------------- #
68+
69+
puzzle_input = test_data[case_to_test]["input"]
70+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
71+
puzzle_actual_result = "Unknown"
72+
73+
74+
# -------------------------------- Actual code execution ----------------------------- #
75+
76+
# Conver integer to 36-character binary
77+
# str_value = "{0:>036b}".format(value)
78+
# Convert binary string to number
79+
# value = int(str_value, 2)
80+
81+
82+
caves = grid.Grid()
83+
caves.text_to_dots(puzzle_input, convert_to_int=True)
84+
85+
width, height = caves.get_size()
86+
87+
if part_to_test == 2:
88+
list_caves = []
89+
for x in range(5):
90+
for y in range(5):
91+
new_cave = copy.deepcopy(caves)
92+
for dot in new_cave.dots:
93+
new_cave.dots[dot].terrain = (
94+
new_cave.dots[dot].terrain + x + y - 1
95+
) % 9 + 1
96+
list_caves.append(new_cave)
97+
caves = grid.merge_grids(list_caves, 5, 5)
98+
99+
edges = {}
100+
for dot in caves.dots:
101+
neighbors = caves.dots[dot].get_neighbors()
102+
edges[caves.dots[dot]] = {target: target.terrain for target in neighbors}
103+
104+
min_x, max_x, min_y, max_y = caves.get_box()
105+
start = caves.dots[min_x + 1j * max_y]
106+
end = caves.dots[max_x + 1j * min_y]
107+
108+
caves_graph = graph.WeightedGraph(caves.dots, edges)
109+
caves_graph.dijkstra(start, end)
110+
puzzle_actual_result = caves_graph.distance_from_start[end]
111+
112+
113+
# -------------------------------- Outputs / results --------------------------------- #
114+
115+
print("Case :", case_to_test, "- Part", part_to_test)
116+
print("Expected result : " + str(puzzle_expected_result))
117+
print("Actual result : " + str(puzzle_actual_result))
118+
# Date created: 2021-12-15 08:16:43.421298
119+
# Part 1: 2021-12-15 08:38:06
120+
# Part 2: 2021-12-15 09:48:14

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