|
1 | 1 | """Day 4 - Title Goes Here""" |
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 | 5 |
|
| 6 | +LooRollGrid = list[list[str]] |
6 | 7 |
|
7 | | -EXAMPLE: list = [] |
8 | | -DATA = input_for_day(4) |
| 8 | +ORIGINAL_EXAMPLE: str = """..@@.@@@@. |
| 9 | +@@@.@.@.@@ |
| 10 | +@@@@@.@.@@ |
| 11 | +@.@@@@..@. |
| 12 | +@@.@@@@.@@ |
| 13 | +.@@@@@@@.@ |
| 14 | +.@.@.@.@@@ |
| 15 | +@.@@@.@@@@ |
| 16 | +.@@@@@@@@. |
| 17 | +@.@.@@@.@.""" |
9 | 18 |
|
| 19 | +EXAMPLE: LooRollGrid = [list(x) for x in ORIGINAL_EXAMPLE.splitlines()] |
| 20 | +INPUT_DATA: list[str] = input_for_day(4, 2025, ff="lines") |
| 21 | +DATA: LooRollGrid = [list(x) for x in INPUT_DATA] |
10 | 22 |
|
11 | | -def helperfunction(data: Any) -> None: |
12 | | - return None |
| 23 | + |
| 24 | +def parse_loogrid_p1(data: LooRollGrid) -> int: |
| 25 | + """Navigate through the Loo Roll Grid for AOCD4P1. |
| 26 | +
|
| 27 | + Insert an extra layer of blank padding, slide a |
| 28 | + 3x3 window and count adjacent loo rolls. |
| 29 | + If less than 4 adjacent loo rolls, count it! |
| 30 | +
|
| 31 | + Args: |
| 32 | + data (LooRollGrid): List of lists, @ = loo roll. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + int: Count of valid positions. |
| 36 | + """ |
| 37 | + forklift_access: int = 0 |
| 38 | + # add an extra padding 0 and -1 |
| 39 | + for row in data: |
| 40 | + row.insert(0, ".") |
| 41 | + row.insert(len(data[0]), ".") |
| 42 | + |
| 43 | + # add an extra layer of padding... |
| 44 | + data.insert(0, list("." * len(data[0]))) |
| 45 | + data.insert(len(data), list("." * len(data[0]))) |
| 46 | + |
| 47 | + # trundle through the grid |
| 48 | + for row_idx, row in enumerate(data[1:-1], 1): |
| 49 | + # print(ri, row[1:-1]) |
| 50 | + for col_idx, col in enumerate(row[1:-1], 1): |
| 51 | + if "@" in col: |
| 52 | + # grab a 3x3 window |
| 53 | + window: LooRollGrid = [ |
| 54 | + row[col_idx - 1:col_idx + 2] |
| 55 | + for row in data[row_idx - 1:row_idx + 2] |
| 56 | + ] |
| 57 | + # smoosh it together |
| 58 | + join_window: str = "".join("".join(row) for row in window) |
| 59 | + # deduct the initial @ |
| 60 | + num_rolls: int = join_window.count("@") - 1 |
| 61 | + if num_rolls < 4: |
| 62 | + forklift_access += 1 |
| 63 | + return forklift_access |
13 | 64 |
|
14 | 65 |
|
15 | 66 | @report_results |
16 | 67 | def solveday(data: Any) -> AoCResult: |
17 | | - p1: int = 0 |
| 68 | + p1: int = parse_loogrid_p1(data) |
18 | 69 | p2: int = 0 |
19 | 70 | return p1, p2 |
20 | 71 |
|
21 | 72 |
|
22 | | -expected_test_results: AoCResult = (0, 0) |
| 73 | +expected_test_results: AoCResult = (13, 0) |
23 | 74 |
|
24 | 75 |
|
25 | 76 | def tests(test_input: Any) -> None: |
|
0 commit comments