Skip to content

Commit 38d5a6a

Browse files
committed
Tidy Everybody.Codes Story 3 Quest 1
1 parent 173fa78 commit 38d5a6a

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

everybody_codes/story03/quest_01.py

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,50 @@
55
from lib import helpers
66
from lib import parsers
77

8-
log = logging.info
98
TR = str.maketrans({c: "0" for c in "srgb"} | {c: "1" for c in "SRGB"})
109

1110

12-
def to_val(color):
13-
return int(color.translate(TR), 2)
14-
1511
def solve(part: int, data: str) -> int:
1612
"""Solve the parts."""
17-
out = 0
18-
numbers = []
19-
for line in data.splitlines():
13+
scales = []
14+
for line in data:
2015
if ":" not in line:
2116
print(line)
22-
lid, rest = line.split(":")
23-
colors = [to_val(i) for i in rest.split()]
24-
numbers.append([int(lid)] + colors)
17+
scale_id, raw_colors = line.split(":")
18+
colors = [int(color.translate(TR), 2) for color in raw_colors.split()]
19+
scales.append([int(scale_id)] + colors)
20+
2521
if part == 1:
2622
return sum(
27-
lid
28-
for lid, *colors in numbers
29-
if colors[1] > colors[0] and colors[1] > colors[2]
23+
scale_id
24+
for scale_id, red, green, blue in scales
25+
if green > red and green > blue
3026
)
27+
3128
if part == 2:
32-
want_shine = max(n[4] for n in numbers)
33-
color = min(sum(n[1:4]) for n in numbers if n[4] == want_shine)
34-
return sum(n[0] for n in numbers if sum(n[1:4]) == color and n[4] == want_shine)
29+
want_shine = max(shine for *_, shine in scales)
30+
color = min(
31+
sum(colors)
32+
for _, *colors, shine in scales
33+
if shine == want_shine
34+
)
35+
return sum(
36+
scale_id
37+
for scale_id, *colors, shine in scales
38+
if sum(colors) == color
39+
and shine == want_shine
40+
)
3541

3642
groups = collections.defaultdict(set)
37-
for lid, *colors, shine in numbers:
43+
for scale_id, *colors, shine in scales:
3844
highest = max(colors)
39-
if 30 < shine < 33 or colors.count(highest) != 1:
40-
continue
41-
groups[shine < 33, colors.index(highest)].add(lid)
45+
if colors.count(highest) == 1 and (shine <= 30 or shine >= 33):
46+
groups[shine < 33, colors.index(highest)].add(scale_id)
4247
largest_group = max(groups, key=lambda x: len(groups[x]))
4348
return sum(groups[largest_group])
4449

45-
46-
4750

48-
PARSER = parsers.parse_one_str
51+
PARSER = parsers.parse_one_str_per_line
4952
TEST_DATA = [
5053
"""\
5154
2456:rrrrrr ggGgGG bbbbBB

0 commit comments

Comments
 (0)