From a0a3593bd5b4689ddbccab50fc76df3dadcd15b5 Mon Sep 17 00:00:00 2001 From: Grimmauld Date: Sat, 7 Dec 2024 00:34:32 +0100 Subject: [PATCH] day 6 --- day6/default.nix | 62 ++++++++++++++++++++++ day6/example | 10 ++++ day6/input | 130 +++++++++++++++++++++++++++++++++++++++++++++++ utils.nix | 18 ++++++- 4 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 day6/default.nix create mode 100644 day6/example create mode 100644 day6/input diff --git a/day6/default.nix b/day6/default.nix new file mode 100644 index 0000000..2ee5b1d --- /dev/null +++ b/day6/default.nix @@ -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; +} diff --git a/day6/example b/day6/example new file mode 100644 index 0000000..a4eb402 --- /dev/null +++ b/day6/example @@ -0,0 +1,10 @@ +....#..... +.........# +.......... +..#....... +.......#.. +.......... +.#..^..... +........#. +#......... +......#... diff --git a/day6/input b/day6/input new file mode 100644 index 0000000..1f20b72 --- /dev/null +++ b/day6/input @@ -0,0 +1,130 @@ +..........#.............................#...............#...........#....................#...........#............................ +..............................#.......#...#.....#.....#...............#.#..#.......................#............#................. +.............#...........#.#........#............#..........................#.........#.......#......#............................ +......................................................#....#..#.............#................................#...#............##.. +...............#.....#....................................#......................................................#................ +..................##............#......#...............................................................#...................#...... +.#......#...........................................................#............................#................................ +#..#.............#...............................#.....#....#.............................#....#..............................#... +#...##..........#.........................#.................................................#............#...............#........ +............#...............#..............................................#.................................#..........##........ +..............#...................#......##...#.....#....................#.......................................................# +.#.....#....#......................#...#........................................#.#.............#.........#....................... +#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................ +...............#...............................#.....#.................................................#..##...................... +......#...................#..........#...........................................#....#........................................... +...............................................#...#....##.................................................#.....#................ +.................#.........#..............................#........#...#...#...................................#.................. +...........#..#.....#...#.........................#..........#.#.............#....#...................##.......................... +.........................................#.........................#.#............#...#...##............................#......... +.....#......................................#....................................................#..............#................. +..................................................#..................#................#.............#.#..#..................#..... +............................................#.....#......................#........#..........................................#.... +.......#..........................#...#...................................#.........................#...#..........#......#....... +............#...........##........................................................................#......................#........ +#.#...............................................#........#..........#.#..#...#................................#................. +..#.........................#.#........................#....##..................................................#............#.... +.....................................................................................#...............#...........#..........#..... +....................#..........#................................#...............................................#................. +#..............#.#...........#.........#..........#..........................#......................##........#.........#......... +....#....#...............................................................................#...#...........#............#...#...#... +.......................................................##......................#...#................................#............. +.......#...........##...........................#..............#..............................#...................#......#........ +......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#. +....#.............................................#........#....#..................................#.............................. +..........#......#...........#....................................#..............#.......#...#............#....................... +...........................................................................................#.......#.............................. +.#..................................................#.............................#..................#...................#........ +......#..#.#................#.....................#..................................#.............#...............#.............. +...............................#..................#........#...................................................................... +......................#..................................................................................#.........#.............. +...............................................................................................................................#.. +.......#..............#...........................................................#......................#.......................# +#...............................#..............................#.......#..............................#...#...#........#.......... +............................#....#...#.........#....#................................................................#............ +.....................#.................................................................#..#....#..................#............... +......................#.............#.............................#...................................................#........... +....#.................#.#.........##................#.....#....................................................#.............#.... +...............................#...#...................#.......................................#......#........................... +....#............................................................................................................................. +...#.......................................................................................................#............#.......#. +..#.............##.............................................................#...#............................#............#.... +......................#.......#............#........................................................#..................#......#... +...................#.............#..................#..............................#............#........#...................#.... +....................#...........#.........................................................#......#.......#...........#.....##..... +...........#.........................................#.............................#..............#..................#............ +......................#.........#...................#......#...........................................#.......................... +..#....#...#.............................#..............#.....................#............#...................................#.. +.....#.............................#....#..................................#...................#...#.............#................ +...................#...#...........#........................#.............#........................#...#.............#..........#. +...#...........#......#.......#..............#......#..........#.....#............................................................ +..#......................................................#......#...........................#...........#......#..#............... +........#............................#.#....................#...#...#......#.....#...............................................# +.............#....#........#.........................................................................................#.........#.. +....................................................................................................#............................. +..#........................#....#............................................................................#.................... +.......................................#.....#.#.......................................#......#........#.#...................#.... +.................#........#....................................#...#..#................................#.......................... +...........#.................................................................................................#.................... +......................#..................#...#.............#................##................#.......#...........#.........#..... +............#..#................#.............................................#............##..................##..........#...... +......#.#.......................#............#...........................................................#........................ +#......................................#.#......^...............................................#................................. +........#...........................................................#...............#............................................. +...#.............................................#.................#...............#.............................#...#.#.......... +....................#........#...........##.......................#.................#..................#................#......... +#................................................................................................................................. +............#.......................................................................................#.#.........#..............#.. +...............#....#............#..................................................#......##.................................#... +.............................#.........#...............#......#.......#.................#........................#........#....... +......................................................#........................................................#.................# +.................#.#...............#......................................................................................#....#.. +........#.............#......................#...........#...........#......................................#..............#...... +......................#......................#...........#.....#.....##............................................#....#......... +.............#................................................................................#..........#.....#.................. +............##...........................#.................................#..#.#....................................#............ +..............................................................................................#............................#...... +...#.............................#..#..#....................................#..............#....#................................. +.....#..........................................................#.....#......#.........#.......................................... +....##......#.....................#..........#..............#..................#.#..............#........#...........#....#....... +.....#...#.........#............#............#.............................................................#...................... +.........#...................................#...........#.....................................##.....#............#.............. +.....##.#............##.....................#..........................#.................................#........#............... +.......................#..............##...#...................................................#.........#........................ +..#........#.......#...#..##............#....................................................................#...........#.....#.. +..#.....................................................................#..............#.............#...................#........ +...........#..........#..........#.....#.................................................................#..#..................... +..........#................#........................................................##.#...............................#.#......#. +#............................................#...........................##...#.......................#.............#............. +..#............#........................................................................................................#......... +....................................................#....................#............#...............#..........................# +........................................................................#.......#.....................................#.#......... +................#..#......................#...........................................#........................................... +........................##............................................#.............##..........................#.......#....#.... +.........#.......#..............................#......#....#............#..................#...#................................. +#.........#............................#....#.#..#.........................................................##..................... +.#..##...............#....#.............................#...............#........#.......................................#......#. +...#............................................................#.....................#.................#.....#................... +...#...........#..#.............................#.....#.............................................................#............. +...#.........................#..............................................................................................##.... +.........................#...........................#.......................#...........#........................................ +.#..................................#............................................................................................. +....................................................................#...........##.....#.#.........#.....#........................ +................#...............................#........#........................................................................ +......#....#.................................#...................................#.#.........#.....#....................#......... +................#..............................#.......................#...#.....#.......#................#....................... +....................#......................................#..#...........#.#.................#...#.......................#....... +.............#..#...#............................#.#...........................................#.......................#.......... +.........#..........#.................#....................................#.#..........................................#......... +....#.............#......................................................#...........#...........................#................ +........#.#............................#...........................................#.................#..............#..........#.. +..............#..........................#............#.........#....#...#.....#.........................#....#...............##.. +...................#.........................................................#.......#........#..............#..........#......... +.#........#....#..................................#..#.#...............#............#...........#................................. +...............................................................................................#.....#....#.............#......... +...........#..........#...........................................#.........#..............#................#..........#.......... +............#.................................###..............#.#...............................................................# +......#...................................................................................#............#...#..........#.....#..... +.............#..................#...................#......#.........#..#.......................#....#................#..........# +..#....##................................#.................#...........................#.......#....#...#..........#.............. +...............................#......................................#.........#.............#......................#..........#. diff --git a/utils.nix b/utils.nix index a413ee3..bf833b6 100644 --- a/utils.nix +++ b/utils.nix @@ -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. }