Advent of Code 2023 - Day 2

preview_player
Показать описание
Placed 25 on part 1; 18th on part 2; 4th overall.
Рекомендации по теме
Комментарии
Автор

ugh you make it look so easy :( I'm glad Im on the right track though.

spicywener
Автор

excellent, congrats!
first I've put some nested fors like you
later refactored it to more FP style
so it looks like:

PlayedSet = namedtuple('PlayedSet', ['red', 'green', 'blue'])
Game = namedtuple('Game', ['id', 'sets'])

def solve1(lines):
games = list(map(to_game, lines))
passed = list(filter(passes, games))
res = sum(list(map(lambda g: g.id, passed)))
return res

def solve2(lines):
games = list(map(to_game, lines))
max_sets = list(map(to_max_played_set, games))
res = sum(list(map(to_power, max_sets)))
return res

rastislavsvoboda
Автор

I had the exact same solution for part one but you did it 1000x faster than me. I love it

jonphinguyen
Автор

You have some secret ability to always avoid the edge cases without much thinking :D
I initialized the max values with 0s:
And had to update the 0 max values from missing colors to ones for the multiplication

MKRCald
Автор

You would gain time if you used tmux or the shortcut to split the windows terminal instead of stopping vim each time.

Also instead of stopping your vim process and losing the whole history of changes, using ctrl z and then foreground could improve that.

zvl
Автор

Oh I did this:

```py
from math import prod
import re
from collections import Counter

import aoc_lube

def parse_raw(raw_data):
return [
Counter({color: int(n) for n, color in re.findall(r"(\d+) (\w+)", line)})
for line in raw_data.splitlines()
]

DATA = parse_raw(aoc_lube.fetch(year=2023, day=2))

def part_one():
max_cubes = Counter({"red": 12, "green": 13, "blue": 14})
return sum(i for i, cubes in enumerate(DATA, start=1) if all(cubes[color] <= max_cubes[color] for color in cubes))

def part_two():
return sum(prod(cubes.values()) for cubes in DATA)

aoc_lube.submit(year=2023, day=2, part=1, solution=part_one)
aoc_lube.submit(year=2023, day=2, part=2, solution=part_two)
```

aidan-
Автор

Check out my one-liners.

Part 1, 121 bytes:
import re
i, c in re.findall("(\d+) (.)", l))<1)*j for j, l in enumerate(open("i"), 1)))

Part 2: 107 bytes:
import re, math
print(sum(math.prod(max(map(int, re.findall("(\d+) "+c, l)))for c in"rgb")for l in open("i")))

squeezy
Автор

every time a finish a day i come here to see how much better i could have done lol

lennyb.