day 1 and day 2 solutions

This commit is contained in:
Gleb Koval 2023-12-02 15:59:13 +00:00
commit 900692ce25
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
7 changed files with 1206 additions and 0 deletions

4
day1/eg1.txt Normal file
View File

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

7
day1/eg2.txt Normal file
View File

@ -0,0 +1,7 @@
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen

1000
day1/input.txt Normal file

File diff suppressed because it is too large Load Diff

34
day1/solution.py Normal file
View File

@ -0,0 +1,34 @@
#!/usr/bin/env python3.12
from typing import Iterable
def first_digit(s: str):
for idx, c in enumerate(s):
if "0" <= c <= "9":
return idx, int(c)
def first_word(s: str, words: list[str]):
indexes = [(s.find(w), w) for w in words]
try:
midx = min([idx for idx in indexes if idx[0] != -1])
return midx[0], words.index(midx[1]) + 1
except ValueError:
return None
def not_null[T](i: Iterable[T | None]):
return [a for a in i if a is not None]
digits = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
rdigits = [d[::-1] for d in digits]
if __name__ == "__main__":
with open("input.txt") as f:
lines = [l for l in f.read().split("\n") if l != ""]
print("Part 1:", sum([
first_digit(line)[1] * 10 + first_digit(line[::-1])[1]
for line in lines
]))
print("Part 2:", sum([
min(not_null([first_digit(line), first_word(line, digits)]))[1] * 10 +
min(not_null([first_digit(rline := line[::-1]), first_word(rline, rdigits)]))[1]
for line in lines
]))

5
day2/eg.txt Normal file
View File

@ -0,0 +1,5 @@
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green

100
day2/input.txt Normal file
View File

@ -0,0 +1,100 @@
Game 1: 4 green, 7 blue; 2 blue, 4 red; 5 blue, 2 green, 2 red; 1 green, 3 red, 9 blue; 3 green, 9 blue; 7 green, 2 blue, 2 red
Game 2: 1 blue, 2 red; 1 green, 2 blue, 1 red; 1 red, 5 green; 3 red, 2 blue, 8 green; 3 blue, 2 red, 4 green; 2 blue, 4 green, 3 red
Game 3: 7 red, 7 blue, 9 green; 15 green, 4 red, 8 blue; 3 green, 12 blue, 6 red
Game 4: 4 blue, 11 green, 6 red; 4 green, 2 red; 12 red, 1 blue, 3 green
Game 5: 10 green, 4 blue, 9 red; 3 green, 15 blue, 11 red; 15 blue, 1 green, 2 red; 8 red, 8 blue, 5 green
Game 6: 5 green, 19 red; 6 green, 13 red, 2 blue; 2 blue, 16 red, 4 green; 13 red, 9 blue, 5 green
Game 7: 1 blue, 6 red, 6 green; 7 blue, 4 red; 6 green, 1 red, 11 blue; 3 green, 4 blue, 4 red; 6 green, 13 blue, 11 red
Game 8: 8 green, 2 blue; 20 green, 1 red; 1 blue, 6 red, 6 green; 9 green
Game 9: 5 red; 4 green, 3 red, 1 blue; 1 blue; 6 red, 1 blue, 9 green
Game 10: 2 green, 3 red; 18 blue, 20 green, 9 red; 7 red, 9 blue, 17 green
Game 11: 15 green, 7 blue, 9 red; 7 blue, 10 green, 7 red; 5 red, 3 blue, 10 green; 5 blue, 12 green; 14 green, 8 blue, 5 red; 7 blue, 2 red, 5 green
Game 12: 3 green, 1 red, 8 blue; 9 blue, 3 red, 3 green; 4 blue, 1 green; 2 red, 3 green, 1 blue; 4 red, 7 blue, 3 green
Game 13: 14 green, 1 red; 4 green; 2 green, 1 blue; 14 green; 13 green, 1 red, 1 blue; 1 blue, 1 red, 5 green
Game 14: 1 blue, 6 red, 13 green; 5 red, 10 blue, 3 green; 19 green, 1 red, 14 blue; 4 red, 17 green, 9 blue; 12 green, 10 blue, 7 red
Game 15: 10 red, 2 blue, 18 green; 17 green, 3 blue, 7 red; 18 blue, 8 red, 12 green; 6 blue, 6 green, 12 red
Game 16: 14 blue, 5 green, 12 red; 7 green, 3 red, 9 blue; 4 green, 1 red, 8 blue; 9 red, 19 green, 12 blue; 12 blue, 7 red, 6 green; 5 blue, 3 green, 6 red
Game 17: 1 green, 1 blue, 15 red; 1 blue, 3 green, 12 red; 9 blue, 2 green, 10 red
Game 18: 12 red, 7 green, 7 blue; 3 blue, 8 red, 1 green; 2 green, 17 red
Game 19: 1 red, 7 blue, 17 green; 11 red, 15 blue; 11 blue, 18 green; 6 blue, 14 green, 14 red; 16 blue, 8 red, 8 green; 17 green, 9 red, 1 blue
Game 20: 11 green; 1 blue, 4 green, 7 red; 7 green; 3 red, 1 blue, 6 green
Game 21: 18 blue, 6 green, 10 red; 12 blue; 9 blue, 2 green, 9 red; 9 red, 20 blue, 1 green; 8 blue, 6 red; 19 blue, 1 green, 4 red
Game 22: 4 blue, 16 red, 3 green; 7 blue, 3 green, 12 red; 10 red, 7 green, 10 blue; 7 red, 11 blue, 4 green; 3 blue, 1 green, 16 red
Game 23: 5 green, 8 red, 1 blue; 2 red, 5 blue, 3 green; 2 green, 17 blue, 4 red; 2 blue, 2 red; 7 red, 1 green, 14 blue; 4 red, 8 blue
Game 24: 1 blue, 3 green, 1 red; 3 blue, 11 green, 15 red; 3 blue, 2 red, 12 green; 9 green, 6 red, 2 blue; 15 green, 12 red, 3 blue; 13 green, 1 blue, 13 red
Game 25: 6 blue, 5 red, 10 green; 9 red, 3 blue, 3 green; 6 blue, 11 red, 15 green; 7 green, 10 red, 4 blue; 2 red, 20 blue, 11 green
Game 26: 3 blue, 5 red, 10 green; 7 green, 6 red; 7 green, 1 red, 3 blue; 10 green, 4 red, 3 blue; 4 red, 7 green, 3 blue; 8 green, 4 red
Game 27: 16 green, 6 blue, 4 red; 3 red, 6 blue, 7 green; 10 green; 6 green, 1 red
Game 28: 5 green, 5 red, 2 blue; 1 blue, 9 red, 6 green; 2 blue, 3 red; 1 blue, 1 green, 5 red; 4 green, 3 red; 9 green, 1 blue, 14 red
Game 29: 1 red, 2 green, 13 blue; 1 green, 2 red, 9 blue; 12 red; 3 blue, 5 red
Game 30: 8 blue, 3 red, 9 green; 10 green, 9 blue; 9 green, 12 blue; 3 blue, 2 red, 4 green; 8 blue, 9 green; 1 red, 12 blue, 6 green
Game 31: 8 red, 16 blue; 2 red, 1 green, 1 blue; 5 red, 8 blue, 1 green
Game 32: 3 green, 5 blue, 8 red; 10 blue, 1 red, 3 green; 9 green, 3 blue, 2 red; 2 blue, 1 red, 14 green; 3 blue, 10 red, 16 green
Game 33: 1 red, 3 green, 2 blue; 15 blue, 1 green; 1 green, 1 red, 10 blue
Game 34: 4 green, 7 blue; 2 blue, 12 green; 6 red, 14 green, 7 blue
Game 35: 9 blue, 1 green; 2 green, 6 blue, 11 red; 1 green, 10 red, 1 blue
Game 36: 5 blue, 1 green, 2 red; 11 blue, 3 green, 5 red; 2 green, 14 blue, 2 red; 3 green, 5 blue, 5 red; 13 blue, 2 green, 5 red; 3 green
Game 37: 4 blue, 2 red, 8 green; 1 blue, 9 green, 4 red; 1 red, 4 green, 1 blue; 16 green, 3 blue, 4 red
Game 38: 9 blue, 9 green; 1 green, 3 blue; 8 blue, 6 red, 5 green; 1 green, 9 red, 1 blue
Game 39: 1 red, 17 green; 1 blue, 7 green, 7 red; 6 red, 4 blue
Game 40: 1 blue, 2 red; 10 blue, 4 green, 2 red; 1 green, 11 blue, 3 red; 4 blue, 2 green; 3 blue, 4 red
Game 41: 7 red, 4 blue, 4 green; 10 red, 11 blue, 1 green; 6 red, 6 blue, 4 green; 13 blue, 3 red, 7 green; 9 green, 12 blue, 14 red; 9 blue, 12 red, 10 green
Game 42: 3 blue, 1 red, 11 green; 4 blue, 9 green, 8 red; 3 red, 5 blue, 1 green
Game 43: 2 green, 17 blue, 9 red; 16 red, 12 blue, 2 green; 12 red, 12 blue, 7 green; 17 red, 16 blue, 7 green
Game 44: 2 red, 3 green, 5 blue; 5 red, 5 blue, 7 green; 2 red, 5 blue, 5 green; 6 red, 5 blue, 2 green
Game 45: 9 green, 1 blue; 1 red, 5 green, 2 blue; 2 blue, 4 green, 9 red; 13 green, 7 red, 1 blue; 3 blue, 4 green
Game 46: 5 green, 7 red; 8 green, 5 blue, 1 red; 1 blue, 7 red, 17 green
Game 47: 1 green, 17 blue; 9 blue, 1 green; 1 blue, 1 red
Game 48: 1 red, 6 green, 7 blue; 9 green, 1 red, 2 blue; 10 blue, 6 green, 1 red; 1 red, 4 green, 9 blue; 6 blue, 3 green, 1 red
Game 49: 1 red, 1 blue, 16 green; 3 red, 1 green; 16 green; 2 blue, 8 red, 19 green; 20 green, 9 red; 8 green, 6 red
Game 50: 1 green, 2 blue; 2 red, 3 blue; 4 red, 2 blue; 1 green; 3 blue; 3 blue, 2 green, 1 red
Game 51: 6 blue, 5 green; 6 red, 5 green; 6 green, 6 blue, 16 red; 10 red, 10 green, 1 blue
Game 52: 4 red, 5 green, 1 blue; 15 green, 1 blue; 8 green, 5 blue, 5 red; 4 blue, 11 green, 1 red
Game 53: 5 green, 3 blue, 5 red; 2 red, 4 blue, 1 green; 1 red, 2 green; 11 red
Game 54: 7 green, 16 blue, 5 red; 5 green; 10 blue, 6 green, 5 red; 3 green
Game 55: 3 green; 16 green, 1 blue; 13 green, 19 blue, 1 red; 13 green, 18 blue
Game 56: 9 green; 3 blue, 1 red, 10 green; 1 red, 4 blue, 9 green
Game 57: 14 blue, 2 red, 3 green; 1 red, 8 blue, 7 green; 1 green, 3 red, 15 blue; 5 green, 12 blue; 4 green, 15 blue
Game 58: 9 red, 5 green; 10 green, 11 red, 1 blue; 12 green, 17 red, 1 blue; 1 blue, 1 green, 17 red; 14 red, 1 blue, 16 green
Game 59: 2 red, 12 blue, 10 green; 6 green, 1 red, 14 blue; 14 blue, 9 green, 2 red; 12 green, 14 blue, 2 red
Game 60: 5 blue, 8 green; 1 red, 6 green, 7 blue; 1 blue
Game 61: 5 red, 2 blue, 5 green; 2 blue, 11 green; 1 blue, 2 red, 14 green; 3 green; 4 red, 13 green; 2 blue, 6 green, 1 red
Game 62: 1 red, 1 blue, 2 green; 3 red, 1 blue, 2 green; 1 blue, 10 red; 6 red, 1 blue
Game 63: 7 red, 6 blue, 4 green; 2 blue, 5 green, 8 red; 5 blue, 4 green, 10 red; 4 blue, 7 red, 10 green; 5 blue, 10 green, 8 red; 4 blue, 10 green, 3 red
Game 64: 6 red, 7 green, 15 blue; 8 blue, 16 green, 3 red; 11 green, 12 blue; 4 red, 17 blue, 8 green
Game 65: 2 blue, 7 green; 2 red, 8 blue; 2 green, 1 red, 5 blue; 1 green, 2 blue; 19 green, 7 blue; 2 red, 3 blue, 14 green
Game 66: 15 green, 7 blue; 9 blue, 3 green, 16 red; 1 red, 1 blue, 16 green; 18 red, 8 blue, 11 green
Game 67: 1 blue, 1 green, 2 red; 5 green, 8 red, 2 blue; 7 red, 1 blue
Game 68: 3 blue, 10 red; 13 red, 1 green; 5 blue, 5 red; 2 blue, 1 green, 16 red; 16 red, 3 blue
Game 69: 7 red, 1 blue, 3 green; 14 green, 2 blue; 3 green, 2 blue; 4 red, 1 green, 1 blue; 10 red, 14 green, 2 blue
Game 70: 10 green, 12 red; 5 red, 7 green; 1 blue, 6 red, 11 green
Game 71: 16 green, 13 red, 10 blue; 7 red, 7 blue, 15 green; 17 green, 13 red, 1 blue; 5 blue, 8 green, 11 red; 7 red, 1 blue, 15 green; 15 green, 4 blue, 2 red
Game 72: 3 blue, 3 red; 2 blue, 3 red, 1 green; 1 red, 1 blue, 3 green; 1 green, 2 blue, 3 red; 3 blue, 1 green, 1 red; 1 blue, 4 red
Game 73: 10 blue, 11 red, 5 green; 6 green, 9 blue, 4 red; 10 red, 5 green, 9 blue
Game 74: 6 green, 17 blue; 1 red, 1 blue, 11 green; 2 blue, 1 red, 3 green
Game 75: 11 red, 11 green, 3 blue; 11 red, 1 blue, 6 green; 4 green, 3 blue, 8 red
Game 76: 3 green, 3 blue, 12 red; 3 blue, 15 green, 3 red; 4 red, 15 green, 2 blue
Game 77: 13 blue, 11 red, 1 green; 3 red, 12 green, 12 blue; 7 red, 15 green, 4 blue; 5 red, 2 green, 3 blue
Game 78: 4 red, 8 blue, 2 green; 7 blue, 3 green, 7 red; 3 green, 13 blue; 3 red, 4 green
Game 79: 7 blue, 13 red, 8 green; 7 green, 15 red, 9 blue; 2 green, 8 red, 10 blue; 13 blue, 20 red, 7 green; 11 red, 2 green, 14 blue
Game 80: 12 red; 2 blue, 15 red, 3 green; 1 blue, 1 green, 2 red; 1 green; 1 green, 3 blue, 13 red; 1 green, 2 blue, 1 red
Game 81: 11 blue; 6 blue, 8 green, 4 red; 7 blue, 1 red, 1 green
Game 82: 9 blue, 1 red; 3 blue, 1 red, 3 green; 8 blue, 8 green, 2 red; 5 blue
Game 83: 7 blue, 13 red; 4 blue, 2 green, 3 red; 15 blue, 9 red, 1 green; 14 red, 1 green, 12 blue
Game 84: 8 blue, 1 green, 20 red; 9 green, 20 red, 18 blue; 16 red, 15 blue, 5 green; 15 red, 10 green, 16 blue; 11 green, 14 red, 12 blue
Game 85: 1 red, 2 blue, 9 green; 13 green, 3 blue, 5 red; 1 green, 1 red, 3 blue; 8 green, 2 blue, 1 red
Game 86: 10 red, 6 blue, 11 green; 1 red, 11 green; 7 blue, 6 red, 11 green
Game 87: 14 red, 4 blue, 4 green; 14 red, 4 blue, 7 green; 12 red, 11 green, 5 blue; 5 blue, 12 red
Game 88: 3 green, 4 blue, 11 red; 3 green, 4 blue, 3 red; 10 red, 3 green; 3 blue, 2 red, 2 green
Game 89: 4 blue, 2 red, 3 green; 3 green, 7 red, 13 blue; 1 green, 6 blue
Game 90: 7 red; 5 blue, 11 red, 8 green; 8 red, 3 green, 2 blue
Game 91: 2 blue; 4 red; 2 blue, 4 green; 3 green, 1 blue, 1 red
Game 92: 7 blue, 10 green; 9 green, 9 blue, 7 red; 6 green; 12 red, 1 blue, 4 green; 5 red, 1 green, 13 blue
Game 93: 7 green, 6 red, 1 blue; 3 red, 6 green, 8 blue; 1 blue, 6 green; 6 red, 15 blue, 4 green; 10 blue, 2 green, 6 red; 3 green, 5 red, 6 blue
Game 94: 5 red, 1 green, 15 blue; 1 blue, 6 red; 2 red, 6 blue, 2 green
Game 95: 9 blue, 4 red, 17 green; 15 green, 9 red, 10 blue; 1 blue, 13 green, 12 red
Game 96: 1 blue, 12 green, 1 red; 3 blue, 1 green, 5 red; 2 blue, 8 red, 10 green
Game 97: 18 blue, 7 red, 11 green; 6 red, 3 blue, 14 green; 1 red, 13 blue, 4 green
Game 98: 5 blue, 2 green, 8 red; 12 red; 13 red, 4 blue, 4 green; 7 red, 11 blue; 10 blue, 2 green, 2 red; 6 red, 12 blue
Game 99: 4 green, 2 blue, 4 red; 9 blue, 11 red, 1 green; 5 green
Game 100: 2 blue, 12 green; 6 green, 1 red, 12 blue; 1 green, 5 blue, 1 red; 1 red, 12 green, 6 blue; 16 blue, 3 green

56
day2/solution.py Normal file
View File

@ -0,0 +1,56 @@
from dataclasses import dataclass
@dataclass
class CubeSet:
red: int
green: int
blue: int
@classmethod
def from_str(cls, s: str):
data = dict([
(colour, int(count))
for colour, count in (x.strip().split(" ")[::-1] for x in s.split(","))
])
return cls(
red = data.get("red", 0),
green = data.get("green", 0),
blue = data.get("blue", 0)
)
def power(self):
return self.red * self.green * self.blue
@dataclass
class Game:
no: int
cube_sets: list[CubeSet]
@classmethod
def from_str(cls, s: str):
game, cube_sets = [x.strip() for x in s.split(":")]
return cls(
no = int(game.split(" ")[1]),
cube_sets = [CubeSet.from_str(x.strip()) for x in cube_sets.split(";")]
)
if __name__ == "__main__":
with open("input.txt") as f:
games = [Game.from_str(s) for s in f.read().split("\n")]
print("Part 1:", sum([
g.no
for g in games
if all([
cs.red <= 12 and cs.green <= 13 and cs.blue <= 14
for cs in g.cube_sets
])
]))
print("Part 2:", sum([
CubeSet(
red = max([cs.red for cs in g.cube_sets]),
green = max([cs.green for cs in g.cube_sets]),
blue = max([cs.blue for cs in g.cube_sets]),
).power()
for g in games
]))