day 8
This commit is contained in:
parent
f4976b6f2e
commit
9347e3c4e4
5 changed files with 108 additions and 12 deletions
|
@ -18,12 +18,6 @@ let
|
|||
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;
|
||||
|
|
25
day8/default.nix
Normal file
25
day8/default.nix
Normal file
|
@ -0,0 +1,25 @@
|
|||
{ lib, utils, ... }:
|
||||
with lib;
|
||||
with builtins;
|
||||
with utils;
|
||||
let
|
||||
grid = map stringToCharacters (readLines ./input);
|
||||
antenna_map = genAttrs (unique (filter (invert (eq ".")) (flatten grid))) (c: filter (i: matIndex i grid == c) (getMatrixIndices grid));
|
||||
inGrid = (flip inRangeMatrix) grid;
|
||||
|
||||
makeAntiNodesPairwise = x: y: [ (vecSum x (vecDiff x y)) (vecSum y (vecDiff y x)) ];
|
||||
makeAntiNodesRay = x: y: let direction = (shortenVec (vecDiff x y)); in (raycastUntil (invert inGrid) direction x) ++ (raycastUntil (invert inGrid) (scalarVecMul (-1) direction) y);
|
||||
|
||||
makeAntiNodes = nodeFunc: coords: concatLists (map (a: concatLists (map (nodeFunc a) (remove a coords)) ) coords);
|
||||
in rec {
|
||||
# inherit antenna_map;
|
||||
part1 = map (makeAntiNodes makeAntiNodesPairwise) (attrValues antenna_map) |> concatLists |> filter inGrid |> unique |> length;
|
||||
part2 = map (makeAntiNodes makeAntiNodesRay) (attrValues antenna_map) |> concatLists |> unique |> length;
|
||||
|
||||
# pairwiseTest = makeAntiNodesPairwise [0 0] [2 2];
|
||||
# shortVecTest = shortenVec [2 6];
|
||||
# x = [1 1];
|
||||
# y = [1 2];
|
||||
# rayTest = makeAntiNodesRay x y;
|
||||
# direction = (shortenVec (vecDiff x y));
|
||||
}
|
12
day8/example
Normal file
12
day8/example
Normal file
|
@ -0,0 +1,12 @@
|
|||
............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............
|
50
day8/input
Normal file
50
day8/input
Normal file
|
@ -0,0 +1,50 @@
|
|||
.AU..Z.....8.......................t..C.6.........
|
||||
..................................................
|
||||
.....K.U....................v.....................
|
||||
...Z..A.............................v8.....t......
|
||||
p..................a8...........b...t.............
|
||||
..pU.....A..4.....................................
|
||||
..........................E.......................
|
||||
...........K..V..............v8.....Cb............
|
||||
....V................b...p........................
|
||||
....7.............................................
|
||||
....4.....A..........V......K..E.....6............
|
||||
.4.........................Vb...........0..C......
|
||||
..................................k........N......
|
||||
K....7...........9...........6.....kE.............
|
||||
......7......1...................k.......C........
|
||||
...p....................9....0.....N6.............
|
||||
..........Z........e..1...........................
|
||||
.............................E................N...
|
||||
...4...............................v0..........z..
|
||||
........U.....Z......1................z..a........
|
||||
.....5.......7......................N.............
|
||||
....................n.............................
|
||||
.......................0.9...c..........z.d.T.....
|
||||
...................n.W......a...t......D....d.....
|
||||
..........I.....e......................o9.........
|
||||
....5..2................e...........D.............
|
||||
...........................n......D...............
|
||||
......25I...1..................c......W.......o...
|
||||
................n..............D..................
|
||||
...........I........i..e..........................
|
||||
......5......2.....P..............a...............
|
||||
...........................z..................T...
|
||||
..........j.....................Wd...........O..o.
|
||||
................................................c.
|
||||
.................I................B...............
|
||||
...........u.............................T.d......
|
||||
.............................................J....
|
||||
.....3.i....u......................o..............
|
||||
3...i.............................................
|
||||
..................................................
|
||||
...........j...............W....O............w....
|
||||
...P........................J.....................
|
||||
.....u............................................
|
||||
.............................w....................
|
||||
......u.................2...w...J.................
|
||||
.....j.....B3......................O..............
|
||||
P....B..............................c.............
|
||||
................B.............w...................
|
||||
.....i.............3..............................
|
||||
..P.j....................J..........O.............
|
27
utils.nix
27
utils.nix
|
@ -12,16 +12,20 @@ with builtins; rec {
|
|||
deleteAt = l: i: (take i l) ++ (drop (i + 1) l);
|
||||
eq = x: y: x == y;
|
||||
greaterThan = x: y: x > y;
|
||||
listRange = l: range 0 ((length l) - 1);
|
||||
listRange = l: genList identity (length 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);
|
||||
identity = x: x;
|
||||
|
||||
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;
|
||||
vecDiff = zipListsWith sub;
|
||||
scalarVecMul = i: map (mul i);
|
||||
scalarVecDiv = i: map ((flip div) i);
|
||||
matIndex = i: pipe' (map (flip elemAt) i);
|
||||
inRangeMatrix = i: m:
|
||||
if i == [ ] then
|
||||
|
@ -29,9 +33,20 @@ with builtins; rec {
|
|||
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.
|
||||
|
||||
raycastUntil = pred: direction: pos:
|
||||
if pred pos then
|
||||
[ ]
|
||||
else
|
||||
[ pos ] ++ (raycastUntil pred direction (vecSum pos direction));
|
||||
|
||||
getMatrixIndices = m: let
|
||||
makeLayers = m: if isList m then [ (listRange m) ] ++ (makeLayers (head m)) else [];
|
||||
layers = makeLayers m;
|
||||
in mapCartesianProduct (args@{...}: attrValues args) (listToAttrs (map (k: nameValuePair (toString k) (elemAt layers k)) (listRange layers)));
|
||||
|
||||
gcd = a: b: if a == 0 then b else if b == 0 then a else if a < b then gcd b a else gcd b (mod a b);
|
||||
listGcd = l: foldl' gcd (head l) (tail l);
|
||||
|
||||
shortenVec = vec: scalarVecDiv (listGcd (map abs vec)) vec;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue