96 lines
3.4 KiB
C#
96 lines
3.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using AdvenOfCode.Contracts;
|
|
|
|
namespace AoC_2025;
|
|
|
|
public class Day10 : IPuzzleSolver<long>
|
|
{
|
|
private static int done = 0;
|
|
private (uint lamps, uint[] buttons, int[] joltages)[] ParsePuzzleInput(string path)
|
|
{
|
|
var puzzleInput = File.ReadAllLines(path)
|
|
.Where(line => !string.IsNullOrWhiteSpace(line))
|
|
.Select(line => line.Split(' ', StringSplitOptions.RemoveEmptyEntries))
|
|
.Select(instruction => instruction switch
|
|
{
|
|
[var lamps, ..var buttons, var joltages] => (lamps: lamps[1..^1], buttons, joltages: joltages[1..^1]),
|
|
_ => throw new DataException("Misaligned data")
|
|
})
|
|
.OrderBy(inst => inst.lamps.Length)
|
|
.ToArray();
|
|
var lamps = puzzleInput
|
|
.Select(input => input.lamps)
|
|
.Select(lamps => lamps.Index())
|
|
.Select(ixlamps =>
|
|
ixlamps.Aggregate((uint)0L, (acc, ixLamp) => acc | ((uint)(ixLamp.Item == '#' ? 1L : 0L) << ixLamp.Index)))
|
|
.ToArray();
|
|
var buttons = puzzleInput
|
|
.Select(input => input.buttons)
|
|
.Select(buttons => buttons
|
|
.Select(button => button[1..^1]
|
|
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(lampIndex => int.Parse(lampIndex))
|
|
.Aggregate((uint)0L, (acc, lampIndex) => acc | ((uint)1 << lampIndex)))
|
|
.ToArray())
|
|
.ToArray();
|
|
var joltages = puzzleInput
|
|
.Select(input => input.joltages)
|
|
.Select(joltage => joltage
|
|
.Split(',', StringSplitOptions.RemoveEmptyEntries)
|
|
.Select(jolt => int.Parse(jolt))
|
|
.ToArray())
|
|
.ToArray();
|
|
var res = lamps
|
|
.Zip(buttons, (lamp, button) => (lamp, button))
|
|
.Zip(joltages, (bl, joltage) => (bl.lamp, bl.button, joltage))
|
|
.ToArray();
|
|
return res;
|
|
}
|
|
|
|
public long SolvePart1(string pathToPuzzleInput)
|
|
{
|
|
var instructions = ParsePuzzleInput(pathToPuzzleInput);
|
|
Console.WriteLine($"total: {instructions.Length}");
|
|
var minCounts = instructions
|
|
.Select(ins => GetMinCountButtonPressesForLamps(ins.lamps, ins.buttons))
|
|
.ToArray();
|
|
return minCounts.Sum();
|
|
}
|
|
|
|
public long SolvePart2(string pathToPuzzleInput)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
private static long GetMinCountButtonPressesForLamps(uint lamps, uint[] buttons)
|
|
{
|
|
var presses = 0L;
|
|
HashSet<uint> lampStates = [lamps];
|
|
while (lampStates.All(state => state > 0) && presses < 100)
|
|
{
|
|
lampStates = lampStates
|
|
.Select(lampState => GetLampStates(lampState, buttons))
|
|
.Aggregate((current, states) => [..current,..states])
|
|
.ToHashSet();
|
|
presses++;
|
|
}
|
|
|
|
var amount = Interlocked.Increment(ref done);
|
|
Console.WriteLine($"done: {amount}, presses: {presses}");
|
|
|
|
return presses;
|
|
}
|
|
|
|
private static uint[] GetLampStates(uint lampState, uint[] buttons)
|
|
{
|
|
var states = buttons
|
|
.Where(button => (lampState & button) > 0)
|
|
.Select(button => lampState ^ button);
|
|
return [..states];
|
|
}
|
|
} |