Compare commits

...

2 commits

Author SHA1 Message Date
4dcc750416 Further optimise day 4 2023-12-05 14:19:16 +01:00
2194015259 Add day 2 2023-12-05 14:12:56 +01:00
3 changed files with 60 additions and 20 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.in
*.ex*

35
day02.py Normal file
View file

@ -0,0 +1,35 @@
import fileinput
from functools import reduce
def main():
part1 = 0
part2 = 0
for i, line in enumerate(fileinput.input()):
line = line.strip()
subsets = line.split(": ")[1].split("; ")
maxes = {}
part1_possible = True
for subset in subsets:
for balls in subset.split(", "):
count, color = balls.split()
count = int(count)
maxes[color] = max(maxes.get(color, 0), count)
part1_possible = (
part1_possible
and (color != "red" or count <= 12)
and (color != "green" or count <= 13)
and (color != "blue" or count <= 14)
)
if part1_possible:
part1 += i + 1
power = reduce(lambda x, y: x * y, maxes.values())
part2 += power
print(part1, part2)
main()

View file

@ -1,13 +1,14 @@
import fileinput import fileinput
def main():
# #
# Part 1 # Part 1
# #
cards = [] cards = []
part1 = 0 part1 = 0
for i, line in enumerate(fileinput.input()): for line in fileinput.input():
card = line.rstrip().split(": ")[1] card = line.split(": ", 1)[1]
(winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | ")) (winning, owned) = ({int(i) for i in h.split()} for h in card.split(" | ", 1))
count = len(winning & owned) count = len(winning & owned)
cards.append(count) cards.append(count)
part1 += 1 << count - 1 if count else 0 part1 += 1 << count - 1 if count else 0
@ -22,3 +23,5 @@ for i in range(len(cards)):
part2 += dp[i] part2 += dp[i]
print(part1, part2) print(part1, part2)
main()