|
1 | | -"""Day 2 - Title Goes Here""" |
| 1 | +"""Day 2 - Gift Shop""" |
2 | 2 |
|
3 | 3 | from typing import Any |
4 | | -from utils.aoc_utils import input_for_day, report_results, AoCResult |
| 4 | +from utils.aoc_utils import report_results, AoCResult, input_for_day |
| 5 | +from itertools import batched |
| 6 | +from rich.progress import track |
5 | 7 |
|
| 8 | +EXAMPLE: list[str] = [ |
| 9 | + "11-22", |
| 10 | + "95-115", |
| 11 | + "998-1012", |
| 12 | + "1188511880-1188511890", |
| 13 | + "222220-222224", |
| 14 | + "1698522-1698528", |
| 15 | + "446443-446449", |
| 16 | + "38593856-38593862", |
| 17 | + "565653-565659", |
| 18 | + "824824821-824824827", |
| 19 | + "2121212118-2121212124", |
| 20 | +] |
| 21 | +INPUT_DATA: str = input_for_day(2, 2025) |
| 22 | +DATA: list[str] = INPUT_DATA.split(",") |
6 | 23 |
|
7 | | -EXAMPLE: list = [] |
8 | | -DATA = input_for_day(2) |
9 | 24 |
|
| 25 | +def part2_wrangle(id_str: str) -> int: |
| 26 | + """Part 2 wrangle. |
| 27 | + Find chunks of string id to make it even chunks. |
| 28 | + If a len(set) of those chunks == 1 - invalid id! |
10 | 29 |
|
11 | | -def helperfunction(data: Any) -> None: |
12 | | - return None |
| 30 | + Args: |
| 31 | + id_str (str): ID String |
| 32 | +
|
| 33 | + Returns: |
| 34 | + int: either the invalid id int or 0 |
| 35 | + """ |
| 36 | + # find each divisor |
| 37 | + id_len: int = len(id_str) |
| 38 | + divisors: list[int] = [x for x in range(1, id_len) if id_len % x == 0] |
| 39 | + |
| 40 | + for d in divisors: |
| 41 | + if id_len / d >= 2: |
| 42 | + b: list[tuple[str, ...]] = list(batched(id_str, d)) |
| 43 | + if len(set(b)) == 1: |
| 44 | + return int(id_str) |
| 45 | + return 0 |
| 46 | + |
| 47 | + |
| 48 | +def id_wrangle(data: list[str]) -> AoCResult: |
| 49 | + """Solution for Day02 AOC25. |
| 50 | + Part 1 - looks for ID's that are just repeats. |
| 51 | + Part 2 - TBA. |
| 52 | +
|
| 53 | + Args: |
| 54 | + data (list[str]): Input data. |
| 55 | +
|
| 56 | + Returns: |
| 57 | + AoCResult: part1 invalid ids, part 2 invalid ids |
| 58 | + """ |
| 59 | + invalid_ids_p1: int = 0 |
| 60 | + invalid_ids_p2: int = 0 |
| 61 | + |
| 62 | + for x in track(data): |
| 63 | + lower_id, upper_id = map(int, x.split("-")) |
| 64 | + id_range: range = range(lower_id, upper_id + 1) |
| 65 | + |
| 66 | + for j in id_range: |
| 67 | + id_str: str = str(j) |
| 68 | + invalid_ids_p2 += part2_wrangle(id_str) |
| 69 | + # print(list(batched(id_str, 2))) |
| 70 | + if len(id_str) % 2 == 0: |
| 71 | + split_id_str1, split_id_str2 = list(batched(id_str, len(id_str) // 2)) |
| 72 | + # print(split_id_str1, split_id_str2) |
| 73 | + if split_id_str1 == split_id_str2: |
| 74 | + # print(id_str, ' = INVALID ID', list(batched(id_str, 2)) |
| 75 | + invalid_ids_p1 += int(id_str) |
| 76 | + |
| 77 | + return invalid_ids_p1, invalid_ids_p2 |
13 | 78 |
|
14 | 79 |
|
15 | 80 | @report_results |
16 | | -def solveday(data: Any) -> AoCResult: |
17 | | - p1: int = 0 |
18 | | - p2: int = 0 |
| 81 | +def solveday(data: list[str]) -> AoCResult: |
| 82 | + p1, p2 = id_wrangle(data) |
19 | 83 | return p1, p2 |
20 | 84 |
|
21 | 85 |
|
22 | | -expected_test_results: AoCResult = (0, 0) |
| 86 | +expected_test_results: AoCResult = (1227775554, 4174379265) |
23 | 87 |
|
24 | 88 |
|
25 | 89 | def tests(test_input: Any) -> None: |
|
0 commit comments