Skip to content

Commit db96045

Browse files
committed
Added 2021-04 & 2021-05 + additions to grid module
1 parent ca6aacc commit db96045

File tree

3 files changed

+412
-1
lines changed

3 files changed

+412
-1
lines changed

2021/04-Giant Squid.py

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
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": """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1
38+
39+
22 13 17 11 0
40+
8 2 23 4 24
41+
21 9 14 16 7
42+
6 10 3 18 5
43+
1 12 20 15 19
44+
45+
3 15 0 2 22
46+
9 18 13 17 5
47+
19 8 7 25 23
48+
20 11 10 24 4
49+
14 21 16 12 6
50+
51+
14 21 17 24 4
52+
10 16 15 9 19
53+
18 8 23 26 20
54+
22 11 13 6 5
55+
2 0 12 3 7""",
56+
"expected": ["4512", "1924"],
57+
}
58+
59+
test = "real"
60+
input_file = os.path.join(
61+
os.path.dirname(__file__),
62+
"Inputs",
63+
os.path.basename(__file__).replace(".py", ".txt"),
64+
)
65+
test_data[test] = {
66+
"input": open(input_file, "r+").read(),
67+
"expected": ["39984", "8468"],
68+
}
69+
70+
71+
# -------------------------------- Control program execution ------------------------- #
72+
73+
case_to_test = "real"
74+
part_to_test = 2
75+
76+
# -------------------------------- Initialize some variables ------------------------- #
77+
78+
puzzle_input = test_data[case_to_test]["input"]
79+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
80+
puzzle_actual_result = "Unknown"
81+
82+
83+
# -------------------------------- Actual code execution ----------------------------- #
84+
85+
# Conver integer to 36-character binary
86+
# str_value = "{0:>036b}".format(value)
87+
# Convert binary string to number
88+
# value = int(str_value, 2)
89+
90+
91+
if part_to_test == 1:
92+
numbers_drawn = ints(puzzle_input.split("\n")[0])
93+
94+
cards_init = {}
95+
cards = {}
96+
97+
for i, card in enumerate(puzzle_input.split("\n\n")[1:]):
98+
cards_init[i] = {}
99+
cards[i] = {}
100+
for r, row in enumerate(card.split("\n")):
101+
cards_init[i][r] = ints(row)
102+
cards[i][r] = ints(row)
103+
104+
for n in numbers_drawn:
105+
cards = {
106+
i: {r: [c if c != n else "x" for c in cards[i][r]] for r in cards[i]}
107+
for i in cards
108+
}
109+
110+
# Check rows
111+
for i in cards:
112+
for r in cards[i]:
113+
if cards[i][r] == ["x", "x", "x", "x", "x"]:
114+
winner_numbers = [
115+
cards_init[i][row][col]
116+
for row in cards[i]
117+
for col in range(5)
118+
if cards[i][row][col] != "x"
119+
]
120+
puzzle_actual_result = sum(winner_numbers) * int(n)
121+
break
122+
if puzzle_actual_result != "Unknown":
123+
break
124+
if puzzle_actual_result != "Unknown":
125+
break
126+
127+
# Check columns
128+
for i in cards:
129+
for c in range(5):
130+
if all(cards[i][r][c] == "x" for r in range(5)):
131+
winner_numbers = [
132+
cards_init[i][row][col]
133+
for row in cards[i]
134+
for col in range(5)
135+
if cards[i][row][col] != "x"
136+
]
137+
puzzle_actual_result = sum(winner_numbers) * int(n)
138+
break
139+
if puzzle_actual_result != "Unknown":
140+
break
141+
if puzzle_actual_result != "Unknown":
142+
break
143+
144+
145+
else:
146+
numbers_drawn = ints(puzzle_input.split("\n")[0])
147+
148+
cards_init = {}
149+
cards = {}
150+
151+
last_card = "Unknown"
152+
153+
for i, card in enumerate(puzzle_input.split("\n\n")[1:]):
154+
cards_init[i] = {}
155+
cards[i] = {}
156+
for r, row in enumerate(card.split("\n")):
157+
cards_init[i][r] = ints(row)
158+
cards[i][r] = ints(row)
159+
160+
for n in numbers_drawn:
161+
cards = {
162+
i: {r: [c if c != n else "x" for c in cards[i][r]] for r in cards[i]}
163+
for i in cards
164+
}
165+
166+
# Check rows
167+
to_remove = []
168+
for i in cards:
169+
for r in cards[i]:
170+
if cards[i][r] == ["x", "x", "x", "x", "x"]:
171+
to_remove.append(i)
172+
break
173+
174+
# Check columns
175+
for i in cards:
176+
for c in range(5):
177+
if all(cards[i][r][c] == "x" for r in range(5)):
178+
to_remove.append(i)
179+
break
180+
181+
if len(cards) == 1:
182+
last_card = list(cards.keys())[0]
183+
if last_card in to_remove:
184+
winner_numbers = [
185+
cards_init[last_card][row][col]
186+
for row in range(5)
187+
for col in range(5)
188+
if cards[last_card][row][col] != "x"
189+
]
190+
puzzle_actual_result = sum(winner_numbers) * int(n)
191+
break
192+
193+
cards = {i: cards[i] for i in cards if i not in to_remove}
194+
195+
196+
# -------------------------------- Outputs / results --------------------------------- #
197+
198+
print("Case :", case_to_test, "- Part", part_to_test)
199+
print("Expected result : " + str(puzzle_expected_result))
200+
print("Actual result : " + str(puzzle_actual_result))
201+
# Date created: 2021-12-05 18:08:14.982011
202+
# Part 1 : 2021-12-05 19:05:21
203+
# Part 2 : 2021-12-05 19:16:15

2021/05-Hydrothermal Venture.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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": """0,9 -> 5,9
38+
8,0 -> 0,8
39+
9,4 -> 3,4
40+
2,2 -> 2,1
41+
7,0 -> 7,4
42+
6,4 -> 2,0
43+
0,9 -> 2,9
44+
3,4 -> 1,4
45+
0,0 -> 8,8
46+
5,5 -> 8,2""",
47+
"expected": ["5", "12"],
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": ["7438", "Unknown"],
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+
dots = {}
84+
for string in puzzle_input.split("\n"):
85+
x1, y1, x2, y2 = ints(string)
86+
x1, x2 = min(x1, x2), max(x1, x2)
87+
y1, y2 = min(y1, y2), max(y1, y2)
88+
89+
if x1 != x2 and y1 != y2:
90+
continue
91+
new_dots = [x + 1j * y for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)]
92+
dots.update({pos: 1 if pos not in dots else 2 for pos in new_dots})
93+
94+
puzzle_actual_result = len([x for x in dots if dots[x] != 1])
95+
96+
97+
else:
98+
dots = {}
99+
for string in puzzle_input.split("\n"):
100+
x1, y1, x2, y2 = ints(string)
101+
102+
if x1 != x2 and y1 != y2:
103+
if x1 > x2:
104+
if y1 > y2:
105+
new_dots = [
106+
x1 + n - 1j * (y1 + n) for n in range(0, x2 - x1 - 1, -1)
107+
]
108+
else:
109+
new_dots = [
110+
x1 + n - 1j * (y1 - n) for n in range(0, x2 - x1 - 1, -1)
111+
]
112+
else:
113+
if y1 > y2:
114+
new_dots = [x1 + n - 1j * (y1 - n) for n in range(x2 - x1 + 1)]
115+
else:
116+
new_dots = [x1 + n - 1j * (y1 + n) for n in range(x2 - x1 + 1)]
117+
118+
else:
119+
x1, x2 = min(x1, x2), max(x1, x2)
120+
y1, y2 = min(y1, y2), max(y1, y2)
121+
new_dots = [
122+
x - 1j * y for x in range(x1, x2 + 1) for y in range(y1, y2 + 1)
123+
]
124+
# print (string, new_dots)
125+
dots.update({pos: 1 if pos not in dots else dots[pos] + 1 for pos in new_dots})
126+
127+
# print (dots)
128+
# grid = grid.Grid({i: str(dots[i]) for i in dots})
129+
# print (grid.dots_to_text())
130+
puzzle_actual_result = len([x for x in dots if dots[x] != 1])
131+
132+
# -------------------------------- Outputs / results --------------------------------- #
133+
134+
print("Case :", case_to_test, "- Part", part_to_test)
135+
print("Expected result : " + str(puzzle_expected_result))
136+
print("Actual result : " + str(puzzle_actual_result))
137+
# Date created: 2021-12-05 20:13:00
138+
# Part 1: 2021-12-05 20:22:20
139+
# Part 1: 2021-12-05 20:36:20

0 commit comments

Comments
 (0)