feat: Add day 7, thanks TG

This commit is contained in:
Sithis 2025-12-07 21:34:15 +01:00
parent 5f74f2bffa
commit b37717d05f
7 changed files with 406 additions and 33 deletions

View file

@ -0,0 +1,16 @@
.......S.......
...............
.......^.......
...............
......^.^......
...............
.....^.^.^.....
...............
....^.^...^....
...............
...^.^...^.^...
...............
..^...^.....^..
...............
.^.^.^.^.^...^.
...............

View file

@ -0,0 +1,142 @@
......................................................................S......................................................................
.............................................................................................................................................
......................................................................^......................................................................
.............................................................................................................................................
.....................................................................^.^.....................................................................
.............................................................................................................................................
....................................................................^.^.^....................................................................
.............................................................................................................................................
...................................................................^.....^...................................................................
.............................................................................................................................................
..................................................................^...^.^.^..................................................................
.............................................................................................................................................
.................................................................^.^...^.^.^.................................................................
.............................................................................................................................................
................................................................^...^.^.^.^.^................................................................
.............................................................................................................................................
...............................................................^.^.^.^.^.^.^.^...............................................................
.............................................................................................................................................
..............................................................^.^...^.^.^.^...^..............................................................
.............................................................................................................................................
.............................................................^.^.^.^.^.^.^.^.^.^.............................................................
.............................................................................................................................................
............................................................^.^.^.............^.^............................................................
.............................................................................................................................................
...........................................................^.....^.....^.^.^.^.^.^...........................................................
.............................................................................................................................................
..........................................................^...^.^.^.^.^...^.^.....^..........................................................
.............................................................................................................................................
.........................................................^.^.^.^.^.....^.^.^.^.^.^.^.........................................................
.............................................................................................................................................
........................................................^.^...^.....^.^.^.^...^.^.^.^........................................................
.............................................................................................................................................
.......................................................^.^.^.^.^.^.^.........^.^.^.^.^.......................................................
.............................................................................................................................................
......................................................^.^.^.......^.^.^.^.^...^.......^......................................................
.............................................................................................................................................
.....................................................^.^.^.^.^.^.....^...^.^.^.^.^...^.^.....................................................
.............................................................................................................................................
....................................................^.^.^.....^...^.^...^.....^...^.^.^.^....................................................
.............................................................................................................................................
...................................................^...^.^.^.....^.^.^.^.^...^.^.......^.^...................................................
.............................................................................................................................................
..................................................^.^.^.^.^.^.^...^.^.....^.^.......^.^.^.^..................................................
.............................................................................................................................................
.................................................^.^.^.^.^...^.^.^.^.^.^...^.......^.^.^.^.^.................................................
.............................................................................................................................................
................................................^.^.......^.^.^.^.......^.^.^.........^...^.^................................................
.............................................................................................................................................
...............................................^.^.^.^.^.^.....^.^.^.^.^.^.^...^.....^.^.^...^...............................................
.............................................................................................................................................
..............................................^.........^.^...^.^.^.^.^.^.^.....^.....^.^.^.^.^..............................................
.............................................................................................................................................
.............................................^.^...^.^.^.^.^.^...^.^.......^.....^.......^.^.^.^.............................................
.............................................................................................................................................
............................................^...^...^.^.^.^.^.^.....^.^...^.^.^.^.^.^.^.......^.^............................................
.............................................................................................................................................
...........................................^.^.^.^.^.^.^.^.......^.......^.^.^.......^.....^...^.^...........................................
.............................................................................................................................................
..........................................^.^.^.....^.....^...^.^.^.^...^.^.^.^...^.^.^.....^...^.^..........................................
.............................................................................................................................................
.........................................^...^.^.^.^...^.^.^...^.^.^...^.^...^.^...^.^.^.^.^...^...^.........................................
.............................................................................................................................................
........................................^.^.^.^.^.^.^.......^.^.^.^...^.^...^.^.^.^.^.^.....^.^...^.^........................................
.............................................................................................................................................
.......................................^.^.^.^.^...^.^...^.^.^.^.....^...^.^.^.....^.^...^.^.^.^.....^.......................................
.............................................................................................................................................
......................................^...^.^.^.^.^...^.^.^...^...^.^.^.^...^.^...^.^.^.......^...^.^.^......................................
.............................................................................................................................................
.....................................^.^...^.^.^...^.^.^.^.....^.^.^.^.^.^...^...^.^.^.....^...^.^.^.^.^.....................................
.............................................................................................................................................
....................................^.^.^...^...^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.^.^.^.^.^....................................
.............................................................................................................................................
...................................^.^.....^.^.....^.......^.^.^.....^.......^.^.......^...^.^.^.^.^.^.^.^...................................
.............................................................................................................................................
..................................^.^...^.^...........^...^.^.^.^...^...^.^...^.^...^.^.^.^.^.^.^.^.^.^...^..................................
.............................................................................................................................................
.................................^.^.^.^.^...^.^.........^...^.^.........^...^.^.^.^.^.^.^.^.^.^.^.....^.^.^.................................
.............................................................................................................................................
................................^.^.^.^.^.^.^.^.......^.....^.^.^...^...^...^.^.^...^.^.....^.^.^.......^.^.^................................
.............................................................................................................................................
...............................^.^.^...^...^.^.....^.^.^.^.^.....^...^.^.^.^.^.^.^.^.^.^...^.^.....^.^.......^...............................
.............................................................................................................................................
..............................^.^.^.^.^.^...^.^.^.^.^...^.^.^.^.^...........^.^.^.^...^.....^.....^.....^.^...^..............................
.............................................................................................................................................
.............................^...^.^.^.^.....^.....^...^...............^.....^.^...^...^.^.......^.^.^...^.^...^.............................
.............................................................................................................................................
............................^.^.^.......^.^.^.^.....^.^.^.^.^.^...^...^.^.........^.^.^.^.^.^.^.^...^.^.^.....^.^............................
.............................................................................................................................................
...........................^.^.^.....^.^.......^...^.^.....^.^...^.^.^.^...^.^.^.^.^.^.^...^.^.^.^.....^.....^.^.^...........................
.............................................................................................................................................
..........................^.^...^.^.^.^.....^...^...^.^.^...^.^.^.......^.^...^...^...^.^.^.^.^.^...^...^.^.......^..........................
.............................................................................................................................................
.........................^.....^.^.^.....^.^.^...^...^.^.....^.^.^...^.^...^.^.^.^.^.^.^...^.^.......^.^.^...^.^...^.........................
.............................................................................................................................................
........................^.^.....^.......^.^.^.^.^.^.^.......^.^.^.^...^...^.^.^...^...^.^.^.^.^.....^.^.^.^...^...^.^........................
.............................................................................................................................................
.......................^.^.^.^.^...^...^.^.....^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.^...^.^.....^.^.^.^.......................
.............................................................................................................................................
......................^...^...^...^...^.^.^.^...^.^.^.....^.^.^.^.^.^.^.....^...^.^.^.....^...........^...^...^.^...^.^......................
.............................................................................................................................................
.....................^.^.^.^.^.^...^.^.^...^.^.^...^.^...^.....^.^.^.^.^.^.^.^.^...^.^.^.....^.^...^.^.^.^...^.^.^.^.^.^.....................
.............................................................................................................................................
....................^...^.........^.^...^...^...^...^.^...^.^.^...^.^.^...............^...^.....^.......^.........^.^.^.^....................
.............................................................................................................................................
...................^.^.^.^.^.......^.^.^.^.^.....^.......^.^.^...^...^.^.^...^.^.^.^.^.^.^.^.^.......^.^.^.^.^.^.^.....^.^...................
.............................................................................................................................................
..................^.........^...^...^...^.^.^.^.^...^.^...^.^.....^.....^.^.^.^.^.....^...^.^.^...^.^...^.^.^.....^.......^..................
.............................................................................................................................................
.................^.^.^.^.^.^...^.^...^.....^.^...^.^.^.^.^.^.^.^.^...^.^.^.^.....^.^.^.....^...^...^...^...^.^.....^...^...^.................
.............................................................................................................................................
................^.^.^.^...^.^.^.^.^...^.^.^.^.^.^...^...^...^...^.^.^.^.^.^.^.^.....^.^.^...^...^.^.^.^...^.^.^.^.^.^.^.^.^.^................
.............................................................................................................................................
...............^...^.^.^.^.^...^.^.^...^.^.^.^.^.^.^.^...^.^...^...^.^.^.^...^...^.^.^.^...^...^.....^.^...^.^.^.^.^.....^.^.^...............
.............................................................................................................................................
..............^...^...^.^.^.^...^.^...^...^.....^...^...^.^...^...^...^.^...^...^.^...^.^...^...^.^.^.....^.^...^.........^...^..............
.............................................................................................................................................
.............^.^.^...^.....^.^.^.....^.....^.......^...^.^.^...^.^.^.^.^...^...^.^.^.^...^.^...^.^.^...^...^.^.^...^.^.^...^...^.............
.............................................................................................................................................
............^.......^.^.^.^.^.......^.^.^.^.^.^.^.^.^.^.^.^.....^...^.^.^.^.^.^.^.^.........^...^.^.^.^.^.^.....^.^.^...^.^.^...^............
.............................................................................................................................................
...........^.^.^...^.^.^.^.^.^...^.^.^.^.^.^.^.^.^.^...^...^.^.^.^.^.^.^.^.....^.^.^.^...^...^.^.^.....^.^.....^.^.^.^.^...^...^.^...........
.............................................................................................................................................
..........^.^.^...^.^.^.^.^.^...^.^.^.....^.^.^.^...^...^.^.^.^.^.^.^.^.^.^.....^.^.^...^.^.^.^.^...^.....^.^.......^.^.^.....^...^..........
.............................................................................................................................................
.........^...^...^.^...^.^.^.^.^.^.^.^.....^.^...^...^...^.^.....^...^.^.^.^...^.^.^.^.^...^.^.^.........^.^.^.^.....^.....^...^...^.........
.............................................................................................................................................
........^.^...^...^.....^.^.^.^.^...^.^.^.^.....^.^...^.^.^.^.^...^.^...^...^.^.^.......^.......^.^.^.^.^...^.^.^.....^...^.^.....^.^........
.............................................................................................................................................
.......^.^.^.^.^.^.^.^.^...^.^.^.^.^.....^...^.....^.^.^...^.....^...^.....^.^...^.^.^.^.^.^.^.....^.^.^.^.^.....^.^...^.^.^.^...^.^.^.......
.............................................................................................................................................
......^.^...^.^.^.^.........^.^.....^.^.^.^...^.^.^.^.^.^...^...^.^.^.^.......^.^.^...^...^.^.^.^...........^.^.^...^.^.^...^.^.^.^.^.^......
.............................................................................................................................................
.....^.^.^.^.^.^.^.^.^.^.......^.^.^.....^.^.^.^.^...^...^.^...^.^.^.^.^.^.^.^...^.^...^.^.^...^...^.^.^.^.^.....^.^.^.^.^...^.^...^...^.....
.............................................................................................................................................
....^...^.^.^.^.^.^.^.^...^.^...^...^.^.^.^.....^.^.^.^.....^.^.^.^.^...^.^.^...^.^.^.^.^.^.....^.^.^.^...^...^.^.....^.^.^...^.^.^.^.^.^....
.............................................................................................................................................
...^.^.^.^.^...^.^.^...^.^.^.^.^...^.^.^...^.^...^.^...^.^.^.^.^.^.^...^...^.^.^.^...^...^.^...^...^.^.^.^.^.^.^.^...^.^.^...^.^...^.^...^...
.............................................................................................................................................
..^.^.^.^.....^.........^...^.....^...^.....^.^.^.....^...^.^.^.^.^...^.^.^.^.^...^.....^.^...^.^...^.^.^...^.^.^.^.....^.^.^...^.^...^.^.^..
.............................................................................................................................................
.^.^.^.^.^.....^.^.^.^.^.^.....^...^...^.^.^.^...^.^.^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^...^.^...^.....^.....^.^.^...^.^.^.^.^...^.^.^.
.............................................................................................................................................

View file

@ -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<string> Lines => field ??= File.ReadAllLines(ToPath());
public List<string> Lines => field ??= File.ReadAllLines(ToPath()).ToList();
}

View file

@ -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,

View file

@ -30,8 +30,8 @@ public class Day2() : Solution(2)
private static IEnumerable<long> 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]);
}

View file

@ -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<string> PaddingField(IEnumerable<string> field)
{
List<string> 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);

View file

@ -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);
// }
}