This commit is contained in:
Grimmauld 2024-12-07 00:34:32 +01:00
parent 85ecd2cfd0
commit a0a3593bd5
Signed by: Grimmauld
SSH key fingerprint: SHA256:Q8IL6Y7sSKqzkyFdV1L0O/EflEh1fFV3tBtwxpapRH4
4 changed files with 219 additions and 1 deletions

62
day6/default.nix Normal file
View file

@ -0,0 +1,62 @@
{ lib, utils, ... }:
with lib;
with builtins;
with utils;
let
grid = map stringToCharacters (readLines ./input);
rotMat = [ [ 0 1 ] [ (-1) 0 ] ];
state = let
r = findFirst (pipe' [ (elemAt grid) (elem "^") ]) 0 (listRange grid);
c = let row = elemAt grid r;
in findFirst (pipe' [ (elemAt row) (eq "^") ]) 0 (listRange row);
in rec {
pos = [ r c ];
direction = [ (-1) 0 ];
path = [ pos ];
cyclic = false;
turns = { }; # only turns are relevant to check cycles
};
raycastUntil = pred: direction: pos:
if pred pos then
[ ]
else
[ pos ] ++ (raycastUntil pred direction (vecSum pos direction));
constructPath = state: extraObstruction:
let
isBlocked = pos: (matIndex pos grid) == "#" || pos == extraObstruction;
# batch pathing via "ray cast" to reduce required recursion depth to something the interpretter likes
pathAddition =
raycastUntil (pos: !inRangeMatrix pos grid || isBlocked pos)
state.direction state.pos;
pos = last pathAddition;
turnEntry = "${toString pos} ${toString state.direction}";
in if (inRangeMatrix (vecSum pos state.direction) grid)
&& !state.cyclic then
(constructPath {
direction = matVecMul rotMat state.direction;
inherit pos;
path = state.path ++ pathAddition;
cyclic = state.cyclic
|| (hasAttr turnEntry state.turns); # logarithmic lookup
turns = state.turns // { "${turnEntry}" = null; };
} extraObstruction)
else {
inherit pos;
inherit (state) direction cyclic turns;
path = state.path ++ pathAddition;
};
uniquePositions = unique (constructPath state null).path;
obstructionCandidates = remove state.pos uniquePositions;
in {
part1 = length uniquePositions;
part2 = count (candidate: (constructPath state candidate).cyclic)
obstructionCandidates;
}

10
day6/example Normal file
View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...

130
day6/input Normal file
View file

@ -0,0 +1,130 @@
..........#.............................#...............#...........#....................#...........#............................
..............................#.......#...#.....#.....#...............#.#..#.......................#............#.................
.............#...........#.#........#............#..........................#.........#.......#......#............................
......................................................#....#..#.............#................................#...#............##..
...............#.....#....................................#......................................................#................
..................##............#......#...............................................................#...................#......
.#......#...........................................................#............................#................................
#..#.............#...............................#.....#....#.............................#....#..............................#...
#...##..........#.........................#.................................................#............#...............#........
............#...............#..............................................#.................................#..........##........
..............#...................#......##...#.....#....................#.......................................................#
.#.....#....#......................#...#........................................#.#.............#.........#.......................
#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................
...............#...............................#.....#.................................................#..##......................
......#...................#..........#...........................................#....#...........................................
...............................................#...#....##.................................................#.....#................
.................#.........#..............................#........#...#...#...................................#..................
...........#..#.....#...#.........................#..........#.#.............#....#...................##..........................
.........................................#.........................#.#............#...#...##............................#.........
.....#......................................#....................................................#..............#.................
..................................................#..................#................#.............#.#..#..................#.....
............................................#.....#......................#........#..........................................#....
.......#..........................#...#...................................#.........................#...#..........#......#.......
............#...........##........................................................................#......................#........
#.#...............................................#........#..........#.#..#...#................................#.................
..#.........................#.#........................#....##..................................................#............#....
.....................................................................................#...............#...........#..........#.....
....................#..........#................................#...............................................#.................
#..............#.#...........#.........#..........#..........................#......................##........#.........#.........
....#....#...............................................................................#...#...........#............#...#...#...
.......................................................##......................#...#................................#.............
.......#...........##...........................#..............#..............................#...................#......#........
......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#.
....#.............................................#........#....#..................................#..............................
..........#......#...........#....................................#..............#.......#...#............#.......................
...........................................................................................#.......#..............................
.#..................................................#.............................#..................#...................#........
......#..#.#................#.....................#..................................#.............#...............#..............
...............................#..................#........#......................................................................
......................#..................................................................................#.........#..............
...............................................................................................................................#..
.......#..............#...........................................................#......................#.......................#
#...............................#..............................#.......#..............................#...#...#........#..........
............................#....#...#.........#....#................................................................#............
.....................#.................................................................#..#....#..................#...............
......................#.............#.............................#...................................................#...........
....#.................#.#.........##................#.....#....................................................#.............#....
...............................#...#...................#.......................................#......#...........................
....#.............................................................................................................................
...#.......................................................................................................#............#.......#.
..#.............##.............................................................#...#............................#............#....
......................#.......#............#........................................................#..................#......#...
...................#.............#..................#..............................#............#........#...................#....
....................#...........#.........................................................#......#.......#...........#.....##.....
...........#.........................................#.............................#..............#..................#............
......................#.........#...................#......#...........................................#..........................
..#....#...#.............................#..............#.....................#............#...................................#..
.....#.............................#....#..................................#...................#...#.............#................
...................#...#...........#........................#.............#........................#...#.............#..........#.
...#...........#......#.......#..............#......#..........#.....#............................................................
..#......................................................#......#...........................#...........#......#..#...............
........#............................#.#....................#...#...#......#.....#...............................................#
.............#....#........#.........................................................................................#.........#..
....................................................................................................#.............................
..#........................#....#............................................................................#....................
.......................................#.....#.#.......................................#......#........#.#...................#....
.................#........#....................................#...#..#................................#..........................
...........#.................................................................................................#....................
......................#..................#...#.............#................##................#.......#...........#.........#.....
............#..#................#.............................................#............##..................##..........#......
......#.#.......................#............#...........................................................#........................
#......................................#.#......^...............................................#.................................
........#...........................................................#...............#.............................................
...#.............................................#.................#...............#.............................#...#.#..........
....................#........#...........##.......................#.................#..................#................#.........
#.................................................................................................................................
............#.......................................................................................#.#.........#..............#..
...............#....#............#..................................................#......##.................................#...
.............................#.........#...............#......#.......#.................#........................#........#.......
......................................................#........................................................#.................#
.................#.#...............#......................................................................................#....#..
........#.............#......................#...........#...........#......................................#..............#......
......................#......................#...........#.....#.....##............................................#....#.........
.............#................................................................................#..........#.....#..................
............##...........................#.................................#..#.#....................................#............
..............................................................................................#............................#......
...#.............................#..#..#....................................#..............#....#.................................
.....#..........................................................#.....#......#.........#..........................................
....##......#.....................#..........#..............#..................#.#..............#........#...........#....#.......
.....#...#.........#............#............#.............................................................#......................
.........#...................................#...........#.....................................##.....#............#..............
.....##.#............##.....................#..........................#.................................#........#...............
.......................#..............##...#...................................................#.........#........................
..#........#.......#...#..##............#....................................................................#...........#.....#..
..#.....................................................................#..............#.............#...................#........
...........#..........#..........#.....#.................................................................#..#.....................
..........#................#........................................................##.#...............................#.#......#.
#............................................#...........................##...#.......................#.............#.............
..#............#........................................................................................................#.........
....................................................#....................#............#...............#..........................#
........................................................................#.......#.....................................#.#.........
................#..#......................#...........................................#...........................................
........................##............................................#.............##..........................#.......#....#....
.........#.......#..............................#......#....#............#..................#...#.................................
#.........#............................#....#.#..#.........................................................##.....................
.#..##...............#....#.............................#...............#........#.......................................#......#.
...#............................................................#.....................#.................#.....#...................
...#...........#..#.............................#.....#.............................................................#.............
...#.........................#..............................................................................................##....
.........................#...........................#.......................#...........#........................................
.#..................................#.............................................................................................
....................................................................#...........##.....#.#.........#.....#........................
................#...............................#........#........................................................................
......#....#.................................#...................................#.#.........#.....#....................#.........
................#..............................#.......................#...#.....#.......#................#.......................
....................#......................................#..#...........#.#.................#...#.......................#.......
.............#..#...#............................#.#...........................................#.......................#..........
.........#..........#.................#....................................#.#..........................................#.........
....#.............#......................................................#...........#...........................#................
........#.#............................#...........................................#.................#..............#..........#..
..............#..........................#............#.........#....#...#.....#.........................#....#...............##..
...................#.........................................................#.......#........#..............#..........#.........
.#........#....#..................................#..#.#...............#............#...........#.................................
...............................................................................................#.....#....#.............#.........
...........#..........#...........................................#.........#..............#................#..........#..........
............#.................................###..............#.#...............................................................#
......#...................................................................................#............#...#..........#.....#.....
.............#..................#...................#......#.........#..#.......................#....#................#..........#
..#....##................................#.................#...........................#.......#....#...#..........#..............
...............................#......................................#.........#.............#......................#..........#.

View file

@ -13,9 +13,25 @@ with builtins; rec {
eq = x: y: x == y;
greaterThan = x: y: x > y;
listRange = l: range 0 ((length l) - 1);
transpose = l: map (i: map ((flip elemAt) i) l) (listRange (head l));
invert = f: x: !(f x);
elemAtMid = l: elemAt l ((length l) / 2);
removeAll = rl: (flip pipe) (map remove rl);
pipe' = flip pipe;
inRange = i: l: (i >= 0) && (i < length l);
transpose = l: map (i: map ((flip elemAt) i) l) (listRange (head l));
matVecMul = m: v: map (r: foldl' add 0 (zipListsWith mul v r)) m;
vecSum = zipListsWith add;
matIndex = i: pipe' (map (flip elemAt) i);
inRangeMatrix = i: m:
if i == [ ] then
true
else
(isList m) && (inRange (head i) m)
&& (inRangeMatrix (tail i) (elemAt m (head i)));
getMatrixIndices = m:
crossLists (x: y: [ x y ]) [
(listRange m)
(listRange (head m))
]; # fixme: only 2d, not arbitrary tensors.
}