You arrive at the first floor of this new building to discover a much less welcoming environment than the shiny atrium of the last one. Instead, you are in a maze of twisty little cubicles, all alike.
Every location in this area is addressed by a pair of non-negative integers (x,y
). Each such coordinate is either a wall or an open space. You can't move diagonally. The cube maze starts at 0,0
and seems to extend infinitely toward positive x
and y
; negative values are invalid, as they represent a location outside the building. You are in a small waiting area at 1,1
.
Read the full puzzle.
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode.Y2016.Day13;
[ProblemName("A Maze of Twisty Little Cubicles")]
class Solution : Solver {
public object PartOne(string input) =>
Steps(int.Parse(input))
.First(s => s.icol == 31 && s.irow == 39)
.steps;
public object PartTwo(string input) =>
Steps(int.Parse(input))
.TakeWhile(s => s.steps <= 50)
.Count();
IEnumerable<(int steps, int irow, int icol)> Steps(int input) {
var q = new Queue<(int steps, int irow, int icol)>();
q.Enqueue((0, 1, 1));
var seen = new HashSet<(int, int)>();
seen.Add((1, 1));
var n = (
from drow in new[] { -1, 0, 1 }
from dcol in new[] { -1, 0, 1 }
where Math.Abs(drow) + Math.Abs(dcol) == 1
select (drow, dcol)
).ToArray();
while (q.Any()) {
var (steps, irow, icol) = q.Dequeue();
yield return (steps, irow, icol);
foreach (var (drow, dcol) in n) {
var (irowT, icolT) = (irow + drow, icol + dcol);
if (irowT >= 0 && icolT >= 0) {
var w = icolT * icolT + 3 * icolT + 2 * icolT * irowT + irowT + irowT * irowT + input;
if (Convert.ToString(w, 2).Count(ch => ch == '1') % 2 == 0) {
if (!seen.Contains((irowT, icolT))) {
q.Enqueue((steps + 1, irowT, icolT));
seen.Add((irowT, icolT));
}
}
}
}
}
}
}
Please ☆ my repo if you like it!