From f32db19af56e65f05a8b353c4f69639db68226cc Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Thu, 4 Dec 2025 08:50:36 +0100 Subject: [PATCH] feat(day_4): both exercises done --- README.md | 11 +- src/__tests__/day_4_test.ts | 22 ++++ src/exercises/assets/day_4_input.txt | 139 ++++++++++++++++++++++ src/exercises/assets/day_4_test_input.txt | 10 ++ src/exercises/day_4.ts | 115 ++++++++++++++++++ 5 files changed, 292 insertions(+), 5 deletions(-) create mode 100644 src/__tests__/day_4_test.ts create mode 100644 src/exercises/assets/day_4_input.txt create mode 100644 src/exercises/assets/day_4_test_input.txt create mode 100644 src/exercises/day_4.ts diff --git a/README.md b/README.md index c8b89f8..c78c3b0 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,9 @@ [Advent of Code](https://adventofcode.com/) problems of 2025. -| Day | Name | Part 1 | Part 2 | -| :-: | :-------------- | :----: | :----: | -| 1 | Secret Entrance | ⭐ | ⭐ | -| 2 | Gift Shop | ⭐ | ⭐ | -| 3 | Lobby | ⭐ | ⭐ | +| Day | Name | Part 1 | Part 2 | +| :-: | :-------------------- | :----: | :----: | +| 1 | Secret Entrance | ⭐ | ⭐ | +| 2 | Gift Shop | ⭐ | ⭐ | +| 3 | Lobby | ⭐ | ⭐ | +| 4 | Printing Department | ⭐ | ⭐ | diff --git a/src/__tests__/day_4_test.ts b/src/__tests__/day_4_test.ts new file mode 100644 index 0000000..e5c6790 --- /dev/null +++ b/src/__tests__/day_4_test.ts @@ -0,0 +1,22 @@ +import { assertEquals } from "@std/assert"; +import { + count_accessible_rolls, + count_accessible_rolls_with_remove, + read_rolls_grid, +} from "../exercises/day_4.ts"; + +Deno.test("Day 4 - A", async () => { + const grid = await read_rolls_grid( + "src/exercises/assets/day_4_test_input.txt", + ); + const accessible_rolls = count_accessible_rolls(grid); + assertEquals(accessible_rolls, 13); +}); + +Deno.test("Day 4 - B", async () => { + const grid = await read_rolls_grid( + "src/exercises/assets/day_4_test_input.txt", + ); + const accessible_rolls = count_accessible_rolls_with_remove(grid); + assertEquals(accessible_rolls, 43); +}); diff --git a/src/exercises/assets/day_4_input.txt b/src/exercises/assets/day_4_input.txt new file mode 100644 index 0000000..d3b1be9 --- /dev/null +++ b/src/exercises/assets/day_4_input.txt @@ -0,0 +1,139 @@ +@..@@.@..@@@.@@@@@@.@@@@@@@@.@@@.@@@.@.@@@.@....@@@.@...@@.@..@@@@@.@@@.@@...@@.@@@.@@@@.@@@@.@.@@@@..@@@@@@.@.@.@.@@@@.@@.@@.@@@@..@.@.@@@ +.@..@@@@@@@.@@@@.@@@@..@@.@.@..@@@.@@@@@@...@@.@@.@...@@@..@.@@@@......@@@@@@@.@@.@.@@@@..@....@.@..@@.@.@@@.@.@@@.@@@@.@@.@.@@..@@@@.@@.@@ +@@@..@@@@@@@@@@@.@..@.@@@@@@@@@....@.@.@@@@@.@....@@@@@@.@@@@@@@@@@@@@.@.@@...@@@@@..@@@@@.@..@@@@.@@@@.@@@@@@@@.@@@@@@@@@@@@@@@@.@@@@@@@.@ +@@@@@@@.@@@.@.@@@.........@.@@@.@@@@@@.@.@@@@@@@@..@@@.@@@@@@.@.@.@.@.@@@@@@@@.@@@@@@.@...@@@.@@.@@.@@@@@@@.@@.@.@.@@@@.@.@@@.@@@.@@.@.@@@. +..@@..@@@.@@@@@@@.@.@.@@.@@..@@...@@@@@.@@@@@.@@.@@@.@@.@@@@@.@@.......@@..@@..@.@@.@@.@@@.@.@@@@.@..@@@.@@@@@.@@....@@@@@@.@@@@.@@.@@@.@.@ +@@@@@@......@@@..@@.@@.@...@.@.@@@@.@.@.@...@@@@.@.@@@@@@@@@@@@...@....@@..@@.@@@@@@@@@..@@..@@@.@@@@@@@@@.@@@.@@@@@.@@@.@@@.@@@@@.@.@..... +.@@@@.@...@@@....@@...@@@@@@@@.@@....@.@.@.@@@@@@@@@@@.@@.....@@@@@@@@@@@.@.@@@@@@@@@@@@.@@@.@@@@@.@@@@@@@..@.@@....@@@..@@.@.@.@.@.@.@@@@. +@@.@@.@@@.@@@@.@..@...@.@@@.@.@@@@..@..@.@@@.@@@@.@...@@@@@@.@.@@@...@@.@@@@@@@@@..@.@@@..@@@@@@@@@@.@@@.@@@@@@...@.@@.@@@.@@...@@@.@.@.@@@ +....@.@..@@@@@.@@@@@@@@@@..@@..@@@@.@@.@.@.@@.@@@.@@.@@@@@@.@@@@@.@@.@@@@@..@@.@.@@@..@@@@.@@@@.@@@@..@@@@@@@@..@..@.@.@.@@@.@.@@@@..@@@@.@ +@@@.@@@@.@.@@@.@..@.@.@@..@@@.@.@.@@@@@@.@..@.@@.@...@@.@.@@...@@@...@@@.@@@@@..@.@@@.@@@@@@@@..@@@@@@@@.@@@@.@@@@@..@@.@@..@@.@@@@..@.@@.@ +@@@@.@@@.@..@@@@.@@....@....@@@@@@@@@@@@.@@@.@@@@@.@@.@..@@..@.@..@..@@..@@@@@..@@.@@@@@@@@@@@..@@@@@.@.@@@@@@@@@@@..@@@@..@@@.@@..@...@@@@ +@.@@@@..@@@@@.@......@@@@@@.@@@..@@@@..@@.@@@.@@@@@@.@@..@@.@@@@@@@@@@@@..@.@@@...@@@..@@.@.@@@@.@.@@@@.@.@..@@@.@..@@.@.@@@@@@.@@@@.@.@..@ +@@@@@.@..@@.@..@@@@@.@@..@@@@...@@.@.@.@.@..@..@@.@@@..@...@@..@@@.@.@@..@@.@@.@@.@@.@.@@.@@.@.@@.@.....@@.@@..@......@@@@@.@@.@@@@@@@.@.@@ +@@@@..@@@@.@.@......@@.@.@.@@..@.@@@@@...@@..@@..@@@@@@.@@@.@@..@..@@.@@@.@@@@.@@@@@@.@.@@.....@.@@@.@@@......@@@...@@..@@@..@.@@@.@@@@.@@@ +.@.@@@.@.@...@@@.@@.@@@@@@@...@@@@@...@@@.@@.@@@@..@@.@@..@@@..@@@.@@@@@@@@@..@...@@..@.@@@@@@@@@.@@@@..@.@@..@@.@@@.@.@@.@.@@@@@...@.@@.@@ +@.@.@@..@@.@.@@@@..@@@@.@.@@@@@@@@@@@..@@.@@.@@..@@..@.@@@@@@@.@..@@@@@.@@@@@.@@@@@@@@@@@..@.@@@@@@.@@@.@@@@.@@@@@@.@@@.@..@...@@@.@@@.@@.@ +@.@@@.@@...@.@@@@@..@@..@@@@@..@@@@@@..@...@@@@@@@@@@@@@@@.@..@.@@@@@@..@@@...@@.@@@@.@.@@.@.@..@@@....@.@@.@@@@@@..@.@.@..@@..@@@@@.@@@.@@ +.@@@@@@@.@@@..@......@@@@@@@@@@..@.@@.@@.@@@.@@.@@@@@@@.@@@@@.@@.@..@@@@@@@@..@@.@.@..@.@@@.@@@@@@@.@.@@.@@@....@@.@...@.@@@.@@@...@@@@@... +@.@@@@@.@@@@...@@@@@.@.@@@@.@@..@.@@@.@.@..@@@..@@@..@@@.@@.@@@@@.@@@@@@@@@@.@.@@@@..@@@.@..@.@@.@..@@@.@@@.@@@@@@@@.@@@..@.@.@...@@...@@@@ +@.@.@@.@..@..@@@@@.@@@@@@.@@@.@..@.@@.@.@@.@@.@.@@..@@@@@@@@@@.@@...@@.@@..@@@.@@@.@@@@...@@@@@@.@@.@@.@.@@@@@@.@.@@@@.@..@.@@@@@.@@.@@@.@@ +@@@....@@.@@@@@@@@@.@@@@@@@@@@@@@@.@@@@@@@@..@.@..@@@....@.@@@.@@@.@.@.@@.@@.@@@...@@@@@@@.@@@@..@.@@@...@@@@..@..@@@.@@@..@@..@@@..@@.@@@@ +..@.@..@@@@@@.@.@.@@.@@@@@@.@.@@..@@@@@@@@@@.@@@.@...@@@.....@@.@@.@.@.@@@@@@@.@..@.@@@@@@@.....@@..@@.@@@.@@@@...@.@.@@@.@@.@@@.@@.@@@..@@ +@@@@.@....@@@@@@@@@@.@@...@@@@.@@.@@@@.@.@@@.@@@@..@@@.@@@.@..@.@@.@@@@.@.@@@@@.@..@@.@@@@@.@.@@..@....@@@@@@@@@@@@@@@..@@@@.@..@@@@@@@.@@. +.@@....@@@...@.@..@....@@.@@@@..@..@@@..@@@@.@@@..@@.@@@.@..@@@..@@@@@@@...@@@@...@.@@.@@@@.@.@@@@@.@...@@.@@@@@....@..@@@@@....@@@@@.@.@@@ +@@.@@@.@@...@@.@.@..@@@..@@.@...@@@@..@@@.@..@....@@@..@@@.@@@@@@@@..@@@..@..@@.@@.@@@@....@@@..@..@@..@@@@.@.@.@@.@@@..@@.@..@@@@...@@@.@. +@.@.@@@@.@@@@@@@@..@@@@..@@..@@@.@@.@@@@@..@@@@@.@..@.@.@.@@@.@.@@.@.@.@@@.@@.@@@@@@@@..@@.@.@@@@@@@@@@.@@@@@@@.@@@@@.@@@@...@@@@@@@@..@@.@ +@@@..@@@@.@.@@.@@..@..@.@@@@.@.@..@@..@.@.@@@@@@..@.....@..@@@@@@@@@.@@..@.@@.@.@@@@@.@@@@@@@@@......@@.@...@@@@@..@@@@@@@.@.@..@@.@@@@@@@@ +@@..@@.@@@@@@@@.@@@@@.@@.@@..@@.@@.@@@@@@@@.@@.@.@@@...@@@@@@@@@.@@@.@@.@.@@.@@@@..@@@.@@@@@@@@...@@..@@@@@.@@@...@..@@..@@@..@.@@@@@@@.@.. +@.@@..@@.@@..@...@.@@@@@.@.@..@.@@@@.@.@.@@@.@.@.@@@..@@@@@@@@@@.@...@@@...@@..@@@.@@@@..@@@.@@@@@@@@@@..@@.@.@@.@..@......@...@@@.@@@@.@.@ +@@.@..@@.@.@@@.@....@@.@@@@@.@.@@@..@@@@.@@@.@....@.@@@..@@@@....@.@....@@.@.@@@@@@@@...@@@.@@@.@.@@@@..@@@@@@@@@..@@...@@@.@.@.@@@@.@.@@@@ +@@@@.@@@@...@@@@.@@@.@@@@@@@@@..@@@@..@@@....@@.@.@@@..@@.@...@@...@@.@@@@.@.@@@@.@@@@.@.@@@@@@@.@.@@...@@@@.@@@.@@@@.@.@@@.@.@@..@@@.@...@ +.@@.@@@@.@..@@@@@@@..@..@@@@@@.@..@...@@.@.@...@..@@@@@@.@@.@.@@@@@.@.@.@@..@.@@@@@@.@@..@@@.@@.@@@@@@.@@@@@@@@@@@@@@@@@@@@@.@@@@@.@...@@@. +@@..@..@..@@.@@@@@@@@@.@@@@@.@@@@.@@@@@@@..@..@@@.@@..@@.@...@@@@@..@@@@@.@..@@..@@@@@.@....@...@@@.@@@.@@@@@.@.@.@..@..@@@.@@@.@..@.@@@@@. +@@@..@@..@@@@@@@@.@@@..@@@@@@@@@@@@.@.@@.@.@@@@@..@.@@@@@.@@@@.@@@@@@@@@@@@..@@.@@@@..@@@.@@..@.@@@@.@@@@@@@@@@@@.@.@@.@..@.@.@@@..@.@.@@.. +...@@@.@@@@...@@.@.@@@@.@@..@.@..@@@.@@@@@..@@@..@@@@..@.@.@@@@@.@.@@@@@@@@@@.@@@.@@@@@@@@@.@.@.@@@@.@@..@@......@@..@@@.@@.@@.@@@@@.@.@@@@ +.@@@@...@....@@@...@@.@.@.@@@@@@@@@@.@.@..@@@@@@.@..@@@.@@@.@@.@@@@@@.@@...@.....@@@.@.@.@@@@@@..@@@@@@@@@.@@@.@...@..@@@@@.@@@...@@@.@@@.@ +@@@@.@@@...@@@.@@@@.@@@@@@@@.@@@@@....@@@@@@@@@.@.@@@@@@.@..@@@.@.@@@..@@@@@..@@.@@@@...@@@@@@....@@@@@.@@@@@@@@.@.@@@@@@@.@.@@@.@@.@.@@..@ +..@@..@@.@.@.@.....@.@@@@@@..@@@@.@@@@@.@@.@.@@@.@@@@@@@@.@@@@.@.@.@.@@@@@@@@@@...@@@@.@..@@@@@.@@.@@@.@@@@@@@@.@@@..@@@.@.....@.@..@@.@@.@ +@.@@@.@@@@@@.@@@@@@@@.@.@@@@.@..@.@@...@..@.@@..@..@.@.@@@..@@.@.@.....@@....@@@..@@@.@@.@@@@@@..@.@@..@@@.@@@@@@@@.@@@@@@@@@@.@@..@@@@@@@. +.@@@@@.@.@.@...@@@.@.@@@@@.@@@.@@@..@.@...@@@@@@@.@@@@@@@@@.@@....@@@@@.@@@@..@@@@@@.@.@.@..@.@.@.@@.@@.@@@@.@@.@.@@.@.@@.@.@..@..@@.@@.... +.@@@@@@....@.@@@@@@@@@@@@@@@..@@@@@.@.@.@.@@@@....@.@.@@.@@@@@@.@.@@@@@@@@..@..@@@..@@@.@@....@@@..@@@@...@@.@@@@@@.@.@@@@..@@@@@@.@...@.@@ +@@@@@@@@.@@@@@.@..@@...@@@..@@.@@..@..@@@@.@.@..@@@@@@.@.@@.@@.@@@@@.@@..@..@@@@@.@@@.@@..@@@@@@..@@@.@.@@@@@@..@..@@@..@.@@.@@@.@.@@..@..@ +@@.@@@@@.@@@@@@@@@@.@@.@..@@..@@@@...@@.@..@@....@@.@@.@.@@@..@.@@@@@@@@@@@@@@..@@@@@@@.@@.@@@@@..@.@@@@@@..@@@...@@@@@.@@@.@@.@.@...@@@@.. +....@@@.@.@@@@@@@@@.@.@@.@...@@@@@@.@.@..@@.@...@@@@.@@@@.@@@.@.@@..@@@@@@.@@@@..@@@@@@@@@@.@.@.@@..@.@.@@.@@@.@@@@@@@..@@.@@@@@.@.@@@.@... +@@@@@@..@.@@@@@@@@@@@@@@..@@...@@@@@@@@@@@.@.@@@.@@@@@..@.@@..@..@@@@@.@@@@@.@@..@.@@@....@@@@.@@@@@@@..@.@@@@@@@@@@@@@@@.@@.@.@.@@@@.@@@@. +@...@@..@.@@@@.@@@..@.@@.@@.@@@.@@.@..@@@..@.@@@@@@.@@@..@@@.@@@@.@@.@.@@....@.@...@@@@@@@.@@@.@@@@.@@.@@.@@.@@@@.@....@@@@@@@@@..@.@@@@.@. +@@@..@@.@.@...@...@.@..@.@@@@.@@@.@.@@@...@@@@@@@...@.....@@@.@@@@@@@@@@@..@.@...@@.@@@@.@@.@@.@..@@@@.@@@@.@@@@@.@..@@@..@@@.@@...@@..@@@@ +.@@@@@@@.@@@@.@@@@@.@@@.@@@@..@@@@@@@@@@@..@.@@@@..@@@@@@@@@..@@@.@@..@@@.@.@@@.@@@..@.@@@@@@@.@@@@@..@.@.@@@@@.@..@.@.@.@@....@@@@@@.@.@@@ +@@@@.@@.@@.@@@.@@@.@@@@@@.@@@.@@@@@@.@@.@@..@@@@@@...@.@@@.@..@@@@...@..@.@@@@@.@.@.@@@@@@@@@@..@@@.@.@@@.@@@@....@.@@..@@.@@@@@@@@@..@@@.@ +@.@.@@@@@.@@.@@@.@.@@@..@@.@@.@@@@@@@@..@@@@..@@...@.@.@@@....@.@..@@@@@@@@@@.@@....@.@@..@@@@@...@@@@@.@.@@@@..@@@.@..@@.@@.@.@@...@@@@@@@ +@@.@@@@.@@@...@@@..@.@.@@@.@@@@@.@@@.@.@.@.@@...@@.@.@@@@.@..@@..@@.@@.@.@.@..@..@@@@.@.@@@@@.@.@..@@@@@@@.@@.@@@....@.@..@@..@@...@@@.@.@@ +@@.@@.@@@@@@.@@.@..@..@...@@@.@@..@.@@@..@@.@.@.@@.@@@..@@@.@@..@@@@@@@..@.@@.@.@@@@.@.@.@@.@@@.@@@.@@.@@@@.@@@@@...@@@@@@@@@.@@@@@.@@..@.. +@.@@@.@@@@@@@.@.@.@@@@@.@@@@@.@@@@@@@.@@.@.@.@@@@@@@@@@@...@..@@.@@.@@@@.@@@.@@@@...@@@@@@.@@@@@@@.@@@.@@@@@....@..@@@.@@..@.@@..@..@@.@@@@ +@.@@@@@..@@@..@@@@...@@@.@@@...@@.@@..@..@@..@@.@@@..@@@@@@.@@@.@@...@.@.@..@.@@@@.@@..@.@@@@.@@.@@@@@.@@@@.@.@@.@.@@@@.@@.@.@..@...@@@@.@. +@@@..@.@.@......@..@...@...@.@@@@@@@@.@@.@@@.@@@.@..@.@@.@.@@.@.@@@.@.@@..@.@.@.@.@.@.@.@@@@@@@@.@.@@@@@@@@@@@@@@.@@@@@@@@..@@..@..@@@@@@.@ +@@@@.@..@.@.....@@.@@.@@.@@..@@@@...@.@@@@@@.@@..@@@.@....@.@@.@....@....@@@.@@@@@@@..@@.@@..@.@@@.@@@@@.@.@@@@@.@.@@@@...@.@@@@@@@@@@@.@@@ +.@.@..@.@@.@@@@@.@@@..@@@.@.@@@..@@@@@@@.@@@.@@.@@@..@@@@@.@.@.@@.@.@@@@.@..@@@.@@@...@@@@.@@@..@@.@....@.@@@@@..@@....@..@@@.@.@@.@.@.@@.@ +..@.@@@@@@.@@@.@@@.@@@@@@@@@@@@@@@...@@.....@@@@@@@.@..@@@@@@@@....@@@@..@@.@@@@@.@@.@.@@@@@.@.@@@@.@@@@@@...@@@.@.@.@.@@@@.@@@.@..@.@@...@ +@.@.@.@@.@@@@.@@@@@.@.@...@@@@..@@@@@...@..@@@@@@..@@.@@.@@@@...@@@@.@@@...@@.@@..@@.@.@@@@@@@@...@@@@@..@@.@.@@@.@@@..@@...@@@@@@@@@@.@@.@ +@@..@@.@.@@@@@@@.@@@@..@.@@.......@@..@@@@@.@@@@..@@@@..@.@.@.@@.@@@.@@@@@.@@@.@@@@@@@.@@@@@@@@.@@@@.@@.@.@@...@..@@@@.@.@@@@.@@..@@@..@@@@ +@@.....@.@.@@@@@.@@..@@@.@@@@.@..@@@@@...@@@.@.@@@.@.@@@@@@.@.@..@.@.@@.@.@@@@@@@@.@.@@@.@..@@.@.@..@.@@@@..@@@@@@@@..@@@@@...@@@@@.@@.@@@. +@@@@@@@@....@@@.@.@@.@@.@..@@@@@@......@@@.@.@.@..@@..@@@@@@@@@..@..@.@@.@@@@.@@@.@@..@@@@@@....@@.@....@@@@.@@@@.@@@@..@@@@@@@@@@@@@@@@..@ +@@..@@@..@.@@@.@..@@@@.@.@..@@.@..@..@@.@@@@@...@@..@.@@@@@@@...@@.@@@.@@@@@@@.@@@.@@..@.@@..@.@.@...@.....@@@@@.@.@@.@.@@@.@..@@@@@@..@@@@ +.@.@@@@.@....@@.@@......@@@@@@@..@@@@@@@.@.@...@@@@@@@@@@@.@..@@..@@..@@@@@.@@@..@@.@@@@@.@@.@@@.@@....@..@@@@@@@@..@.@@@..@@@@..@.@.....@@ +.@.@..@@.@.@@@@@@.@@@@@...@@...@@.@@.@@@@@@@@@@...@.@.@.@@...@.@@..@.@@@@@@@@.@@@@@@@...@@...@@@@@@@@@@@@@...@@.@@@...@@@.@@@@@@@.@@.@@..@@ +..@@.@@.@...@@@....@@....@@.@@@.@@..@..@@..@@@@.@.@@@@@.@@@@...@@@@@@@@@@.@.@...@.@@@@@.@@@@.@@@@@.@@@@@@.@@.@@@.@@@.@@@@@@..@@@@@@@@.@@@@. +@@@.@@@@@@@@..@..@@@.@@@.@@@.@@.@@..@..@.@..@....@@@@@..@@@..@.@@@@@@.@@.@@.@@@@...@.@@@@@.@.@@@@.@@@@@@@@@@.@@.@@@@@@@@.@@@@@@@@@.@@@..@.@ +@@@.@.@@.@@....@@@@@..@.@@@...@.@....@@@@@.@.@@@@@@@@@...@@...@.@@@@@..@..@@@.@@@.@@...@.@.@@@....@.@..@@...@...@@.@@@@..@.@@@@@@@.@..@@@.. +.@@@.@......@.@..@.@@....@.@@@.@@.@....@@..@.@@@.@@@@...@@.@@@@@.@@@..@@@@@@..@@.@.@@@.@...@@@@@.@..@@@@@@..@@..@.@@@.@..@@.@@.@@.@@.@.@@@@ +@@.@@@@@@..@@.@.@@@@@.@@@@@@@.@.@..@@@@@.@@@...@.@@@@@@@@..@@@.@@@@@@.@..@@@@@..@....@...@@@.@@.@.@@@@@@@@.@@@@@.@..@@.@@@@.@@@@.@@..@.@..@ +@.@..@@.@@@@.@.@@@@.@@@@@@@@.@@@@@.@@@@@@@@@@@.@@@@@@@.@@@@@@...@.@@@@.@@@@@...@@@@.@@..@@@@.@@.@..@..@@@.@.@@@@.@.......@@.@.@..@.@.@@@.@. +....@.@@@@@@@@@@.@@@.@..@@@.@@@@@.@@@@@..@@@@..@@@.@.@.@..@@.@@.@.@@@@@@.@.....@.@@@..@@@@@@@@..@@@@.@.@@@@@@..@@..@@@.@@.@@.@@@@.@.@@@@@@. +@@...@@.@.@@.@@@@@@@.@@@.@.@@..@.@@@@@.@.@...@@..@@@.@..@@@.@@@@@@@@@.@@@.@@@@@.@.@@@@@@.@.@@.@@@.@.@@@@..@@..@.@@@..@@@.@.@@@..@.@.@@@.@@@ +@..@@@.@@@@@.@@.@@@@@@@@@@@.@@@..@@@@@@@@@.@@.@.@@@@..@@..@@.@.@@@@.@.@@@.@@@.@@.@@@...@@@@@...@...@@@@@..@@...@@@.@@@@@@@@..@@.@@.@@@@@@@@ +.@.@.@@@..@@@.@@@.@@@@@@@@..@@@@@..@@@@@.@@@@@..@..@.@.@.@@@@@@@@@@@@@.@..@@@@@@@@@@.@.@.@@@@.@.@@@@@@@.@@@@@...@@...@@@@@@@@.@@.@@@@@@@@@@ +@@@@.@@@@.@@@@@.@..@@.@@@@@...@@...@.@..@..@.@.@.@@..@..@@@@.@@@@@.@..@@@@@@@@@@@.@@.@@@.@@@@.@@...@@@@@@@@..@@.@.@@.@@@.@..@@@@@@@@@@....@ +..@.@.@.@@....@@@@.@@@@@@@@.@@@@@.@@@@.@..@@@.@.@.@@.@@@@...@@@@..@.@...@@@@@@@@..@@@@@...@@...@.@..@@@@@.@@@@.@..@@.@@@@@@...@@.@@@.....@@ +@@@@@@.@@.@@@..@.@@@@@....@.@@@@@.@..@.@@@@@@..@@.@...@@..@@@@@@.@.@@.@.@.@@...@...@@@..@@@@@@.@.@@@@@..@@@.@.@@@@@@@..@@.@@@@.@.@.@.@.@..@ +@@@@@..@@.@@@..@@@....@@..@@@.@@...@.@@.@@@@..@@@@@@..@@@@@@@..@..@.@@.@.@..@@@@@@@...@.@@@.@@@@@@@@@@@@.@...@@.@.....@@@@.@.@@@@@@.@@@@@.@ +.@.@@@..@.@.@@.@.@..@@@@@@.@.@@@@@.@@@.@@@@@@@@.@.@.@.@@@@..@..@@@@@@.@...@.@@@@....@.@@@@@@.@..@@@.@@.@..@.@.@.@..@@@.@@@.@..@...@@@.@@@@@ +@@.@@@@@@@...@@.@.@@@.@@@@.@.@...@.@@@...@.@@@.@.@@..@@@@@.@@@@@@@@.@.@@..@.@...@@@@@...@@.@.@.@@@@...@@.@.@.@.@@@@@.@..@@..@.@..@@@..@..@@ +@@@@@@@@..@.@@@.@.@...@...@@..@@.@@@...@....@@@.@@@.@.@.@..@.@@@@.@@.@@@.@.@@.@@@@@@...@.@.@@@.@.@@@@@.@@@@@..@@@@..@..@@@@@..@...@@@@@@.@@ +.@@..@.@..@.@@.@@@@.@@@@.@@@@@.@.@@@@@.@@@...@.@@@.@@@....@..@@.@.@@@...@@@.@@...@.@@...@@..@..@@@.@@.@@@@@@@@..@.@@@@.@@@@@@..@@@.@@.@@@.@ +@@@.@@@@@..@.@@@.@@@....@.@...@.@..@@@.@@.@@@@@@.@.@..@@@@.@@.@.....@..@@.@.@@@@..@@@@@@@@..@@.@@.@.@@@@@.@..@@@@.@@.@@@@.@.@@@@@.@@.....@@ +@@@.@@@.@@@.@@@@@.@@..@@@@@.@.@@@@@@@.@.@.@@@@@@@@@@@@@.@@.@..@.@@.@@@@.@@@@.@@@@@@@..@@....@@.@.@@@@....@....@@.@@...@.@@@@.@.@@@@.@@@@@@@ +@@@.....@@@@.@@@..@.@@.@.@@@@...@@@@@@@.@@@@@@@.@@.@@..@@@@@@@...@@@..@@@@..@..@@@.@@@.@.@@@@@.@@.@@.@..@....@.@@@.@@@@@.@@.@@...@@@.@.@..@ +@@.@@.@@@....@.@@.@@.@@@@@@@@@@@@@..@..@..@@.@.@.@..@@.@@@@.@@.@@.@..@.@......@@.@..@@@.@@@.@@@.@@@@....@.@@@@.@@@@@@@..@@.@@@..@.@@..@...@ +@@.@@.@@@@@..@..@...@@@.@.@.@@.@...@.@.@@@@@@@.@@.@..@.@...@..@@@@@.@@@@@@.@@.@.@@@.@@.@@@@@.@@@@.@.@.@@@@@@@@@.@.@@@@.@@@.@.@@.@@@.@.@@@@@ +.@@@@.@@...@@@.@@.@@@@@.@@..@@.@.@@..@.@.@@@.@.@@@.@@.@.@.@..@@@.@.@@@@@@@..@@.@@.@@.....@@..@.@.@@@..@.@@@.@.@...@@@@@@@.@.@@@@.@@@@.@.@.. +.@..@@@@.@@.@.@@@..@@@.@@@@@@@@@@@.@@..@@..@@@@@.@.@.@.@..@.@.@.@@@@@@@@@@@@.@@@..@.@...@.@@@.@..@@@@@@.@@@.@@@...@@@.@@@..@.@@.@@.@.@@.@@. +@@@@@@@.@.@.@@@@@.@..@@@@@@@...@@@@@@@.@@.@@@@@..@@@.@@@.@.@@.@@..@@@@@@@@@@@.@..@@@@..@@@@@.@@@@@@...@@@.@@.@.@.@@@@....@@.@@...@@.@.@@@@@ +@@@.@@@@@@....@@..@@.@@@@@..@@@.@..@@.@@@@.@@.@@@@@@@@.@.@@.@@@.@@@@@...@@@@@@.@.@.@@.@@@@@.@..@@..@@.@@@@.@@@@@.@@@@@@@..@.@@....@@.@@.@@@ +@@@@@@@.@@@.@@@@@@@..@@..@.@@@@.@@...@@..@@@@@.@@..@@@.@.@@@..@.@@@@@@.@@@@@.@.@.@@@..@@.@@.@@@...@@...@@.@@@@@..@@@.@..@@....@@@..@@@@.@.. +.@@..@.@.@@..@..@@@.@@...@@...@@.@.@@@@@@.@.@@.@@@@@@@@.@@.@@@@..@..@.@..@@@..@@@@....@.@.@@@@@..@.@@@@@@@@@.@@.@.@@.@@.@@@.@@@.@@@@.@@@@@@ +@@@......@@@.....@@@..@.@@@...@.@@@@@@@.@.@@@@@.@.@@@@@..@.@@@.@@.@@@@@@@@@.@.@@@.@@@.@.@@@@..@@@@..@@@@.@.@..@...@@...@..@@@.@....@@.@@.@. +@@@.@@@@@@@@@@@....@..@@@.@...@@@@@@@.@@@@.@.@@..@@...@@@@@.@@@@@...@..@.@.@@@..@@@@@.@.@.@.@@@.@@@..@.@@@@@@.@@@@@@.@@@@@@@@@@.@@.@..@@@@. +.@@@.@.@.@.@@@@@....@.@@..@@@@..@.@@@@@@.@.@@@@@.@@@..@@@.@.@@@.@@..@@..@.@.@@@@.@@@@.@.@..@..@.@@@.@.@@@@@.@.@@@.@@..@@@@@@@.@@@@..@@@@..@ +@@.@@@..@.@.@.@@@@.@@@@@.@@...@.@@@@@@..@@@@.@.@@@@.@@@@@@@@@.@.@@...@.@@@..@.@.@@@@@@@.@.@@.@@@.@@.@@...@...@.@@@..@.@.@..@.@@@@@@@@.@@@@. +@@@@@@@@.@..@@@@@@@.@.@@@@@@..@.@@@@@@@..@..@@@@@@@@@@@@@@@@@@.@@@@@@@.@@@@@@@@@@.@@@@@@@.@.@@@.@.@@@@@..@.@@@@@...@@.@@@@..@@@@..@@@.@@@@. +.@@@@@.@@@@@.@.@@@@@@@..@@@@@@@@@.@@@@.@@.@...@@.@@@@@@.@@@@.@@@@@@.@@@@@@...@@@..@@@@..@.@.@.@@@@@@@@@@.@.@@@@@@@@@..@@.@@@@.@...@@@@@.@.@ +@@@@.@@@.@@@@@@...@@..@.@...@@@.@@.@.@.@....@@@...@@....@@....@.@@@@@@@...@...@@@@@@@@@@@@@@@@@...@@@@@@@@@.@.@@@.@@@@@@@@@@@@@@.@@@@@@@.@. +@@@@@@.@@@.@@@..@@@.@.@.@@.@@.@..@@@.@@..@@.@@@@.@.@.@@@@@.@.@@........@@.@@.@@.@@..@.@@.@.@..@@@.@@....@@@@.@.@@@..@..@.@@..@@@.@@@.@.@@@. +..@@.@@@@@@.@@@..@@@.@.@@@@.@.@@.@@@.@.@@@@.@.@.@.@@.@..@@@@@......@.@@@@.@@@.@@.@@@@@@.@@@@.@@...@@.@.@@.@@.@.@@.@@.@@.@@.@@@@@@@.@@@@@.@@ +@..@...@.@@@@@@@@...@@@@@@..@@....@..@@@@.@.@.@@@@@@@..@..@@@@.@@.....@..@.@..@@.@..@@@..@.@@@@@.@@@..@@@@@@@@.@@@@@@.....@@.@@.@@.@@..@..@ +@@@@.@.@@@..@.@@@..@@@@@@@@.@@....@..@@..@@.@.@.@@.@@.@@@@.@.@.@@..@@.@..@..@@@..@@...@.@@@@@@@.@@.@@@@.@@@@.@@@..@..@.@@.@@@@@.@@.@@@...@@ +.@@@.@@.@.@..@.@@..@....@@@@@@.@@.@@.@.@@.@.@@@..@@.@..@.@..@@@@@..@@..@@@.@..@@@...@.@@@@@..@.@..@@...@@.@@..@.@@@@@...@@.@.@.@@.@@@@@@@@@ +@.@@@@@.@@..@@.@.@@@@@@@.@@.@@@@@@@@@@...@@@@.@..@.@@@..@.@@@@@@@.@@.@@...@.@@@@..@@@@.@@.@@.@@@@@.@.@.@.....@@.@@..@@@@@@@@..@.@.@@@@.@@@. +@@.@@@@.@@@.@@@.@@.@@@@@@@..@@..@.@..@...@@@.@@@@@@@@@@@@.@@.@@.@@..@@@.@@@.@...@@.@@@@@@@@.@.@@@...@@..@.@.@@@@@@.@@@...@@@..@.@..@.@@.@@@ +.@@.@@@@@@@@@...@@.@@@@@@@@..@...@.@@@.@@.@@@@@@@@@.@@@@@@@@@@@@.@@@@@.@..@.@.@@@...@@.@.@@....@@..@@@@@@.@@@.@@@.@@@..@@@@@@.@@.@.@@..@.@@ +@.@..@@..@@@.@..@@@.@@.@..@@..@...@@.@.@@@.@@@....@.@@@@@@.@.@.@.@.@@@.@@.@@@@@@..@.@@@@@.@@@@@.@.@@@.@@@@@..@.@.@.@@@@@.@@@@@@@@@@@.@@..@@ +.@@@..@@@@@@@@@@@@@.@..@@.@.@@@@@@@@@.@.@@.@@@@@@@@@@.@@@@..@@@.@@@@@.@@@@@..@.@@...@@@@@@@....@@.@@@@@.@@@@.@@@@..@@@@@@@.@.....@.@@@.@@.. +@@@@@@@@@.@@@@..@@@@.....@.@@@@@.@.@@@@@@.@@@.@..@@@...@@@@@@@@@@@.@@@...@@@@.@@@@@@.@@..@@.@...@@@@@@@..@@@@@.@.@@@@@@@@@.@..@@@.@@@@@@@.. +@@@...@@.@@...@@@.@@.@@@@@@.@@@.@@....@.@.@@@..@.@@@.@.@@..@@@.@@.@@@@.@@@.@.@.@@@..@@@..@@@@@@@@@..@@@.@.@..@.@@.@@@...@.@@@@@.@@..@@@@@.@ +@@.@@...@@@.@@@.@@.@.@@@@@.@@@@@.@..@@@@.@@.@@@@@.@@.@@@@@@@.@.@@@..@@@@@@...@@@@@.@.@@@.@....@@@@@@@@@@@@@.@@.@..@@@@@@@@@@@@.@@@.@.@.@@.@ +@.@.@@@.@.@.@@@@@@@@@@.@@.@@@@....@..@@..@@@.@.@.@.@.@.@@@@..@@.@@@@@..@@@.@@@.@@@@@@@@@@.@@@@@@@@...@@@@@@@@@.@@@.@@.@@.@...@@@.@@@@.@...@ +..@@@@@@...@.@@@@@@@@...@...@@@.@@.@..@.@.@@@.@.@@..@@@.@.@.@.@@@@@@@@@@@.@@@@@@@.@.@@@@.@..@@@.@..@.@@..@...@@..@@@..@@..@.@@@@.@.@@..@@@@ +@@.@@@@@@@.@@.@..@@@@..@@@@@@@@@@@@@...@@@...@@.@@@@@@@@@@@@@.@@@.@..@..@.@@@@@@.@@@@@.@.@@@@@@@.@.@@@@.@.@..@@@@.@@.@@.@@@@.@.@.@@@@@..@.@ +@@@@@@.@.@...@.@@@@@@...@@@@@@@@.@@@@@@..@@@..@@@@@.@@@@@.@@@.@@.@@@@@.@@.@@@@@@@@@@..@..@@@@@@.@@@@.@...@@.@@@.@@.@...@@@@@@.@@@.@.@..@.@@ +.@@@@@...@.@...@@.@.@..@.@...@@@.@@@@@@@..@@@@@@..@.@@@@@@@@@..@@@@@@@@@@@..@@@@@@@@.@.@@@@..@@@@@@@...@..@@.@.@.@.@..@.@@@@@@@@..@@@@@@..@ +@@@@@@....@..@@.@@..@@@@.@@@@@@@.@@.@@..@@@@@@@.@.@@.@@.....@.@@@@@@..@.@@..@..@..@@@@@.@@.@@@@@@.@@@.@@@.@.@@@.@@@.@@.@@@@.@...@@@@@..@.@. +@@@..@.@.@@@.@@@@@@.@.@@@@@@..@@.@@.@@@@....@@@.@@@.@.@.@@@@@..@@@.@@@@..@@.@.....@@..@.@@@.@.@@@.@@@@@@.@@..@@.@..@@.@@@@@@@.@@@...@@@@@@@ +...@@@@@@@@.....@.@@.@..@@..@@.@.@.@.@@..@@@@@@@@@...@@@@@@..@@..@@@.@@@@@@@....@.@@.@@@@@@.@@@@@.@@@@@@@@@@@@@.@.@@@@@@..@.@@@@@@..@@@@@@. +@@@@.@@@@@.@@@..@@.@@@@@.@@@.@@@...@@@.@.@.@@.@@@@.@.@@@@.@@@@..@.@@.@...@@@@@@@@@.@@@@...@.@.@@@.@@@..@@.@..@.@.@@..@@.....@@@@.@.@@.@@.@@ +..@@......@@.@@@.@@..@@@@@..@@..@@.@@@@@@@@@@.@.@@@.@@@@....@@@@..@..@@..@@@...@@.@.@@@@...@@@@@@@...@@@@.@.@.@@@...@@@.@@@@@.@@@.@@....@.@ +@.@@.@@..@@@@@@.@.@.@.@@@@@@..@..@@.@@.@@..@.@.@.@..@..@.@@@@..@@@@.@@@@@.@@.@.@@@.@@.@@.@..@@...@@.@.@@@.@@@@.@@@@.@@@@..@.@...@@@@@@@@@@. +.@@..@@.@@@.@..@@@@....@@@..@..@@@.@@@@@@@@@.@@@.@@@@@@..@@@@@@@@@..@@@..@.@@.@@@@....@@@@@..@@.@@@..@@@.@@@@.@.@.@@@@@@@..@..@@@@...@@@@@@ +@.@@@@.@@@@@.@.@.@@.@@@.@@.@.@.@@..@.@..@@@@@.@@..@@.@@@@@@.@..@@@...@.@..@..@@@@...@@@@.@@@@@@@..@..@@@.@@.@@@@.@..@@..@@@@@..@.@@@@@@.@.@ +@@@@@@@.@@@@..@@@@@@@@.@@@@@@@@@@@@@@@@.@.@..@@...@@.@@.@@@.@@@@..@.@@@@.@.@.@@@@@....@.@@@..@@@@..@@@@.@@@@@@@@...@@..@.@.@@@@@@@@@@...@.@ +@@@@@.@.@@@..@@@@..@..@@...@@@.@@.@@@.@@.@@@.@.@@@@@..@..@.@...@@@@@.@@@@@@@@@.@.@@@@@@.@@.....@@@..@@@.@@@@@.@.@@@.@..@.@.@.@@@@.@@@..@@@@ +.@@@@@@@.@@.@@@@@@.@@...@@@@.@@@@.@@.@@@@.@.@....@.@.@@@@@@.@..@.@@@@@@@@@...@@.@.@......@.@@..@...@@@@@@.@.@@@@..@@@.@.@@@....@@@.@.@@@@@. +..@@@@@@@@.@@.@@.@.@.@@@@..@..@@.@...@@@.@@@@@@.@..@..@@.......@@.@@@@@@@@@@@.@....@...@..@.@@@....@.@.@.@.@@@@@.@.@@@@@.@@@@@@@@...@@@@.@. +@.@@@.@.@@.@@..@@@...@@.@@.@...@@@...@@@@@..@.@@..@...@@@.@@@@@.@@@@@@@.@@@@.@@@.@@@.@.@@@..@@@@@.....@@@@@@@..@@@.@.@@.@.@..@...@@@@@@@@.@ +@@@..@.@@@.@.@@..@@@@...@@.@.@@@.@@@@...@@..@@@@@@.@..@...@@@@..@@..@@@@@......@.@@@@@@@.@@@@.@@@.@@@.@@@@@@@@@@@.@.@@@@...@.@@..@@@@@@@@@@ +@@@@@@@@@@@.@@@@@@@@@@@@@@.@.@..@@.@@@@.@.@.@@@..@@@@@.@@@@.@@@...@@@.@..@@@..@.@@@@@.@@..@@.@@@@@...@@@@.@..@..@.@.@@@@@@@@.@@@.@.@@@@@..@ +@.@.@@@@@.@.@@@.@@@...@.@.@.@.@@@.@.@@.@@@...@@@@@..@.@@@@.@@@@@.@.@.@@@..@.@.@@.@.@.@@@@..@.@.@@@@@@@.@@@..@@@.@@.@..@@@.@.@@@@@@@.@..@@.@ +..@...@@@@@.@..@@@..@.@@@@@.@.@@.@.@@@@.@.@.....@@.@@@.@@@@@@@@..@@@..@.@..@.@@@@@@..@@.@@..@..@@@..@@@.@..@@...@@.@.@@@@@.@.@@@@.@.@@@@@.@ +.@@@@@@..@@@@..@@@..@@@@@@.@@@@.@.@@@@..@@@@@.@@@@...@@.@@@...@@@@..@@.@..@@@@@..@...@@@@@..@@@.@@@@@@.@@@@@@@@..@@.@@@@.@.@..@@.@.@@@.@@.@ +.@.@@.@@@@.@.@@@..@@.@.@@..@.@@@@...@@...@.@@@@@.@@.@.@@.@.@...@..@..@@..@...@....@.@@..@@@@.@.@.@.@@@@@@..@@.@@@@@@@.@@.@.@.@@.@@@..@..@.@ +...@.@@@@..@@@@.@...@..@.@@@.@@@@.@@@@@@@.@@@@@@@@@@@@.@@@.@@.@@.@@@.@@..@..@.@@@@@.@@@@@@@@@.@@@@.@@@@@@@.@@@@@@..@@@@@@@.@..@@.@...@@@@@@ diff --git a/src/exercises/assets/day_4_test_input.txt b/src/exercises/assets/day_4_test_input.txt new file mode 100644 index 0000000..8209399 --- /dev/null +++ b/src/exercises/assets/day_4_test_input.txt @@ -0,0 +1,10 @@ +..@@.@@@@. +@@@.@.@.@@ +@@@@@.@.@@ +@.@@@@..@. +@@.@@@@.@@ +.@@@@@@@.@ +.@.@.@.@@@ +@.@@@.@@@@ +.@@@@@@@@. +@.@.@@@.@. diff --git a/src/exercises/day_4.ts b/src/exercises/day_4.ts new file mode 100644 index 0000000..e6d1591 --- /dev/null +++ b/src/exercises/day_4.ts @@ -0,0 +1,115 @@ +type Grid = string[][]; +type Coordinate = { + x: number; + y: number; +}; + +export default async function PrintingDepartment() { + const grid = await read_rolls_grid("src/exercises/assets/day_4_input.txt"); + console.log(">> Accessible roll count: ", count_accessible_rolls(grid)); + console.log( + ">> Accessible roll count with remove: ", + count_accessible_rolls_with_remove(grid), + ); +} + +const MAX_ROLLS = 4; +export function count_accessible_rolls(grid: Grid): number { + let total_accessible_rolls = 0; + grid.forEach((row, row_index) => { + row.forEach((pos, pos_index) => { + if (pos === ROLL) { + let adjacent_rolls_count = 0; + get_adjacent_positions(grid, row_index, pos_index).forEach( + (adj_pos) => { + if (grid[adj_pos.x][adj_pos.y] === ROLL) { + adjacent_rolls_count += 1; + } + }, + ); + if (adjacent_rolls_count < MAX_ROLLS) { + total_accessible_rolls += 1; + } + } + }); + }); + + return total_accessible_rolls; +} + +export function count_accessible_rolls_with_remove(grid: Grid): number { + let total_accessible_rolls = 0; + const to_remove: Coordinate[] = []; + grid.forEach((row, row_index) => { + row.forEach((pos, pos_index) => { + if (pos === ROLL) { + let adjacent_rolls_count = 0; + get_adjacent_positions(grid, row_index, pos_index).forEach( + (adj_pos) => { + if (grid[adj_pos.x][adj_pos.y] === ROLL) { + adjacent_rolls_count += 1; + } + }, + ); + if (adjacent_rolls_count < MAX_ROLLS) { + total_accessible_rolls += 1; + to_remove.push({ x: row_index, y: pos_index }); + } + } + }); + }); + + // Clean up removed + to_remove.forEach((coord) => { + grid[coord.x][coord.y] = "."; + }); + + if (total_accessible_rolls === 0) { + return 0; + } + + return total_accessible_rolls + count_accessible_rolls_with_remove(grid); +} + +// NOTE: manual generation to be quick +function get_adjacent_positions( + grid: Grid, + x: number, + y: number, +): Coordinate[] { + const adjacent_list: Coordinate[] = []; + + if (x - 1 >= 0 && y - 1 >= 0) { + adjacent_list.push({ x: x - 1, y: y - 1 }); + } + if (x - 1 >= 0) { + adjacent_list.push({ x: x - 1, y: y }); + } + if (y - 1 >= 0) { + adjacent_list.push({ x: x, y: y - 1 }); + } + if (x + 1 < grid.length && y + 1 < grid[0].length) { + adjacent_list.push({ x: x + 1, y: y + 1 }); + } + if (x + 1 < grid.length) { + adjacent_list.push({ x: x + 1, y: y }); + } + if (y + 1 < grid[0].length) { + adjacent_list.push({ x: x, y: y + 1 }); + } + + if (x + 1 < grid.length && y - 1 >= 0) { + adjacent_list.push({ x: x + 1, y: y - 1 }); + } + if (y + 1 < grid[0].length && x - 1 >= 0) { + adjacent_list.push({ x: x - 1, y: y + 1 }); + } + + return adjacent_list; +} + +const ROLL = "@"; +export async function read_rolls_grid(path: string): Promise { + const input_txt = await Deno.readTextFile(path); + return input_txt.split("\n").map((row) => row.split("")); +}