Skip to content

Commit 157555c

Browse files
Clean up 2023 day 2
1 parent a1c58ed commit 157555c

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

2023/python/aoc_2023/day02.py

+10-13
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,22 @@ def parse_game(line: str) -> Game:
2424
return int(id.split(" ")[-1]), [parse_rgb(x) for x in subsets.split("; ")]
2525

2626

27-
with open("2023/input/02.txt", encoding="utf-8") as f:
28-
input = [parse_game(line.strip()) for line in f.readlines()]
29-
30-
possible_games = [
31-
game
32-
for game in input
33-
if all(r <= 12 and g <= 13 and b <= 14 for r, g, b in game[1])
34-
]
35-
36-
print("Part one:", sum(game[0] for game in possible_games))
27+
def is_possible(game: Game) -> bool:
28+
return all(r <= 12 and g <= 13 and b <= 14 for r, g, b in game[1])
3729

3830

3931
def power(game: Game) -> int:
40-
r_max, g_max, b_max = game[1][0]
41-
for r, g, b in game[1][1:]:
32+
r_max, g_max, b_max = 0, 0, 0
33+
for r, g, b in game[1]:
4234
r_max = max(r_max, r)
4335
g_max = max(g_max, g)
4436
b_max = max(b_max, b)
4537
return r_max * g_max * b_max
4638

4739

48-
print("Part two:", sum(power(game) for game in input))
40+
with open("2023/input/02.txt", encoding="utf-8") as f:
41+
input = f.readlines()
42+
43+
games = [parse_game(line.strip()) for line in input]
44+
print("Part one:", sum(game[0] for game in games if is_possible(game)))
45+
print("Part two:", sum(power(game) for game in games))

0 commit comments

Comments
 (0)