2023.02
This commit is contained in:
parent
cb0c8c2845
commit
d78038b6ab
4 changed files with 100 additions and 0 deletions
1
challenges/2023/02-cubeConundrum/README.md
Normal file
1
challenges/2023/02-cubeConundrum/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 2: Cube Conundrum](https://adventofcode.com/2023/day/2)
|
90
challenges/2023/02-cubeConundrum/main.py
Normal file
90
challenges/2023/02-cubeConundrum/main.py
Normal file
|
@ -0,0 +1,90 @@
|
|||
import os
|
||||
import sys
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass(init=False)
|
||||
class Game:
|
||||
id: int
|
||||
hands: list[dict[str, int]]
|
||||
|
||||
def __init__(self, id: int):
|
||||
self.id = id
|
||||
self.hands = []
|
||||
|
||||
|
||||
def parse(inp: str) -> list[Game]:
|
||||
res = []
|
||||
for line in inp.splitlines():
|
||||
game_decl, game_hands = line.split(": ")
|
||||
|
||||
game = Game(int(game_decl.lstrip("Game ")))
|
||||
|
||||
for hand in game_hands.split(";"):
|
||||
hand = hand.strip()
|
||||
counts = {}
|
||||
for part in hand.split(", "):
|
||||
n, colour = part.split(" ")
|
||||
counts[colour] = int(n)
|
||||
game.hands.append(counts)
|
||||
|
||||
res.append(game)
|
||||
|
||||
return res
|
||||
|
||||
|
||||
COLOUR_MAXVALS = {"red": 12, "green": 13, "blue": 14}
|
||||
|
||||
|
||||
def one(inp: str):
|
||||
games = parse(inp)
|
||||
|
||||
acc = 0
|
||||
|
||||
for game in games:
|
||||
ok = True
|
||||
|
||||
for hand in game.hands:
|
||||
for key in COLOUR_MAXVALS:
|
||||
if COLOUR_MAXVALS[key] < hand.get(key, 0):
|
||||
ok = False
|
||||
break
|
||||
|
||||
if not ok:
|
||||
break
|
||||
|
||||
if ok:
|
||||
acc += game.id
|
||||
|
||||
return acc
|
||||
|
||||
|
||||
def two(inp: str):
|
||||
games = parse(inp)
|
||||
acc = 0
|
||||
|
||||
for game in games:
|
||||
x = 1
|
||||
|
||||
for colour in COLOUR_MAXVALS.keys():
|
||||
x *= max(map(lambda x: x.get(colour, 0), game.hands))
|
||||
|
||||
acc += x
|
||||
|
||||
return acc
|
||||
|
||||
|
||||
def _debug(*args, **kwargs):
|
||||
kwargs["file"] = sys.stderr
|
||||
print(*args, **kwargs)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) < 2 or sys.argv[1] not in ["1", "2"]:
|
||||
print("Missing day argument", file=sys.stderr)
|
||||
os.exit(1)
|
||||
inp = sys.stdin.read().strip()
|
||||
if sys.argv[1] == "1":
|
||||
print(one(inp))
|
||||
else:
|
||||
print(two(inp))
|
8
challenges/2023/02-cubeConundrum/tests.json
Normal file
8
challenges/2023/02-cubeConundrum/tests.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"1": [
|
||||
{"in": "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green\nGame 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue\nGame 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red\nGame 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red\nGame 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", "res": "8"}
|
||||
],
|
||||
"2": [
|
||||
{"in": "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green\nGame 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue\nGame 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red\nGame 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red\nGame 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green", "res": "2286"}
|
||||
]
|
||||
}
|
|
@ -11,3 +11,4 @@ Solutions to the [2023 Advent of Code](https://adventofcode.com/2023).
|
|||
| Day | Status | Solutions | Notes |
|
||||
|-----|--------|-----------|-------|
|
||||
| 01 - Trebuchet?! | ★ ★ | Python | I never knew detecting numbers could be so confusingly tricky |
|
||||
| 01 - Cube Conundrum | ★ ★ | Python | Pleasingly straightforwards, though seems like it would be well suited to Haskell |
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue