Last active
December 5, 2022 22:03
-
-
Save maurobaraldi/8d28a931a160095f3aa9b6579fe8518f to your computer and use it in GitHub Desktop.
Advent of Code 2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Solutions for Advent of Code 2022 | |
# Problem 1.1 | |
with open('./input.txt') as f: | |
elves_kal = [list(map(int, i.split())) for i in f.read().split('\n\n')] | |
print(max(sum(k) for k in elves_kal)) | |
# Problem 1.2 | |
from heapq import nlargest | |
print(sum(nlargest(3, [sum(k) for k in elves_kal]))) | |
# Problem 2.1 | |
with open('./input-2.txt') as f: | |
results = [i for i in f.read().strip().split('\n')] | |
table = {"A X": 4, "A Y": 8, "A Z": 3, "B X": 1, "B Y": 5, "B Z": 9, "C X": 7, "C Y": 2, "C Z": 6} | |
print(sum([table[result] for result in results])) | |
# Problem 2.2 | |
decrypt = { "A X": 3, "A Y": 4, "A Z": 8, "B X": 1, "B Y": 5, "B Z": 9, "C X": 2, "C Y": 6, "C Z": 7} | |
print(sum([decrypt[result] for result in results])) | |
# Problem 3.1 | |
with open('./input-3.txt') as f: | |
ruscksacks = [i for i in f.read().strip().split('\n')] | |
litems = {chr(i): i - 96 for i in range(97, 123)} | |
uitems = {chr(i): i - 38 for i in range(65, 91)} | |
result = 0 | |
for r in ruscksacks: | |
a, b = r[len(r)//2:], r[:len(r)//2] | |
for i in a: | |
if i in b: | |
result += litems.get(i, 0) + uitems.get(i, 0) | |
break | |
# Problem 3.2 | |
with open('./input-3.txt') as f: | |
elves_groups = [ruscksacks[i:i+3] for i in range(0, len(ruscksacks), 3)] | |
result = 0 | |
for eg in elves_groups: | |
e1, e2, e3 = eg | |
# i = next(iter(set(e1) & set(e2) & set(e3))) # hackerman solution | |
# result += litems.get(i, 0) + uitems.get(i, 0) | |
item = [i for i in e1 if i in e2 and i in e3][0] | |
result += litems.get(item, 0) + uitems.get(item, 0) | |
# Problem 4.1 | |
with open('./input-4.txt') as f: | |
sections = [i.strip().split(',') for i in f.readlines()] | |
result = 0 | |
for pairs in sections: | |
first, second = pairs | |
f1, f2, s1, s2 = list(map(int, first.split('-'))) + list(map(int, second.split('-'))) | |
# result += int((f1 >= s1 and f2 <= s2) or (s1 >= f1 and s2 <= f2)) # witchcraft | |
if (f1 >= s1 and f2 <= s2) or (s1 >= f1 and s2 <= f2): | |
result += 1 | |
# Problem 4.2 | |
result = 0 | |
for pairs in sections: | |
first, second = pairs | |
f1, f2, s1, s2 = list(map(int, first.split('-'))) + list(map(int, second.split('-'))) | |
# result += int(f1 <= s2 and f2 >= s1) # witchcraft | |
if f1 <= s2 and f2 >= s1: | |
result += 1 | |
# Problem 5.1 | |
with open('./input-5.txt') as f: | |
moves = [i.strip() for i in f.readlines() if i.startswith('m')] | |
stacks = { | |
1: 'Z T F R W J G'.split(), | |
2: 'G W M'.split(), | |
3: 'J N H G'.split(), | |
4: 'J R C N W'.split(), | |
5: 'W F S B G Q V M'.split(), | |
6: 'S R T D V W C'.split(), | |
7: 'H B N C D Z G V'.split(), | |
8: 'S J N M G C'.split(), | |
9: 'G P N W C J D L'.split() | |
} | |
for line in moves: | |
crates, _from, to = map(int, line.replace('move ', '').replace(' from ', ',').replace(' to ', ',').split(',')) | |
while crates > 0: | |
stacks[to].append(stacks[_from].pop()) | |
crates -= 1 | |
print(''.join([stacks[i].pop() for i in stacks])) | |
# Problem 5.2 | |
for line in moves: | |
print(stacks) | |
crates, _from, to = map(int, line.replace('move ', '').replace(' from ', ',').replace(' to ', ',').split(',')) | |
stacks[to] = stacks[to] + stacks[_from][(-1*crates):] | |
stacks[_from] = stacks[_from][:(-1*crates)] | |
print(''.join([stacks[i].pop() for i in stacks])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment