Compare commits

..

7 Commits

Author SHA1 Message Date
f229802a6b day 10 part 2 wip 2026-01-04 12:48:48 -05:00
4b7eee6b24 day 10 part 1 2026-01-04 00:01:19 -05:00
2d7e0f9c15 no idea what i'm doing 2026-01-01 11:17:34 -05:00
7564a3627f day 4 part 2 2025-12-30 18:56:25 -05:00
7460c1b70d day 8 wip 2025-12-26 19:17:41 -05:00
4acdf43eb1 day 8 wip 2025-12-14 15:43:09 -05:00
2c8cf1ebfd day 7 2025-12-14 15:18:04 -05:00
6 changed files with 420 additions and 26 deletions

View File

@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace AdventOfCode.Problems.AOC2025.Day10;
[ProblemInfo(2025, 10, "Factory")]
internal class Factory : Problem<int, int>
{
private List<Machine> _data = [];
public override void CalculatePart1()
{
var results = new int[_data.Count];
Parallel.ForEach(_data, (m, _, i) => results[i] = m.SolveLights());
Part1 = results.Sum();
}
public override void CalculatePart2()
{
var results = new int[_data.Count];
Parallel.ForEach(_data, (m, _, i) => results[i] = m.SolveJoltage());
Part2 = results.Sum();
}
public override void LoadInput()
{
_data = ReadInputLines("input.txt").Select(l => new Machine(l)).ToList();
}
public class Machine
{
public bool[] Target { get; private set; }
public int[][] Operations { get; private set; }
public int[] Requirements { get; private set; }
public Machine(string data)
{
var sections = data.Split(' ');
Target = sections[0][1..^1].Select(c => c == '.' ? false : true).ToArray();
Operations = sections[1..^1].Select(op => op[1..^1])
.Select(op => op.Split(',').Select(int.Parse).ToArray())
.ToArray();
Requirements = sections[^1][1..^1].Split(',').Select(int.Parse).ToArray();
}
public int SolveLights()
{
var state = Enumerable.Repeat(false, Target.Length).ToArray();
var best = Target.Length;
return Solve(Target, Operations, state, ref best);
static int Solve(bool[] goal, int[][] operations, bool[] curState, ref int best, int depth = 0)
{
static bool IsSolved(bool[] goal, bool[] state)
{
for (int i = 0; i < goal.Length; i++)
{
if (goal[i] != state[i])
return false;
}
return true;
}
if (IsSolved(goal, curState))
return depth;
if (depth >= best)
return depth;
var opCount = int.MaxValue;
foreach (var op in operations)
{
var state = new bool[curState.Length];
Buffer.BlockCopy(curState, 0, state, 0, curState.Length);
foreach (var idx in op)
state[idx] = !curState[idx];
var c = Solve(goal, operations, state, ref best, depth + 1);
if (c < best)
best = c;
if (c < opCount)
opCount = c;
}
return opCount;
}
}
public int SolveJoltage()
{
var state = new int[Requirements.Length];
var best = int.MaxValue;
return Solve(Requirements, Operations, state, ref best);
static int Solve(int[] target, int[][] operations, int[] curState, ref int best, int depth = 0)
{
if (depth > best)
return int.MaxValue;
if (IsOver(target, curState))
return int.MaxValue;
if (IsSolved(target, curState))
{
if (depth < best)
best = depth;
return depth;
}
static bool IsOver(int[] target, int[] curState)
{
for (int i = 0; i < target.Length; i++)
{
if (target[i] < curState[i])
return true;
}
return false;
}
static bool IsSolved(int[] target, int[] curState)
{
for (int i = 0; i < target.Length; i++)
{
if (target[i] != curState[i])
return false;
}
return true;
}
var opCount = int.MaxValue;
foreach (var op in operations)
{
var state = new int[curState.Length];
Buffer.BlockCopy(curState, 0, state, 0, curState.Length * sizeof(int));
foreach (var idx in op)
state[idx] += 1;
var c = Solve(target, operations, state, ref best, depth + 1);
if (c < opCount)
opCount = c;
}
return opCount;
}
}
}
}

View File

@@ -1,6 +1,7 @@
using AdventOfCode.Utils.Models; using AdventOfCode.Utils.Models;
using System; using System;
using System.Collections.Frozen;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
@@ -13,36 +14,23 @@ internal class PrintingDeparment: Problem<int, int>
public override void CalculatePart1() public override void CalculatePart1()
{ {
var c = 0; Part1 = GetAccessableRolls(_data, _size).Count;
for (int y = 0; y < _size.Y; y++)
{
for (int x = 0; x < _size.X; x++)
{
var pos = new Vec2<int>(x, y);
if (_data[pos.Y][pos.X] != '@')
continue;
var n = CountNeighbors(pos);
if (n < 4)
c++;
}
}
Part1 = c;
} }
public int CountNeighbors(Vec2<int> pos) public static int CountNeighbors(string[] data, Vec2<int> size, Vec2<int> pos)
{ {
var c = 0; var c = 0;
for (int y = pos.Y-1; y <= pos.Y + 1; y++) for (int y = pos.Y-1; y <= pos.Y + 1; y++)
{ {
if (y < 0 || y >= _size.Y) if (y < 0 || y >= size.Y)
continue; continue;
for (int x = pos.X - 1; x <= pos.X + 1; x++) for (int x = pos.X - 1; x <= pos.X + 1; x++)
{ {
if (x < 0 || x >= _size.X) if (x < 0 || x >= size.X)
continue; continue;
if (pos.X == x && pos.Y == y) if (pos.X == x && pos.Y == y)
continue; continue;
if (_data[y][x] == '@') if (data[y][x] == '@')
c++; c++;
} }
} }
@@ -51,7 +39,50 @@ internal class PrintingDeparment: Problem<int, int>
public override void CalculatePart2() 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<Vec2<int>> GetAccessableRolls(string[] data, Vec2<int> size)
{
var results = new List<Vec2<int>>();
for (int y = 0; y < size.Y; y++)
{
for (int x = 0; x < size.X; x++)
{
var pos = new Vec2<int>(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<int> size, List<Vec2<int>> rolls)
{
var positions = rolls.ToFrozenSet();
return data.Select((row, y) =>
{
var updatedRow = row.Select((col, x) =>
{
var pos = new Vec2<int>(x, y);
if (positions.Contains(pos))
return '.';
else
return col;
}).ToArray();
return new string(updatedRow);
}).ToArray();
} }
public override void LoadInput() public override void LoadInput()

View File

@@ -1,23 +1,111 @@
using System; using AdventOfCode.Utils.Models;
using MaybeError;
using MaybeError.Errors;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Text; using System.Text;
namespace AdventOfCode.Problems.AOC2025.Day7; namespace AdventOfCode.Problems.AOC2025.Day7;
[ProblemInfo(2025, 7, "Laboratories")] [ProblemInfo(2025, 7, "Laboratories")]
internal class Laboratories : Problem<long, long> internal class Laboratories : Problem<long, long>
{ {
private string[] _grid = [];
private Vec2<int> _size;
public override void CalculatePart1() public override void CalculatePart1()
{ {
throw new NotImplementedException(); var start = GetStart(_grid).Value;
Part1 = TraceBeam(start, _grid, _size);
} }
public override void CalculatePart2() public override void CalculatePart2()
{ {
throw new NotImplementedException(); var start = GetStart(_grid).Value;
Part2 = QuantumTraceBeam(start, _grid, _size, []);
} }
private static Maybe<Vec2<int>> GetStart(string[] data)
{
for (int y = 0; y < data.Length; y++)
{
var ln = data[y];
for (int x = 0; x < ln.Length; x++)
{
if (ln[x] == 'S')
return new Vec2<int>(x, y);
}
}
return new Error("There is no start position");
}
private static long TraceBeam(Vec2<int> pos, string[] data, Vec2<int> size)
{
return TraceBeam(pos, data, size, []);
}
private static long TraceBeam(Vec2<int> pos, string[] data, Vec2<int> size, HashSet<Vec2<int>> visited)
{
if (pos.X < 0 || pos.Y < 0)
return 0;
if (pos.X >= size.X || pos.Y >= size.Y)
return 0;
var curPos = data[pos.Y][pos.X];
if (curPos == '^')
{
var left = new Vec2<int>(pos.X - 1, pos.Y);
var right = new Vec2<int>(pos.X + 1, pos.Y);
var leftCount = visited.Add(left) ? TraceBeam(left, data, size, visited) : 0;
var rightCount = visited.Add(right) ? TraceBeam(right, data, size, visited) : 0;
return 1 + leftCount + rightCount;
}
var next = new Vec2<int>(pos.X, pos.Y + 1);
if (visited.Contains(next))
return 0;
visited.Add(next);
return TraceBeam(next, data, size, visited);
}
private static long QuantumTraceBeam(Vec2<int> pos, string[] data, Vec2<int> size, Dictionary<Vec2<int>, long> trace)
{
if (pos.X < 0 || pos.Y < 0)
return 1;
if (pos.X >= size.X || pos.Y >= size.Y)
return 1;
if (trace.TryGetValue(pos, out var c))
return c;
var curPos = data[pos.Y][pos.X];
if (curPos == '^')
{
var left = new Vec2<int>(pos.X - 1, pos.Y);
var right = new Vec2<int>(pos.X + 1, pos.Y);
if (!trace.TryGetValue(left, out long leftCount))
{
leftCount = QuantumTraceBeam(left, data, size, trace);
trace.Add(left, leftCount);
}
if (!trace.TryGetValue(right, out long rightCount))
{
rightCount = QuantumTraceBeam(right, data, size, trace);
trace.Add(right, rightCount);
}
return leftCount + rightCount;
}
return QuantumTraceBeam(new(pos.X, pos.Y + 1), data, size, trace);
}
public override void LoadInput() public override void LoadInput()
{ {
throw new NotImplementedException(); _grid = ReadInputLines("input.txt");
_size = new Vec2<int>(_grid[0].Length, _grid.Length);
} }
} }

View File

@@ -0,0 +1,129 @@
using AdventOfCode.Utils.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace AdventOfCode.Problems.AOC2025.Day8;
[ProblemInfo(2025, 8, "Playground")]
internal class Playground : Problem<long, long>
{
private Vec3i[] _boxPositions= [];
public override void CalculatePart1()
{
var networks = new List<Network>();
var closest = GetClosestPairs(_boxPositions, 1000);
foreach (var (a, b)in closest)
{
var existingNetworkA = networks.FirstOrDefault(n => n.Members.Contains(a));
var existingNetworkB = networks.FirstOrDefault(n => n.Members.Contains(b));
if ((existingNetworkA != null && existingNetworkB == null))
existingNetworkA.AddConnection(a, b);
else if (existingNetworkB != null && existingNetworkA == null)
existingNetworkB.AddConnection(a, b);
else if (existingNetworkA != null && existingNetworkB != null && existingNetworkA != existingNetworkB)
{
existingNetworkA.AddConnection(a, b);
existingNetworkA.MergeWith(existingNetworkB);
networks.Remove(existingNetworkB);
}
else if(existingNetworkA == null && existingNetworkB == null)
{
var newNetwork = new Network().AddConnection(a, b);
networks.Add(newNetwork);
}
}
Console.WriteLine($"Networks: {networks.Count}");
Part1 = networks.Select(n => n.Members.Count)
.OrderDescending()
.Take(3)
.Aggregate((a, b) => a * b);
}
private static List<(Vec3i a , Vec3i b)> GetClosestPairs(Vec3i[] boxes, int count = 10)
{
var distances = new Dictionary<(Vec3i a, Vec3i b), double>();
for (int i = 0; i < boxes.Length; i++)
{
var a = boxes[i];
for (int j = (i + 1); j < boxes.Length; j++)
{
var b = boxes[j];
if (distances.ContainsKey((a, b)) || distances.ContainsKey((b, a)))
continue;
distances.Add((a, b), Math.Sqrt(a.DistanceSq(b)));
}
}
return distances.OrderBy(v => v.Value).Take(count).Select(v => v.Key).ToList();
}
public override void CalculatePart2()
{
throw new NotImplementedException();
}
public override void LoadInput()
{
_boxPositions = ReadInputLines("sample.txt")
.Select(l => l.Split(',').Select(int.Parse))
.Select(c => new Vec3i(c.First(), c.Skip(1).First(), c.Last()))
.ToArray();
}
private record class JunctionBox(Vec3i Pos, HashSet<Vec3i> Connections);
private class Network
{
public HashSet<Vec3i> Members { get; private set; } = [];
public List<JunctionBox> Boxes { get; private set; } = [];
public bool IsConnectedTo(Vec3i pos)
{
return Members.Contains(pos);
}
public bool IntersectsWith(Network other)
{
return Members.Intersect(other.Members).Any();
}
public Network AddConnection(Vec3i a, Vec3i b)
{
if (Members.Contains(a) && Members.Contains(b))
return this;
Members.Add(a);
Members.Add(b);
var boxA = GetOrAddBox(a);
var boxB = GetOrAddBox(b);
boxA.Connections.Add(b);
boxB.Connections.Add(a);
return this;
}
private JunctionBox GetOrAddBox(Vec3i pos)
{
var box = Boxes.FirstOrDefault(box => box.Pos == pos);
if(box == null)
{
box = new JunctionBox(pos, []);
Boxes.Add(box);
}
return box;
}
public Network MergeWith(Network other)
{
foreach (var box in other.Boxes)
{
foreach (var connection in box.Connections)
{
AddConnection(box.Pos, connection);
}
}
return this;
}
}
}

View File

@@ -4,4 +4,4 @@ global using AdventOfCode.Utils;
var runner = new AOCRunner(); var runner = new AOCRunner();
runner.WithDay(7).RenderInteractiveMenu(); runner.RenderInteractiveMenu();

View File

@@ -1,4 +1,10 @@
using System.Numerics; global using Vec3i = AdventOfCode.Utils.Models.Vec3<int>;
global using Vec2i = AdventOfCode.Utils.Models.Vec2<int>;
global using Vec3l = AdventOfCode.Utils.Models.Vec3<long>;
global using Vec2l = AdventOfCode.Utils.Models.Vec2<long>;
global using Vec3f = AdventOfCode.Utils.Models.Vec3<float>;
global using Vec2f = AdventOfCode.Utils.Models.Vec2<float>;
using System.Numerics;
namespace AdventOfCode.Utils.Models; namespace AdventOfCode.Utils.Models;
@@ -36,6 +42,7 @@ public record struct Vec2<T>(T X, T Y) where T : INumber<T>
public readonly Vec2<T> Abs() => new(T.Abs(X), T.Abs(Y)); public readonly Vec2<T> Abs() => new(T.Abs(X), T.Abs(Y));
public override readonly string ToString() public override readonly string ToString()
{ {
return $"({X}, {Y})"; return $"({X}, {Y})";
@@ -64,7 +71,7 @@ public record struct Vec3<T>(T X, T Y, T Z) where T : INumber<T>
var a = other.X - this.X; var a = other.X - this.X;
var b = other.Y - this.Y; var b = other.Y - this.Y;
var c = other.Z - this.Z; var c = other.Z - this.Z;
return (a * a) + (b * b) + (c * c); return T.Abs((a * a) + (b * b) + (c * c));
} }
public readonly Vec3<T> Min(Vec3<T> other) => new(T.Min(X, other.X), T.Min(Y, other.Y), T.Min(Z, other.Z)); public readonly Vec3<T> Min(Vec3<T> other) => new(T.Min(X, other.X), T.Min(Y, other.Y), T.Min(Z, other.Z));