diff --git a/day6/default.nix b/day6/default.nix index 2ee5b1d..7169e9c 100644 --- a/day6/default.nix +++ b/day6/default.nix @@ -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; diff --git a/day8/default.nix b/day8/default.nix new file mode 100644 index 0000000..8b37ed5 --- /dev/null +++ b/day8/default.nix @@ -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)); +} diff --git a/day8/example b/day8/example new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/day8/example @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/day8/input b/day8/input new file mode 100644 index 0000000..1a7b6f2 --- /dev/null +++ b/day8/input @@ -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............. diff --git a/utils.nix b/utils.nix index bf833b6..0bf7d1a 100644 --- a/utils.nix +++ b/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; }