mirror of
https://github.com/parnic/advent-of-code-2022.git
synced 2025-06-16 13:40:13 -05:00
Day 24 solution
This is super slow, but it works. Talked through some speedup ideas with @tocchan that I'm going to try next. Gonna be a BFS pro by the end of this advent...
This commit is contained in:
@ -95,6 +95,8 @@
|
|||||||
<EmbeddedResource Include="inputs\23b.txt" />
|
<EmbeddedResource Include="inputs\23b.txt" />
|
||||||
<EmbeddedResource Include="inputs\24.txt" />
|
<EmbeddedResource Include="inputs\24.txt" />
|
||||||
<None Remove="inputs\25.txt" />
|
<None Remove="inputs\25.txt" />
|
||||||
|
<EmbeddedResource Include="inputs\24a.txt" />
|
||||||
|
<EmbeddedResource Include="inputs\24b.txt" />
|
||||||
<EmbeddedResource Include="inputs\25.txt" />
|
<EmbeddedResource Include="inputs\25.txt" />
|
||||||
<None Remove="inputs\02a.txt" />
|
<None Remove="inputs\02a.txt" />
|
||||||
<EmbeddedResource Include="inputs\02a.txt" />
|
<EmbeddedResource Include="inputs\02a.txt" />
|
||||||
|
292
inputs/24.txt
292
inputs/24.txt
@ -1,255 +1,37 @@
|
|||||||
inp w
|
#.####################################################################################################
|
||||||
mul x 0
|
#<v>v><>>>v>v^<<<^><^>>^..>^^^><vv^>^><<.<^<^v<v^><<.v^>^vv.^v^>^>.^>^<<v^>v>v>>v><^..^.^<v^.>v..^v^>#
|
||||||
add x z
|
#><v^v.v.<>>><v<>.^^>>^^>^>><<v.v.^<<<<<><<<^v><v<<v^><.<v<v^^v^v^^^<^v><>>.v.vvv<>v<<v<<<<vv>>><^^^>#
|
||||||
mod x 26
|
#><v^>.v.<>>vvv^.>^<v>v<^^^.^.><^vv<v>>^..>^>>>><v.<^v^><^^v>^v<v><vv>^>v<<>>.^>^^^>>vv>v>..^>..v<<<<#
|
||||||
div z 1
|
#<^>v><v<^^vv<<>>^<<^^<^^^^<.<..<v<>..>v..^v<<vvv>.v<v<.^^>^<<>>^^>.<^^>><>^<.><v<>vv.^<^^^.v<^>v^^^>#
|
||||||
add x 11
|
#>vv^<^.^^^.^.><<<^vv>^<^<>v>>^^^.v<><v^<v<v<^^>><>vv.v.<<>>><<^<v^>><<.><><<>^v^<>v>^vv>^><>.v><v>.<#
|
||||||
eql x w
|
#<^<v>^v<>^<<<vvv<^^^><.v<<vv>v.>^v^^^^>v><^>v<^^.v^^v<>v.>><.<<^^>>.^<>>v^^v<.><<>v>^^^v>v^<v>><v^<.#
|
||||||
eql x 0
|
#<v<>^<>.<.<vvv.>^^v^v^.v>>>v>.^.vv<^^^>^<>>.<^<<v<v^>><<>^^vv><<<<vv.^^>><^v><vv^v^>>^>v>v>^<>v^v<><#
|
||||||
mul y 0
|
#<>><><^><v^.<..^vv^^v><.v<vv<<>^v^>><>v>^>.<.>>.v<^v^v.^<<<.v^.<>>.^v<..^.v^.v^v^^^>>^vvv.<.^<<>.<<>#
|
||||||
add y 25
|
#><>>vv^v^.>v^^v<vv><<.>v<^><^v>><vvv>.^v>><>^.>><^<>^>^>v>^>^^.>><^>^..<^<><^^<^><><<v><.>.<.<<v^.^>#
|
||||||
mul y x
|
#>^>v^v>v<^v>>^vv^v<vv<^^<>.>^.vv^>>^<^>^^vvv^>v^^^><.<vv>>><>^.v<^<<<.v<<<<<>.v>>^>^vv>^^^^>^><^v^^.#
|
||||||
add y 1
|
#>^v<>v<v>><.vv^^>>.>v^>>^.>>^><<^<<v^>>>v^><>.^v.v^^>v><^v<><^.^vv.vv.v>.v<v<><^<v<v^>^>>vv<>^^v^>^>#
|
||||||
mul z y
|
#<<>vv<.>v<v>.<<^<>v>.^..<^><<>>v>^>v<>v^<>.vv>v>>^.>>>>v<^^.>^^<<v^<^^>.<^v^>.v^v<^<.<<v.v<<<^v^<<><#
|
||||||
mul y 0
|
#><v^^v>>vv>^.<vv<^v^..^<<.>^<^.v><>>vv<.^<>.<.v.v<vv^>>v^<>^<^v.^<><<>^^^.><>vv>><.^v^^<^v<^vv>>v^<>#
|
||||||
add y w
|
#>^>>v<^<<^>><<^.^>>>v^>vv<<<><^^>><v>>>>.v<v>.<v^v.^v>^^>^>^^>.^<>><<vvvv>^<^<.><v<.^^>.^^^<^vvvv<>>#
|
||||||
add y 6
|
#>.>>>v^>>>^.^<^>^<v>.v.^.^><^><^^v<v>.v<<<>>>v>v^v^>v^<>^>v<<^>v^^><<<>v^v.^^.^.v>vv>.^>v><v><v<>v<>#
|
||||||
mul y x
|
#>v<>^<^.<<v><>vv^>>^^<<^^>v><><v<><<^><<>v<<<^^^>v>>v.v<v>><<^^^<^<vvv>v^^>>^><<>>v<v^v>>><.>.>v>>><#
|
||||||
add z y
|
#<v^<<vv^v^>^<.^>v<^v><^^<v>>v.<^<^v^^.>^v>v^><vvv^.<^<^>v^^>><<v>>><v<^^^^v^v>^>.<>^v>>.<<<^<^v^<^v<#
|
||||||
inp w
|
#>.>v<v.>>><.<^<.^>v><v>.v>^^<<^>v<^<.<>vv<v<vvv<vv><v^>^v>>.v<^><<.<<><>>>>^<<<^.>><><v.<<>vv<><^><>#
|
||||||
mul x 0
|
#>v>vv<<^<<v>^<v>><^<>>><^><v^v^>v>.^<>vv<>v<^.<^<><<.<>v><>><<v.^<><v^^>^><<^v<.^<^>^<v^.v^.<>^v^v>>#
|
||||||
add x z
|
#>v^v.<v^<>v.><.^>v>vv<v^^.><><v>...>^.>^.>>>^^><^>.<><<^.^.vvv^v^<vvv^.>v>.^<<<v<^<.v.>vv<v.^v.>><.<#
|
||||||
mod x 26
|
#>.^.^><^<.<>v><.><<vv^^^>><.>><v.^.^<.<^<^>>>v^vv<v^<^vv<vv^^>^>v><^<.>^<vv<.<<v>>v><v^>^>v^^><^^<^>#
|
||||||
div z 1
|
#<v<^.<v<^.<<>^^v.^^..vv>>vv>><^^>v^>v<.<><>.^>v^vv.<v.v^^^vv<^><v^.<<>^<v>.^<<v>..v><.^v^^<>>^^>.<<>#
|
||||||
add x 13
|
#>v^<<vv<v<<>v<<v>^v>v><^>v><v^v^^<v^vv<v^>v<>vv>^v<<vv.<vv<v^.<<<>>^v<v<v^<.v^.<^<><<<^vvv<<^<>^v^>>#
|
||||||
eql x w
|
#>>>v<^>>v>.<><<<>.<v>>.^vv<<vvv>>vv^.^vv>^vv<v>.v<.vv>v>>>>..vv<vv.v^vv^.vv>^<^>^^...^vv^vv>>^v<v>^<#
|
||||||
eql x 0
|
#>v>.vvv<>v^.^<<v<v^<>v^.>v<^vv.<^<<v^v^^v>v^^^>^><<.^^^^v>vv<<<>.v<v>^>>v>v<>v><^vv.<<^>^v><v>^^>v<>#
|
||||||
mul y 0
|
#<.<>^.<vv^^><<v<.<<^<>v<>v><v>>>.><<<>.^<v<><^vv<.^<.vv<>vv<v^v.^v^vv^^><^<v<>>^<.>vvv<^<<vv><<><^.<#
|
||||||
add y 25
|
#>^vv<.><<^<v.<^>><^>>^^>v>>v.v.^>v>^<^>><^v>.vvvv<>v>^^>vv.<>v>^<<<^.v^v^^^^>^.vv..^^>.>>^><^>v^v>^>#
|
||||||
mul y x
|
#>^<<v<>.><^<>v^<<>^^>v>^>vv^><.v^<>>^v.>^v^<>.v^v<.^>^^v.v<>v<^>>^^>^vv.<v>>^v<<.^><^<^^vv>>vvv^^v..#
|
||||||
add y 1
|
#.<^<v><<>^v.<<.<^vvv>vv>.v>vv<^<<<<<v<<>^vv>vv<v.^<<.^^vv<<><^<.^>^.^>v<<>^>v^^^>^>.<>^v<^<>v<v<^^^<#
|
||||||
mul z y
|
#<>v<><><^.^v.>>>.<^>>^>^>v^<vv^vvv^^<^>^^.^<.<>vv>^<><vv^vv>><><vvv<v>v>^<v>>.v<>v^>^>v.<v.<.>v.>^v<#
|
||||||
mul y 0
|
#>v^>^>v^.<v^<>vv<.^>v.v<v<>v>.^>^>^^><<^^v<>v<^vv><>^^^<<<vv><><<>^<.>.vv^>vv<<<<<v><v.^vv>vvv<v^v>>#
|
||||||
add y w
|
#>vv<<vv.^<<><<<v^^^>^v<>>v^..v.<<^v<<<<^.<<<<^>>v.<^^<<^v>v^><.>v<^>><v<>^.v^vv<<<v<<^>>^^vv>^>v>>v>#
|
||||||
add y 14
|
#>>.^v>>v^v^>^^.v^v<v<v^.<>>^v>>><v<<<>>>vv><vv>>^>^v><>^^>><v.^<^^^.v^v<>>vv>v>^>vv.^v>vv<v>>.>^v<v>#
|
||||||
mul y x
|
#<^<^.>.><v^.v>>v.<<>v>^.v^<<v><><><>^v^v<>^<v><>>.vv<<^v<<>^^v^^^>>.<<^^v<<vv^<v>^>v<v^^v>>.v>vv>><<#
|
||||||
add z y
|
#><v^<^.v<<^^<><>>v.<<<v^<v.^<^>>.>.>^^^^<v<^<>^vvv><><<>>>v^v.^.><>><.^<>..><>^.><<v><.^^>v>^.>>>v>>#
|
||||||
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 14
|
|
||||||
mul y x
|
|
||||||
add z y
|
|
||||||
inp w
|
|
||||||
mul x 0
|
|
||||||
add x z
|
|
||||||
mod x 26
|
|
||||||
div z 26
|
|
||||||
add x -8
|
|
||||||
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 13
|
|
||||||
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 9
|
|
||||||
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 12
|
|
||||||
mul y x
|
|
||||||
add z y
|
|
||||||
inp w
|
|
||||||
mul x 0
|
|
||||||
add x z
|
|
||||||
mod x 26
|
|
||||||
div z 26
|
|
||||||
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 8
|
|
||||||
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 26
|
|
||||||
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 12
|
|
||||||
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 6
|
|
||||||
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 9
|
|
||||||
mul y x
|
|
||||||
add z y
|
|
||||||
inp w
|
|
||||||
mul x 0
|
|
||||||
add x z
|
|
||||||
mod x 26
|
|
||||||
div z 26
|
|
||||||
add x -1
|
|
||||||
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 15
|
|
||||||
mul y x
|
|
||||||
add z y
|
|
||||||
inp w
|
|
||||||
mul x 0
|
|
||||||
add x z
|
|
||||||
mod x 26
|
|
||||||
div z 26
|
|
||||||
add x -8
|
|
||||||
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 -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 10
|
|
||||||
mul y x
|
|
||||||
add z y
|
|
||||||
|
|
||||||
101011000001011100001001101
|
|
||||||
10001011110100101011011101110101
|
|
7
inputs/24a.txt
Normal file
7
inputs/24a.txt
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#.#####
|
||||||
|
#.....#
|
||||||
|
#>....#
|
||||||
|
#.....#
|
||||||
|
#...v.#
|
||||||
|
#.....#
|
||||||
|
#####.#
|
6
inputs/24b.txt
Normal file
6
inputs/24b.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#.######
|
||||||
|
#>>.<^<#
|
||||||
|
#.<..<<#
|
||||||
|
#>v.><>#
|
||||||
|
#<^v^^>#
|
||||||
|
######.#
|
260
src/24.cs
Normal file
260
src/24.cs
Normal file
@ -0,0 +1,260 @@
|
|||||||
|
using aoc2022.Util;
|
||||||
|
|
||||||
|
namespace aoc2022;
|
||||||
|
|
||||||
|
internal class Day24 : Day
|
||||||
|
{
|
||||||
|
private enum cellType
|
||||||
|
{
|
||||||
|
open = 0,
|
||||||
|
wall = 1 << 0,
|
||||||
|
blizUp = 1 << 1,
|
||||||
|
blizRight = 1 << 2,
|
||||||
|
blizDown = 1 << 3,
|
||||||
|
blizLeft = 1 << 4,
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[][]? grid;
|
||||||
|
internal override void Parse()
|
||||||
|
{
|
||||||
|
var lines = new List<string>(Util.Parsing.ReadAllLines($"{GetDayNum()}"));
|
||||||
|
grid = new int[lines.Count][];
|
||||||
|
for (int row = 0; row < lines.Count; row++)
|
||||||
|
{
|
||||||
|
var line = lines[row];
|
||||||
|
grid[row] = new int[line.Length];
|
||||||
|
for (int col = 0; col < line.Length; col++)
|
||||||
|
{
|
||||||
|
grid[row][col] = line[col] switch
|
||||||
|
{
|
||||||
|
'#' => (int)cellType.wall,
|
||||||
|
'.' => (int)cellType.open,
|
||||||
|
'^' => (int)cellType.blizUp,
|
||||||
|
'>' => (int)cellType.blizRight,
|
||||||
|
'v' => (int)cellType.blizDown,
|
||||||
|
'<' => (int)cellType.blizLeft,
|
||||||
|
_ => throw new Exception(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[][] deepCopyGrid(int[][] src)
|
||||||
|
{
|
||||||
|
var copy = (int[][])src.Clone();
|
||||||
|
for (int i = 0; i < src.Length; i++)
|
||||||
|
{
|
||||||
|
copy[i] = (int[])src[i].Clone();
|
||||||
|
}
|
||||||
|
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void render(string label, int[][] grid)
|
||||||
|
{
|
||||||
|
Console.WriteLine(label);
|
||||||
|
|
||||||
|
for (int row = 0; row < grid.Length; row++)
|
||||||
|
{
|
||||||
|
for (int col = 0; col < grid[row].Length; col++)
|
||||||
|
{
|
||||||
|
if (grid[row][col] == (int) cellType.wall)
|
||||||
|
{
|
||||||
|
Console.Write('#');
|
||||||
|
}
|
||||||
|
else if (grid[row][col] == (int) cellType.open)
|
||||||
|
{
|
||||||
|
Console.Write('.');
|
||||||
|
}
|
||||||
|
else if (grid[row][col] == (int) cellType.blizRight)
|
||||||
|
{
|
||||||
|
Console.Write('>');
|
||||||
|
}
|
||||||
|
else if (grid[row][col] == (int) cellType.blizLeft)
|
||||||
|
{
|
||||||
|
Console.Write('<');
|
||||||
|
}
|
||||||
|
else if (grid[row][col] == (int) cellType.blizUp)
|
||||||
|
{
|
||||||
|
Console.Write('^');
|
||||||
|
}
|
||||||
|
else if (grid[row][col] == (int) cellType.blizDown)
|
||||||
|
{
|
||||||
|
Console.Write('v');
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int numSet = 0;
|
||||||
|
var enumVals = Enum.GetValues<cellType>();
|
||||||
|
for (int i = 2; i < enumVals.Length; i++)
|
||||||
|
{
|
||||||
|
if ((grid[row][col] & (int)enumVals[i]) != 0)
|
||||||
|
{
|
||||||
|
numSet++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Console.Write($"{numSet}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int[][] advanceSim(int[][] grid)
|
||||||
|
{
|
||||||
|
var next = deepCopyGrid(grid);
|
||||||
|
for (int row = 1; row < grid.Length - 1; row++)
|
||||||
|
{
|
||||||
|
for (int col = 0; col < grid[row].Length; col++)
|
||||||
|
{
|
||||||
|
if (grid[row][col] == (int)cellType.wall)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (grid[row][col] == (int)cellType.open)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var isRight = (grid[row][col] & (int) cellType.blizRight) != 0;
|
||||||
|
var isLeft = (grid[row][col] & (int) cellType.blizLeft) != 0;
|
||||||
|
var isUp = (grid[row][col] & (int) cellType.blizUp) != 0;
|
||||||
|
var isDown = (grid[row][col] & (int) cellType.blizDown) != 0;
|
||||||
|
if (isRight)
|
||||||
|
{
|
||||||
|
var nextCol = col + 1;
|
||||||
|
if (grid[row][nextCol] == (int) cellType.wall)
|
||||||
|
{
|
||||||
|
nextCol = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
next[row][col] -= (int) cellType.blizRight;
|
||||||
|
next[row][nextCol] += (int) cellType.blizRight;
|
||||||
|
}
|
||||||
|
if (isLeft)
|
||||||
|
{
|
||||||
|
var nextCol = col - 1;
|
||||||
|
if (grid[row][nextCol] == (int) cellType.wall)
|
||||||
|
{
|
||||||
|
nextCol = grid[row].Length - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
next[row][col] -= (int) cellType.blizLeft;
|
||||||
|
next[row][nextCol] += (int) cellType.blizLeft;
|
||||||
|
}
|
||||||
|
if (isUp)
|
||||||
|
{
|
||||||
|
var nextRow = row - 1;
|
||||||
|
if (grid[nextRow][col] == (int) cellType.wall)
|
||||||
|
{
|
||||||
|
nextRow = grid.Length - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
next[row][col] -= (int) cellType.blizUp;
|
||||||
|
next[nextRow][col] += (int) cellType.blizUp;
|
||||||
|
}
|
||||||
|
if (isDown)
|
||||||
|
{
|
||||||
|
var nextRow = row + 1;
|
||||||
|
if (grid[nextRow][col] == (int) cellType.wall)
|
||||||
|
{
|
||||||
|
nextRow = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
next[row][col] -= (int) cellType.blizDown;
|
||||||
|
next[nextRow][col] += (int) cellType.blizDown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static (int steps, int[][] gridState) getMinSteps(int[][] grid, ivec2 start, ivec2 dest)
|
||||||
|
{
|
||||||
|
Queue<(ivec2 pos, int[][] gridState, int steps)> states = new();
|
||||||
|
states.Enqueue((start, deepCopyGrid(grid), 0));
|
||||||
|
|
||||||
|
int? minSteps = null;
|
||||||
|
int[][]? minGridState = null;
|
||||||
|
|
||||||
|
// render("Start:", p1grid);
|
||||||
|
while (states.TryDequeue(out var q))
|
||||||
|
{
|
||||||
|
if (minSteps != null && q.steps > minSteps)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var next = advanceSim(q.gridState);
|
||||||
|
// check if we can wait
|
||||||
|
if (next[q.pos.y][q.pos.x] == (int) cellType.open)
|
||||||
|
{
|
||||||
|
var nextState = (pos: q.pos, next: next, steps: q.steps + 1);
|
||||||
|
if (!states.Any(s => s.pos == nextState.pos && s.steps == nextState.steps))
|
||||||
|
{
|
||||||
|
states.Enqueue(nextState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// queue up all neighbor possibilities
|
||||||
|
foreach (var n in q.pos.GetOrthogonalNeighbors())
|
||||||
|
{
|
||||||
|
if (n == dest)
|
||||||
|
{
|
||||||
|
if (minSteps == null || q.steps + 1 < minSteps)
|
||||||
|
{
|
||||||
|
minSteps = q.steps + 1;
|
||||||
|
minGridState = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n.x < 0 || n.y < 0 || n.x >= next[0].Length || n.y >= next.Length)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (next[n.y][n.x] == (int) cellType.open)
|
||||||
|
{
|
||||||
|
var nextState = (pos: n, next: next, steps: q.steps + 1);
|
||||||
|
if (!states.Any(s => s.pos == nextState.pos && s.steps == nextState.steps))
|
||||||
|
{
|
||||||
|
states.Enqueue(nextState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// render($"After {i+1} minute{(i + 1 == 1 ? "" : "s")}:", p1grid);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (minSteps!.Value, minGridState!);
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string Part1()
|
||||||
|
{
|
||||||
|
var start = new ivec2(1, 0);
|
||||||
|
var dest = new ivec2(grid![0].Length - 2, grid!.Length - 1);
|
||||||
|
|
||||||
|
var (minSteps, _) = getMinSteps(grid!, start, dest);
|
||||||
|
|
||||||
|
return $"Minimum steps to reach the end: <+white>{minSteps}";
|
||||||
|
}
|
||||||
|
|
||||||
|
internal override string Part2()
|
||||||
|
{
|
||||||
|
var start = new ivec2(1, 0);
|
||||||
|
var dest = new ivec2(grid![0].Length - 2, grid!.Length - 1);
|
||||||
|
|
||||||
|
var toEndOnce = getMinSteps(grid!, start, dest);
|
||||||
|
var backToStart = getMinSteps(toEndOnce.gridState, dest, start);
|
||||||
|
var toEndAgain = getMinSteps(backToStart.gridState, start, dest);
|
||||||
|
|
||||||
|
return $"Minimum steps to go there and back again: <+white>{toEndOnce.steps + backToStart.steps + toEndAgain.steps}";
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user