Skip to content

Commit 034feec

Browse files
committed
Added days 2021-09, 2021-10, 2021-11, 2021-12 and 2021-13
1 parent 80e258e commit 034feec

File tree

5 files changed

+837
-0
lines changed

5 files changed

+837
-0
lines changed

2021/09-Smoke Basin.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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": """2199943210
38+
3987894921
39+
9856789892
40+
8767896789
41+
9899965678""",
42+
"expected": ["15", "1134"],
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": ["508", "1564640"],
54+
}
55+
56+
57+
# -------------------------------- Control program execution ------------------------- #
58+
59+
case_to_test = "real"
60+
part_to_test = 2
61+
62+
# -------------------------------- Initialize some variables ------------------------- #
63+
64+
puzzle_input = test_data[case_to_test]["input"]
65+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
66+
puzzle_actual_result = "Unknown"
67+
68+
69+
# -------------------------------- Actual code execution ----------------------------- #
70+
71+
# Conver integer to 36-character binary
72+
# str_value = "{0:>036b}".format(value)
73+
# Convert binary string to number
74+
# value = int(str_value, 2)
75+
76+
77+
if part_to_test == 1:
78+
area = grid.Grid()
79+
area.text_to_dots(puzzle_input)
80+
risk_level = 0
81+
for dot in area.dots:
82+
if all(
83+
[
84+
int(neighbor.terrain) > int(area.dots[dot].terrain)
85+
for neighbor in area.dots[dot].get_neighbors()
86+
]
87+
):
88+
risk_level += int(area.dots[dot].terrain) + 1
89+
90+
puzzle_actual_result = risk_level
91+
92+
93+
else:
94+
areas = puzzle_input.replace("9", "#")
95+
area = grid.Grid()
96+
area.text_to_dots(areas)
97+
98+
area_graph = area.convert_to_graph()
99+
basins = area_graph.dfs_groups()
100+
sizes = sorted([len(x) for x in basins])
101+
102+
puzzle_actual_result = sizes[-1] * sizes[-2] * sizes[-3]
103+
104+
# -------------------------------- Outputs / results --------------------------------- #
105+
106+
print("Case :", case_to_test, "- Part", part_to_test)
107+
print("Expected result : " + str(puzzle_expected_result))
108+
print("Actual result : " + str(puzzle_actual_result))
109+
# Date created: 2021-12-09 18:13:45.008055
110+
# Part 1: 2021-12-09 18:18:53
111+
# Part 2: 2021-12-09 18:25:25

2021/10-Syntax Scoring.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, dot, assembly, re, itertools, statistics
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": """[({(<(())[]>[[{[]{<()<>>
38+
[(()[<>])]({[<{<<[]>>(
39+
{([(<{}[<>[]}>{[]{[(<()>
40+
(((({<>}<{<{<>}{[]{[]{}
41+
[[<[([]))<([[{}[[()]]]
42+
[{[{({}]{}}([{[{{{}}([]
43+
{<[[]]>}<{[{[{[]{()[[[]
44+
[<(<(<(<{}))><([]([]()
45+
<{([([[(<>()){}]>(<<{{
46+
<{([{{}}[<[[[<>{}]]]>[]]""",
47+
"expected": ["26397", "288957"],
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": ["268845", "4038824534"],
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+
if part_to_test == 1:
83+
symbols = ["()", "[]", "<>", "{}"]
84+
opening_symbols = ["(", "[", "<", "{"]
85+
match = {"(": ")", "[": "]", "<": ">", "{": "}"}
86+
score = {")": 3, "]": 57, ">": 25137, "}": 1197}
87+
syntax_score = 0
88+
for string in puzzle_input.split("\n"):
89+
for i in range(15):
90+
for symbol in symbols:
91+
string = string.replace(symbol, "")
92+
93+
while string != "" and string[-1] in opening_symbols:
94+
string = string[:-1]
95+
96+
if string == "":
97+
continue
98+
99+
for i in range(len(string)):
100+
if string[i] in opening_symbols:
101+
last_character = string[i]
102+
else:
103+
if string[i] == match[last_character]:
104+
print("Cant compute")
105+
else:
106+
syntax_score += score[string[i]]
107+
break
108+
109+
puzzle_actual_result = syntax_score
110+
111+
112+
else:
113+
symbols = ["()", "[]", "<>", "{}"]
114+
opening_symbols = ["(", "[", "<", "{"]
115+
match = {"(": ")", "[": "]", "<": ">", "{": "}"}
116+
score = {")": 1, "]": 2, ">": 4, "}": 3}
117+
all_scores = []
118+
print_it = False
119+
for string in puzzle_input.split("\n"):
120+
syntax_score = 0
121+
string2 = string
122+
# Determine whether it's an incomplete or erroneous line
123+
for i in range(10):
124+
for symbol in symbols:
125+
string2 = string2.replace(symbol, "")
126+
127+
while string2 != "" and string2[-1] in opening_symbols:
128+
string2 = string2[:-1]
129+
130+
if string2 != "":
131+
continue
132+
133+
# Remove matching elements
134+
for i in range(15):
135+
for symbol in symbols:
136+
string = string.replace(symbol, "")
137+
138+
missing_letters = ""
139+
for letter in string:
140+
if letter in match:
141+
missing_letters = match[letter] + missing_letters
142+
143+
for letter in missing_letters:
144+
syntax_score *= 5
145+
syntax_score += score[letter]
146+
147+
all_scores.append(syntax_score)
148+
149+
puzzle_actual_result = statistics.median(all_scores)
150+
151+
# -------------------------------- Outputs / results --------------------------------- #
152+
153+
print("Case :", case_to_test, "- Part", part_to_test)
154+
print("Expected result : " + str(puzzle_expected_result))
155+
print("Actual result : " + str(puzzle_actual_result))
156+
# Date created: 2021-12-10 07:58:18.043288
157+
# Part 1: 2021-12-10 08:06:21
158+
# Part 2: 2021-12-10 08:30:02

0 commit comments

Comments
 (0)