Skip to content

Commit e7b8a23

Browse files
committed
Added days 2020-21 and 2020-22
1 parent c086762 commit e7b8a23

File tree

2 files changed

+327
-0
lines changed

2 files changed

+327
-0
lines changed

2020/21-Allergen Assessment.py

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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": """mxmxvkd kfcds sqjhc nhms (contains dairy, fish)
38+
trh fvjkl sbzzf mxmxvkd (contains dairy)
39+
sqjhc fvjkl (contains soy)
40+
sqjhc mxmxvkd sbzzf (contains fish)""",
41+
"expected": ["5", "mxmxvkd,sqjhc,fvjkl"],
42+
}
43+
44+
test = "real"
45+
input_file = os.path.join(
46+
os.path.dirname(__file__),
47+
"Inputs",
48+
os.path.basename(__file__).replace(".py", ".txt"),
49+
)
50+
test_data[test] = {
51+
"input": open(input_file, "r+").read(),
52+
"expected": ["2410", "tmp,pdpgm,cdslv,zrvtg,ttkn,mkpmkx,vxzpfp,flnhl"],
53+
}
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+
all_ingredients = defaultdict(int)
71+
all_allergens = {}
72+
nb_allergens = defaultdict(int)
73+
allergens_ingredients = {}
74+
75+
for string in puzzle_input.split("\n"):
76+
if "contains" in string:
77+
ingredients = string.split(" (")[0].split(" ")
78+
allergens = string.split("(contains ")[1][:-1].split(", ")
79+
if isinstance(allergens, str):
80+
allergens = [allergens]
81+
82+
for allergen in allergens:
83+
nb_allergens[allergen] += 1
84+
if allergen not in all_allergens:
85+
all_allergens[allergen] = ingredients.copy()
86+
allergens_ingredients[allergen] = defaultdict(int)
87+
allergens_ingredients[allergen].update(
88+
{ingredient: 1 for ingredient in ingredients}
89+
)
90+
91+
else:
92+
for ingredient in ingredients:
93+
allergens_ingredients[allergen][ingredient] += 1
94+
for ingredient in all_allergens[allergen].copy():
95+
if ingredient not in ingredients:
96+
all_allergens[allergen].remove(ingredient)
97+
98+
for ingredient in ingredients:
99+
all_ingredients[ingredient] += 1
100+
101+
else:
102+
print("does not contain any allergen")
103+
104+
105+
for allergen in test:
106+
if allergen != "shellfish":
107+
continue
108+
print(
109+
allergen,
110+
test2[allergen],
111+
[ing for ing, val in test[allergen].items() if val == test2[allergen]],
112+
)
113+
114+
sum_ingredients = 0
115+
for ingredient in all_ingredients:
116+
if not (any(ingredient in val for val in all_allergens.values())):
117+
sum_ingredients += all_ingredients[ingredient]
118+
119+
if part_to_test == 1:
120+
puzzle_actual_result = sum_ingredients
121+
122+
123+
else:
124+
allergens_ingredients = {
125+
aller: [
126+
ing
127+
for ing, val in allergens_ingredients[aller].items()
128+
if val == nb_allergens[aller]
129+
]
130+
for aller in nb_allergens
131+
}
132+
final_allergen = {}
133+
while len(final_allergen) != len(nb_allergens):
134+
for allergen, val in allergens_ingredients.items():
135+
if len(val) == 1:
136+
final_allergen[allergen] = val[0]
137+
138+
allergens_ingredients = {
139+
aller: [
140+
ing
141+
for ing in allergens_ingredients[aller]
142+
if ing not in final_allergen.values()
143+
]
144+
for aller in nb_allergens
145+
}
146+
147+
print(final_allergen)
148+
ing_list = ""
149+
for aller in sorted(final_allergen.keys()):
150+
ing_list += final_allergen[aller] + ","
151+
puzzle_actual_result = ing_list[:-1]
152+
153+
# -------------------------------- Outputs / results --------------------------------- #
154+
155+
print("Case :", case_to_test, "- Part", part_to_test)
156+
print("Expected result : " + str(puzzle_expected_result))
157+
print("Actual result : " + str(puzzle_actual_result))
158+
# Date created: 2020-12-21 06:07:34.505688
159+
# Part 1: 2020-12-21 07:22:36
160+
# Part 2: 2020-12-21 07:30:15

2020/22-Crab Combat.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os, grid, graph, copy, 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": """Player 1:
38+
9
39+
2
40+
6
41+
3
42+
1
43+
44+
Player 2:
45+
5
46+
8
47+
4
48+
7
49+
10""",
50+
"expected": ["306", "291"],
51+
}
52+
53+
test += 1
54+
test_data[test] = {
55+
"input": """Player 1:
56+
43
57+
19
58+
59+
Player 2:
60+
2
61+
29
62+
14
63+
64+
""",
65+
"expected": ["Unknown", "1 wins"],
66+
}
67+
68+
test = "real"
69+
input_file = os.path.join(
70+
os.path.dirname(__file__),
71+
"Inputs",
72+
os.path.basename(__file__).replace(".py", ".txt"),
73+
)
74+
test_data[test] = {
75+
"input": open(input_file, "r+").read(),
76+
"expected": ["30197", "34031"],
77+
}
78+
79+
80+
# -------------------------------- Control program execution ------------------------- #
81+
82+
case_to_test = "real"
83+
part_to_test = 2
84+
85+
# -------------------------------- Initialize some variables ------------------------- #
86+
87+
puzzle_input = test_data[case_to_test]["input"]
88+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
89+
puzzle_actual_result = "Unknown"
90+
91+
92+
# -------------------------------- Actual code execution ----------------------------- #
93+
94+
if part_to_test == 1:
95+
players = puzzle_input.split("\n\n")
96+
cards = [ints(player) for i, player in enumerate(players)]
97+
cards[0].pop(0)
98+
cards[1].pop(0)
99+
100+
while len(cards[0]) != 0 and len(cards[1]) != 0:
101+
if cards[0][0] >= cards[1][0]:
102+
cards[0].append(cards[0].pop(0))
103+
cards[0].append(cards[1].pop(0))
104+
else:
105+
cards[1].append(cards[1].pop(0))
106+
cards[1].append(cards[0].pop(0))
107+
108+
winner = cards[0] + cards[1]
109+
110+
score = sum([card * (len(winner) - i) for i, card in enumerate(winner)])
111+
112+
puzzle_actual_result = score
113+
114+
115+
else:
116+
117+
def find_winner(cards):
118+
previous_decks = []
119+
120+
while len(cards[0]) != 0 and len(cards[1]) != 0:
121+
# #print ('before', cards)
122+
if cards in previous_decks:
123+
return (0, 0)
124+
previous_decks.append(copy.deepcopy(cards))
125+
126+
if cards[0][0] < len(cards[0]) and cards[1][0] < len(cards[1]):
127+
# #print ('subgame')
128+
winner, score = find_winner(
129+
[cards[0][1 : cards[0][0] + 1], cards[1][1 : cards[1][0] + 1]]
130+
)
131+
# #print ('subgame won by', winner)
132+
cards[winner].append(cards[winner].pop(0))
133+
cards[winner].append(cards[1 - winner].pop(0))
134+
135+
elif cards[0][0] >= cards[1][0]:
136+
cards[0].append(cards[0].pop(0))
137+
cards[0].append(cards[1].pop(0))
138+
else:
139+
cards[1].append(cards[1].pop(0))
140+
cards[1].append(cards[0].pop(0))
141+
142+
winner = [i for i in (0, 1) if cards[i] != []][0]
143+
144+
score = sum(
145+
[card * (len(cards[winner]) - i) for i, card in enumerate(cards[winner])]
146+
)
147+
148+
return (winner, score)
149+
150+
players = puzzle_input.split("\n\n")
151+
cards = [ints(player) for i, player in enumerate(players)]
152+
cards[0].pop(0)
153+
cards[1].pop(0)
154+
155+
# #print (find_winner(cards))
156+
157+
puzzle_actual_result = find_winner(cards)[1]
158+
159+
160+
# -------------------------------- Outputs / results --------------------------------- #
161+
162+
print("Case :", case_to_test, "- Part", part_to_test)
163+
print("Expected result : " + str(puzzle_expected_result))
164+
print("Actual result : " + str(puzzle_actual_result))
165+
# Date created: 2020-12-22 06:31:42.000598
166+
# Part 1: 2020-12-22 06:38:55
167+
# Part 2: 2020-12-22 07:01:53

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