|
5 | 5 | from lib import helpers |
6 | 6 | from lib import parsers |
7 | 7 |
|
8 | | -log = logging.info |
9 | 8 | TR = str.maketrans({c: "0" for c in "srgb"} | {c: "1" for c in "SRGB"}) |
10 | 9 |
|
11 | 10 |
|
12 | | -def to_val(color): |
13 | | - return int(color.translate(TR), 2) |
14 | | - |
15 | 11 | def solve(part: int, data: str) -> int: |
16 | 12 | """Solve the parts.""" |
17 | | - out = 0 |
18 | | - numbers = [] |
19 | | - for line in data.splitlines(): |
| 13 | + scales = [] |
| 14 | + for line in data: |
20 | 15 | if ":" not in line: |
21 | 16 | 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 | + |
25 | 21 | if part == 1: |
26 | 22 | 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 |
30 | 26 | ) |
| 27 | + |
31 | 28 | 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 | + ) |
35 | 41 |
|
36 | 42 | groups = collections.defaultdict(set) |
37 | | - for lid, *colors, shine in numbers: |
| 43 | + for scale_id, *colors, shine in scales: |
38 | 44 | 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) |
42 | 47 | largest_group = max(groups, key=lambda x: len(groups[x])) |
43 | 48 | return sum(groups[largest_group]) |
44 | 49 |
|
45 | | - |
46 | | - |
47 | 50 |
|
48 | | -PARSER = parsers.parse_one_str |
| 51 | +PARSER = parsers.parse_one_str_per_line |
49 | 52 | TEST_DATA = [ |
50 | 53 | """\ |
51 | 54 | 2456:rrrrrr ggGgGG bbbbBB |
|
0 commit comments