Browse Source

day 24

main
Ben Harris 6 months ago
parent
commit
92744372ec
Signed by: ben
GPG Key ID: 4E0AF802FFF7960C
  1. 3
      aoc2021.test/DayTests.cs
  2. 53
      aoc2021/Day24.cs
  3. 252
      aoc2021/input/day24.in

3
aoc2021.test/DayTests.cs

@ -44,6 +44,7 @@ public class DayTests
[DataRow(typeof(Day21), "512442", "346642902541848")]
[DataRow(typeof(Day22), "658691", "1228699515783640")]
[DataRow(typeof(Day23), "15365", "52055")]
[DataRow(typeof(Day24), "99299513899971", "93185111127911")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
var s = Stopwatch.StartNew();
@ -72,7 +73,7 @@ public class DayTests
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
Assert.AreEqual(part2, part2Actual, $"Incorrect answer for Day {day.DayNumber} Part2");
}
[DataTestMethod]
[DataRow(typeof(Day01), "7", "5")]
[DataRow(typeof(Day02), "150", "900")]

53
aoc2021/Day24.cs

@ -0,0 +1,53 @@
namespace aoc2021;
/// <summary>
/// Day 24: <see href="https://adventofcode.com/2021/day/24"/>
/// </summary>
public sealed class Day24 : Day
{
private readonly Dictionary<int, (int x, int y)> _keys = new();
public Day24() : base(24, "Arithmetic Logic Unit")
{
var lines = Input.ToList();
var pairs = Enumerable.Range(0, 14)
.Select(i => (int.Parse(lines[i * 18 + 5][6..]), int.Parse(lines[i * 18 + 15][6..])))
.ToList();
var stack = new Stack<(int, int)>();
foreach (var ((x, y), i) in pairs.Select((pair, i) => (pair, i)))
if (x > 0)
stack.Push((i, y));
else
{
var (j, jj) = stack.Pop();
_keys[i] = (j, jj + x);
}
}
public override object Part1()
{
var output = new Dictionary<int, int>();
foreach (var (key, (x, y)) in _keys)
{
output[key] = Math.Min(9, 9 + y);
output[x] = Math.Min(9, 9 - y);
}
return long.Parse(string.Join("", output.OrderBy(x => x.Key).Select(x => x.Value)));
}
public override object Part2()
{
var output = new Dictionary<int, int>();
foreach (var (key, (x, y)) in _keys)
{
output[key] = Math.Max(1, 1 + y);
output[x] = Math.Max(1, 1 - y);
}
return long.Parse(string.Join("", output.OrderBy(x => x.Key).Select(x => x.Value)));
}
}

252
aoc2021/input/day24.in

@ -0,0 +1,252 @@
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 7
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 13
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -6
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 10
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 14
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 0
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -4
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 13
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 15
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 6
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 1
add x 11
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 1
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x 0
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 7
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x 0
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 11
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -3
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 14
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 4
mul y x
add z y
inp w
mul x 0
add x z
mod x 26
div z 26
add x -9
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y 10
mul y x
add z y
Loading…
Cancel
Save