From 7564a3627fe766f5490292a36cb050b84d4bab78 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Tue, 30 Dec 2025 18:56:24 -0500 Subject: [PATCH] day 4 part 2 --- .../AOC2025/Day4/PrintingDeparment.cs | 69 ++++++++++++++----- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/AdventOfCode/Problems/AOC2025/Day4/PrintingDeparment.cs b/AdventOfCode/Problems/AOC2025/Day4/PrintingDeparment.cs index 18a56de..9f3edd1 100644 --- a/AdventOfCode/Problems/AOC2025/Day4/PrintingDeparment.cs +++ b/AdventOfCode/Problems/AOC2025/Day4/PrintingDeparment.cs @@ -1,6 +1,7 @@ using AdventOfCode.Utils.Models; using System; +using System.Collections.Frozen; using System.Collections.Generic; using System.Text; @@ -13,36 +14,23 @@ internal class PrintingDeparment: Problem public override void CalculatePart1() { - var c = 0; - for (int y = 0; y < _size.Y; y++) - { - for (int x = 0; x < _size.X; x++) - { - var pos = new Vec2(x, y); - if (_data[pos.Y][pos.X] != '@') - continue; - var n = CountNeighbors(pos); - if (n < 4) - c++; - } - } - Part1 = c; + Part1 = GetAccessableRolls(_data, _size).Count; } - public int CountNeighbors(Vec2 pos) + public static int CountNeighbors(string[] data, Vec2 size, Vec2 pos) { var c = 0; for (int y = pos.Y-1; y <= pos.Y + 1; y++) { - if (y < 0 || y >= _size.Y) + if (y < 0 || y >= size.Y) continue; for (int x = pos.X - 1; x <= pos.X + 1; x++) { - if (x < 0 || x >= _size.X) + if (x < 0 || x >= size.X) continue; if (pos.X == x && pos.Y == y) continue; - if (_data[y][x] == '@') + if (data[y][x] == '@') c++; } } @@ -51,7 +39,50 @@ internal class PrintingDeparment: Problem public override void CalculatePart2() { - throw new NotImplementedException(); + var data = _data; + var rolls = GetAccessableRolls(data, _size); + Part2 += rolls.Count; + while(rolls.Count > 0) + { + data = RemoveRolls(data, _size, rolls); + rolls = GetAccessableRolls(data, _size); + Part2 += rolls.Count; + } + } + + public static List> GetAccessableRolls(string[] data, Vec2 size) + { + var results = new List>(); + for (int y = 0; y < size.Y; y++) + { + for (int x = 0; x < size.X; x++) + { + var pos = new Vec2(x, y); + if (data[pos.Y][pos.X] != '@') + continue; + var n = CountNeighbors(data, size, pos); + if (n < 4) + results.Add(pos); + } + } + return results; + } + + public static string[] RemoveRolls(string[] data, Vec2 size, List> rolls) + { + var positions = rolls.ToFrozenSet(); + return data.Select((row, y) => + { + var updatedRow = row.Select((col, x) => + { + var pos = new Vec2(x, y); + if (positions.Contains(pos)) + return '.'; + else + return col; + }).ToArray(); + return new string(updatedRow); + }).ToArray(); } public override void LoadInput()