From b37717d05ff3de6997f4fc733027441ee3ea2710 Mon Sep 17 00:00:00 2001 From: Sithis Date: Sun, 7 Dec 2025 21:34:15 +0100 Subject: [PATCH] feat: Add day 7, thanks TG --- AdventOfCode2025/Inputs/Day7_0.txt | 16 ++ AdventOfCode2025/Inputs/Day7_1.txt | 142 ++++++++++++++++++ AdventOfCode2025/Inputs/Input.cs | 2 +- AdventOfCode2025/Solutions/Day1.cs | 12 +- AdventOfCode2025/Solutions/Day2.cs | 12 +- AdventOfCode2025/Solutions/Day4.cs | 29 ++-- AdventOfCode2025/Solutions/Day7.cs | 226 +++++++++++++++++++++++++++++ 7 files changed, 406 insertions(+), 33 deletions(-) create mode 100644 AdventOfCode2025/Inputs/Day7_0.txt create mode 100644 AdventOfCode2025/Inputs/Day7_1.txt create mode 100644 AdventOfCode2025/Solutions/Day7.cs diff --git a/AdventOfCode2025/Inputs/Day7_0.txt b/AdventOfCode2025/Inputs/Day7_0.txt new file mode 100644 index 0000000..8868910 --- /dev/null +++ b/AdventOfCode2025/Inputs/Day7_0.txt @@ -0,0 +1,16 @@ +.......S....... +............... +.......^....... +............... +......^.^...... +............... +.....^.^.^..... +............... +....^.^...^.... +............... +...^.^...^.^... +............... +..^...^.....^.. +............... +.^.^.^.^.^...^. +............... \ No newline at end of file diff --git a/AdventOfCode2025/Inputs/Day7_1.txt b/AdventOfCode2025/Inputs/Day7_1.txt new file mode 100644 index 0000000..cc6fac6 --- /dev/null +++ b/AdventOfCode2025/Inputs/Day7_1.txt @@ -0,0 +1,142 @@ +......................................................................S...................................................................... +............................................................................................................................................. +......................................................................^...................................................................... +............................................................................................................................................. +.....................................................................^.^..................................................................... +............................................................................................................................................. +....................................................................^.^.^.................................................................... +............................................................................................................................................. +...................................................................^.....^................................................................... +............................................................................................................................................. +..................................................................^...^.^.^.................................................................. +............................................................................................................................................. +.................................................................^.^...^.^.^................................................................. +............................................................................................................................................. +................................................................^...^.^.^.^.^................................................................ +............................................................................................................................................. +...............................................................^.^.^.^.^.^.^.^............................................................... +............................................................................................................................................. +..............................................................^.^...^.^.^.^...^.............................................................. +............................................................................................................................................. +.............................................................^.^.^.^.^.^.^.^.^.^............................................................. +............................................................................................................................................. +............................................................^.^.^.............^.^............................................................ +............................................................................................................................................. +...........................................................^.....^.....^.^.^.^.^.^........................................................... +............................................................................................................................................. +..........................................................^...^.^.^.^.^...^.^.....^.......................................................... +............................................................................................................................................. +.........................................................^.^.^.^.^.....^.^.^.^.^.^.^......................................................... +............................................................................................................................................. +........................................................^.^...^.....^.^.^.^...^.^.^.^........................................................ +............................................................................................................................................. +.......................................................^.^.^.^.^.^.^.........^.^.^.^.^....................................................... +............................................................................................................................................. +......................................................^.^.^.......^.^.^.^.^...^.......^...................................................... +............................................................................................................................................. +.....................................................^.^.^.^.^.^.....^...^.^.^.^.^...^.^..................................................... +............................................................................................................................................. +....................................................^.^.^.....^...^.^...^.....^...^.^.^.^.................................................... +............................................................................................................................................. +...................................................^...^.^.^.....^.^.^.^.^...^.^.......^.^................................................... +............................................................................................................................................. +..................................................^.^.^.^.^.^.^...^.^.....^.^.......^.^.^.^.................................................. +............................................................................................................................................. +.................................................^.^.^.^.^...^.^.^.^.^.^...^.......^.^.^.^.^................................................. +............................................................................................................................................. +................................................^.^.......^.^.^.^.......^.^.^.........^...^.^................................................ +............................................................................................................................................. +...............................................^.^.^.^.^.^.....^.^.^.^.^.^.^...^.....^.^.^...^............................................... +............................................................................................................................................. +..............................................^.........^.^...^.^.^.^.^.^.^.....^.....^.^.^.^.^.............................................. +............................................................................................................................................. +.............................................^.^...^.^.^.^.^.^...^.^.......^.....^.......^.^.^.^............................................. +............................................................................................................................................. +............................................^...^...^.^.^.^.^.^.....^.^...^.^.^.^.^.^.^.......^.^............................................ +............................................................................................................................................. +...........................................^.^.^.^.^.^.^.^.......^.......^.^.^.......^.....^...^.^........................................... +............................................................................................................................................. +..........................................^.^.^.....^.....^...^.^.^.^...^.^.^.^...^.^.^.....^...^.^.......................................... +............................................................................................................................................. +.........................................^...^.^.^.^...^.^.^...^.^.^...^.^...^.^...^.^.^.^.^...^...^......................................... +............................................................................................................................................. +........................................^.^.^.^.^.^.^.......^.^.^.^...^.^...^.^.^.^.^.^.....^.^...^.^........................................ +............................................................................................................................................. +.......................................^.^.^.^.^...^.^...^.^.^.^.....^...^.^.^.....^.^...^.^.^.^.....^....................................... +............................................................................................................................................. +......................................^...^.^.^.^.^...^.^.^...^...^.^.^.^...^.^...^.^.^.......^...^.^.^...................................... +............................................................................................................................................. +.....................................^.^...^.^.^...^.^.^.^.....^.^.^.^.^.^...^...^.^.^.....^...^.^.^.^.^..................................... +............................................................................................................................................. +....................................^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.^.^.^.^.^.................................... +............................................................................................................................................. +...................................^.^.....^.^.....^.......^.^.^.....^.......^.^.......^...^.^.^.^.^.^.^.^................................... +............................................................................................................................................. +..................................^.^...^.^...........^...^.^.^.^...^...^.^...^.^...^.^.^.^.^.^.^.^.^.^...^.................................. +............................................................................................................................................. +.................................^.^.^.^.^...^.^.........^...^.^.........^...^.^.^.^.^.^.^.^.^.^.^.....^.^.^................................. +............................................................................................................................................. +................................^.^.^.^.^.^.^.^.......^.....^.^.^...^...^...^.^.^...^.^.....^.^.^.......^.^.^................................ +............................................................................................................................................. +...............................^.^.^...^...^.^.....^.^.^.^.^.....^...^.^.^.^.^.^.^.^.^.^...^.^.....^.^.......^............................... +............................................................................................................................................. +..............................^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.^...........^.^.^.^...^.....^.....^.....^.^...^.............................. +............................................................................................................................................. +.............................^...^.^.^.^.....^.....^...^...............^.....^.^...^...^.^.......^.^.^...^.^...^............................. +............................................................................................................................................. +............................^.^.^.......^.^.^.^.....^.^.^.^.^.^...^...^.^.........^.^.^.^.^.^.^.^...^.^.^.....^.^............................ +............................................................................................................................................. +...........................^.^.^.....^.^.......^...^.^.....^.^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.....^.^.^........................... +............................................................................................................................................. +..........................^.^...^.^.^.^.....^...^...^.^.^...^.^.^.......^.^...^...^...^.^.^.^.^.^...^...^.^.......^.......................... +............................................................................................................................................. +.........................^.....^.^.^.....^.^.^...^...^.^.....^.^.^...^.^...^.^.^.^.^.^.^...^.^.......^.^.^...^.^...^......................... +............................................................................................................................................. +........................^.^.....^.......^.^.^.^.^.^.^.......^.^.^.^...^...^.^.^...^...^.^.^.^.^.....^.^.^.^...^...^.^........................ +............................................................................................................................................. +.......................^.^.^.^.^...^...^.^.....^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^...^.^.....^.^.^.^....................... +............................................................................................................................................. +......................^...^...^...^...^.^.^.^...^.^.^.....^.^.^.^.^.^.^.....^...^.^.^.....^...........^...^...^.^...^.^...................... +............................................................................................................................................. +.....................^.^.^.^.^.^...^.^.^...^.^.^...^.^...^.....^.^.^.^.^.^.^.^.^...^.^.^.....^.^...^.^.^.^...^.^.^.^.^.^..................... +............................................................................................................................................. +....................^...^.........^.^...^...^...^...^.^...^.^.^...^.^.^...............^...^.....^.......^.........^.^.^.^.................... +............................................................................................................................................. +...................^.^.^.^.^.......^.^.^.^.^.....^.......^.^.^...^...^.^.^...^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.....^.^................... +............................................................................................................................................. +..................^.........^...^...^...^.^.^.^.^...^.^...^.^.....^.....^.^.^.^.^.....^...^.^.^...^.^...^.^.^.....^.......^.................. +............................................................................................................................................. +.................^.^.^.^.^.^...^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^...^...^...^...^.^.....^...^...^................. +............................................................................................................................................. +................^.^.^.^...^.^.^.^.^...^.^.^.^.^.^...^...^...^...^.^.^.^.^.^.^.^.....^.^.^...^...^.^.^.^...^.^.^.^.^.^.^.^.^.^................ +............................................................................................................................................. +...............^...^.^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^...^...^.^.^.^...^...^.^.^.^...^...^.....^.^...^.^.^.^.^.....^.^.^............... +............................................................................................................................................. +..............^...^...^.^.^.^...^.^...^...^.....^...^...^.^...^...^...^.^...^...^.^...^.^...^...^.^.^.....^.^...^.........^...^.............. +............................................................................................................................................. +.............^.^.^...^.....^.^.^.....^.....^.......^...^.^.^...^.^.^.^.^...^...^.^.^.^...^.^...^.^.^...^...^.^.^...^.^.^...^...^............. +............................................................................................................................................. +............^.......^.^.^.^.^.......^.^.^.^.^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.^.^.........^...^.^.^.^.^.^.....^.^.^...^.^.^...^............ +............................................................................................................................................. +...........^.^.^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.....^.^.^.^...^...^.^.^.....^.^.....^.^.^.^.^...^...^.^........... +............................................................................................................................................. +..........^.^.^...^.^.^.^.^.^...^.^.^.....^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^.^.^...^.....^.^.......^.^.^.....^...^.......... +............................................................................................................................................. +.........^...^...^.^...^.^.^.^.^.^.^.^.....^.^...^...^...^.^.....^...^.^.^.^...^.^.^.^.^...^.^.^.........^.^.^.^.....^.....^...^...^......... +............................................................................................................................................. +........^.^...^...^.....^.^.^.^.^...^.^.^.^.....^.^...^.^.^.^.^...^.^...^...^.^.^.......^.......^.^.^.^.^...^.^.^.....^...^.^.....^.^........ +............................................................................................................................................. +.......^.^.^.^.^.^.^.^.^...^.^.^.^.^.....^...^.....^.^.^...^.....^...^.....^.^...^.^.^.^.^.^.^.....^.^.^.^.^.....^.^...^.^.^.^...^.^.^....... +............................................................................................................................................. +......^.^...^.^.^.^.........^.^.....^.^.^.^...^.^.^.^.^.^...^...^.^.^.^.......^.^.^...^...^.^.^.^...........^.^.^...^.^.^...^.^.^.^.^.^...... +............................................................................................................................................. +.....^.^.^.^.^.^.^.^.^.^.......^.^.^.....^.^.^.^.^...^...^.^...^.^.^.^.^.^.^.^...^.^...^.^.^...^...^.^.^.^.^.....^.^.^.^.^...^.^...^...^..... +............................................................................................................................................. +....^...^.^.^.^.^.^.^.^...^.^...^...^.^.^.^.....^.^.^.^.....^.^.^.^.^...^.^.^...^.^.^.^.^.^.....^.^.^.^...^...^.^.....^.^.^...^.^.^.^.^.^.... +............................................................................................................................................. +...^.^.^.^.^...^.^.^...^.^.^.^.^...^.^.^...^.^...^.^...^.^.^.^.^.^.^...^...^.^.^.^...^...^.^...^...^.^.^.^.^.^.^.^...^.^.^...^.^...^.^...^... +............................................................................................................................................. +..^.^.^.^.....^.........^...^.....^...^.....^.^.^.....^...^.^.^.^.^...^.^.^.^.^...^.....^.^...^.^...^.^.^...^.^.^.^.....^.^.^...^.^...^.^.^.. +............................................................................................................................................. +.^.^.^.^.^.....^.^.^.^.^.^.....^...^...^.^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.....^.....^.^.^...^.^.^.^.^...^.^.^. +............................................................................................................................................. \ No newline at end of file diff --git a/AdventOfCode2025/Inputs/Input.cs b/AdventOfCode2025/Inputs/Input.cs index 18f7252..a2d1944 100644 --- a/AdventOfCode2025/Inputs/Input.cs +++ b/AdventOfCode2025/Inputs/Input.cs @@ -4,5 +4,5 @@ public sealed record Input(int Day, int PartNumber = 1) { private const string BasePath = "../../../Inputs/"; private string ToPath() => BasePath + $"Day{Day}_{PartNumber}.txt"; - public IEnumerable Lines => field ??= File.ReadAllLines(ToPath()); + public List Lines => field ??= File.ReadAllLines(ToPath()).ToList(); } \ No newline at end of file diff --git a/AdventOfCode2025/Solutions/Day1.cs b/AdventOfCode2025/Solutions/Day1.cs index c550835..c54a54a 100644 --- a/AdventOfCode2025/Solutions/Day1.cs +++ b/AdventOfCode2025/Solutions/Day1.cs @@ -10,9 +10,9 @@ public sealed class Day1() : Solution(1) { var arrow = 50; var zeroCounter = 0; - foreach (string line in Input.Lines) + foreach (var line in Input.Lines) { - int number = int.Parse(line[1..]); + var number = int.Parse(line[1..]); arrow = line.First() switch { 'L' => arrow - number, @@ -35,13 +35,13 @@ public sealed class Day1() : Solution(1) var arrow = 50; var zeroCounter = 0; - foreach (string line in Input.Lines) + foreach (var line in Input.Lines) { - int number = int.Parse(line[1..]); + var number = int.Parse(line[1..]); zeroCounter += number / MaxArrow; - int remainingNumber = number % MaxArrow; + var remainingNumber = number % MaxArrow; - int end = line.First() switch + var end = line.First() switch { 'L' => arrow - remainingNumber, 'R' => arrow + remainingNumber, diff --git a/AdventOfCode2025/Solutions/Day2.cs b/AdventOfCode2025/Solutions/Day2.cs index 41f00e2..b0790f1 100644 --- a/AdventOfCode2025/Solutions/Day2.cs +++ b/AdventOfCode2025/Solutions/Day2.cs @@ -30,8 +30,8 @@ public class Day2() : Solution(2) private static IEnumerable GetIds(string from, string to) { - long current = long.Parse(from); - long end = long.Parse(to); + var current = long.Parse(from); + var end = long.Parse(to); while (current <= end) { yield return current; @@ -53,8 +53,8 @@ public class Day2() : Solution(2) continue; } - string testBlock = input[..subLength]; - string theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength)); + var testBlock = input[..subLength]; + var theoreticalString = string.Concat(Enumerable.Repeat(testBlock, input.Length / subLength)); if (theoreticalString == input) { return true; @@ -77,7 +77,7 @@ public class Day2() : Solution(2) } var firstHalfIndex = 0; - int secondHalfIndex = input.Length / 2; + var secondHalfIndex = input.Length / 2; while (secondHalfIndex < input.Length) { if (input[firstHalfIndex] != input[secondHalfIndex]) @@ -94,7 +94,7 @@ public class Day2() : Solution(2) private static (string from, string to) ToLimit(string range) { - string[] parts = range.Split('-'); + var parts = range.Split('-'); return (parts[0], parts[1]); } diff --git a/AdventOfCode2025/Solutions/Day4.cs b/AdventOfCode2025/Solutions/Day4.cs index d02ff72..44373d2 100644 --- a/AdventOfCode2025/Solutions/Day4.cs +++ b/AdventOfCode2025/Solutions/Day4.cs @@ -9,7 +9,7 @@ public class Day4() : Solution(4) public override string SolvePart1() { - char[][] grid = PaddingField(Input.Lines) + var grid = PaddingField(Input.Lines) .Select(line => line.ToCharArray()) .ToArray(); @@ -18,7 +18,7 @@ public class Day4() : Solution(4) public override string SolvePart2() { - char[][] grid = PaddingField(Input.Lines) + var grid = PaddingField(Input.Lines) .Select(line => line.ToCharArray()) .ToArray(); List<(int, int)> paperCanBeRemoved; @@ -27,12 +27,11 @@ public class Day4() : Solution(4) { paperCanBeRemoved = GetRemovablePaper(grid).ToList(); paperCount += paperCanBeRemoved.Count; - foreach ((int row, int col) in paperCanBeRemoved) + foreach (var (row, col) in paperCanBeRemoved) { grid[row][col] = NoneChar; } - } - while (paperCanBeRemoved.Count > 0); + } while (paperCanBeRemoved.Count > 0); return paperCount.ToString(); } @@ -59,22 +58,12 @@ public class Day4() : Solution(4) private static bool HasMorePaperNeighborsAs(int x, int y, int threshold, char[][] grid) { - var directions = new (int dx, int dy)[] - { - (-1, -1), - (0, -1), - (1, -1), - (-1, 0), - (1, 0), - (0, 1), - (-1, 1), - (1, 1) - }; + var directions = new (int dx, int dy)[] { (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (0, 1), (-1, 1), (1, 1) }; var paperNeighbors = 0; - foreach ((int dx, int dy) in directions) + foreach (var (dx, dy) in directions) { - int nx = x + dx; - int ny = y + dy; + var nx = x + dx; + var ny = y + dy; if (nx < 0 || nx >= grid[0].Length || ny < 0 || ny >= grid.Length) { continue; @@ -98,7 +87,7 @@ public class Day4() : Solution(4) private IEnumerable PaddingField(IEnumerable field) { List lines = field.ToList(); - int width = lines.First().Length; + var width = lines.First().Length; lines.Insert(0, new string(NoneChar, width)); lines.Add(new string(NoneChar, width)); return lines.Select(l => NoneChar + l + NoneChar); diff --git a/AdventOfCode2025/Solutions/Day7.cs b/AdventOfCode2025/Solutions/Day7.cs new file mode 100644 index 0000000..ae746ac --- /dev/null +++ b/AdventOfCode2025/Solutions/Day7.cs @@ -0,0 +1,226 @@ +// using System.Collections.Concurrent; +using System.Numerics; + +namespace AdventOfCode2025.Solutions; + +public class Day7() : Solution(7) +{ + private const char Free = '.'; + private const char Start = 'S'; + private const char Split = '^'; + private const char Beam = '|'; + // private readonly ConcurrentStack<(int row, int col)> stack = new(); + // private readonly AutoResetEvent hasWork = new(false); + private char[][] field = null!; + // private int counter = 0; + + public override string SolvePart1() + { + SetupField(); + int indexOfS = field[0].IndexOf(Start); + int count = FirePart1(1, indexOfS); + return count.ToString(); + } + + /* + Row by row count on which cell how many timeline passes... + + .S.. + ..^. + .^.. + + dp[0] = [0, 1, 0, 0] + dp[1] = [0, 1, 0, 0] + + dp[1] = [0, 1, 0, 0] + dp[2] = [0, 1, 0, 0] + + dp[2] = [0, 1, 0, 0] // Split on (2,1) + // Left -> dp[2][0] += 1 + // Right -> dp[2][2] += 1 + dp[2] = [1, 0, 1, 0] + + // Count dp[2] => number of timelines + */ + public override string SolvePart2() + { + SetupField(); + int height = field.Length; + int width = field[0].Length; + + var downwardPropagation = new BigInteger[height][]; + for (var i = 0; i < height; i++) + { + downwardPropagation[i] = new BigInteger[width]; + } + + int startCol = field[0].IndexOf(Start); + downwardPropagation[0][startCol] = 1; + + for (var row = 0; row < height; row++) + { + // downward propagation + if (row > 0) + { + for (var col = 0; col < width; col++) + { + downwardPropagation[row][col] += downwardPropagation[row - 1][col]; + } + } + + var changed = true; + while (changed) + { + changed = false; + for (var col = 0; col < width; col++) + { + if (field[row][col] != Split || downwardPropagation[row][col] <= 0) + { + continue; + } + + BigInteger x = downwardPropagation[row][col]; + downwardPropagation[row][col] = 0; + + if (col > 0) + { + downwardPropagation[row][col - 1] += x; + } + + if (col + 1 < width) + { + downwardPropagation[row][col + 1] += x; + } + + changed = true; + } + } + } + + // final timelines = count on last row + BigInteger result = downwardPropagation[height - 1].Aggregate(BigInteger.Zero, (a, b) => a + b); + return result.ToString(); + } + + // public string SolvePart2_Iterative() // Does take too much tim + // { + // SetupField(); + // int indexOfS = field[0].IndexOf(Start); + // stack.Push((1, indexOfS)); + // int workerCount = Environment.ProcessorCount; + // var workers = new Task[workerCount]; + // var idleWorkersLock = new Lock(); + // int idleWorker = workerCount; + // + // for (var i = 0; i < workerCount; i++) + // { + // int workerId = i; + // workers[i] = Task.Run(() => + // { + // var isIdle = true; + // while (true) + // { + // if (stack.TryPop(out (int row, int col) data)) + // { + // if (isIdle) + // { + // isIdle = false; + // lock (idleWorkersLock) + // { + // idleWorker--; + // } + // + // Console.WriteLine($"Worker {workerId}: started, Idle workers: {idleWorker}"); + // } + // + // FirePart2Worker(data.row, data.col); + // } + // else + // { + // if (!isIdle) + // { + // isIdle = true; + // lock (idleWorkersLock) + // { + // idleWorker++; + // } + // + // Console.WriteLine($"Worker {workerId}: done, Idle workers: {idleWorker}"); + // } + // + // lock (idleWorkersLock) + // { + // if (idleWorker == workerCount) + // { + // return; + // } + // } + // + // hasWork.WaitOne(); + // } + // } + // }); + // } + // + // Task.WaitAll(workers); + // return counter.ToString(); + // } + + private void SetupField() + { + field = Input + .Lines + .Where(line => line.Contains(Start) || line.Contains(Split)) + .Select(line => line.ToCharArray()) + .ToArray(); + } + + private int FirePart1(int row, int col) + { + if (field[row][col] == Beam) + { + return 0; + } + + while (row <= field.Length - 1 && field[row][col] == Free) + { + field[row][col] = Beam; + row++; + } + + if (row > field.Length - 1) + { + return 0; + } + + if (field[row][col] == Split) + { + return 1 + FirePart1(row, col + 1) + FirePart1(row, col - 1); + } + + return 0; + } + + // private void FirePart2Worker(int row, int col) + // { + // while (row < field.Length) + // { + // if (field[row][col] == Split) + // { + // int left = col - 1; + // int right = col + 1; + // row++; + // + // stack.Push((row, right)); + // hasWork.Set(); + // col = left; + // } + // else + // { + // row++; + // } + // } + // + // Interlocked.Increment(ref counter); + // } +} \ No newline at end of file