diff --git a/Nix/2024/01.nix b/Nix/2024/01.nix new file mode 100644 index 0000000..833d096 --- /dev/null +++ b/Nix/2024/01.nix @@ -0,0 +1,29 @@ +let + lib = import ../lib; + inherit (builtins) match fromJSON head sort lessThan length filter; + inherit (lib.aoc) input solution; + inherit (lib.lists) last zipWith sum; + inherit (lib.functions) flip pipe; + inherit (lib.math) abs; + inherit (lib.strings) splitLines trim; + + sorted = sort lessThan; + + parsed = pipe input [ + trim + splitLines + (map (flip pipe [ + (match " *([0-9]+) *([0-9]+) *") + (map fromJSON) + ])) + ]; + + left = map head parsed; + right = map last parsed; + + count = x: xs: length (filter (a: a == x) xs); +in + solution { + p1 = sum (zipWith (a: b: abs (a - b)) (sorted left) (sorted right)); + p2 = sum (map (l: l * (count l right)) left); + } diff --git a/Nix/lib/lists.nix b/Nix/lib/lists.nix index ecd8165..29df81f 100644 --- a/Nix/lib/lists.nix +++ b/Nix/lib/lists.nix @@ -1,5 +1,5 @@ lib: let - inherit (builtins) elemAt length head tail isList concatMap; + inherit (builtins) elemAt length head tail isList concatMap foldl' add; in rec { last = lst: elemAt lst (length lst - 1); @@ -35,4 +35,11 @@ in rec { if isList lst then concatMap flatten lst else [lst]; + + zipWith = f: xs: ys: + if xs == [] || ys == [] + then [] + else [(f (head xs) (head ys))] ++ (zipWith f (tail xs) (tail ys)); + + sum = foldl' add 0; } diff --git a/Nix/lib/math.nix b/Nix/lib/math.nix new file mode 100644 index 0000000..d3d032f --- /dev/null +++ b/Nix/lib/math.nix @@ -0,0 +1,6 @@ +lib: { + abs = a: + if a < 0 + then -a + else a; +} diff --git a/README.md b/README.md index d680857..9d00b8f 100644 --- a/README.md +++ b/README.md @@ -9,10 +9,10 @@ |[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of ≥255548)| |[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of ≥197402)| -## [2024](https://adventofcode.com/2024) ([ Rust](Rust/2024): 1/25 | [ Haskell](Haskell/2024): 1/25 | [ Uiua](Uiua/2024): 1/25) +## [2024](https://adventofcode.com/2024) ([ Rust](Rust/2024): 1/25 | [ Haskell](Haskell/2024): 1/25 | [ Uiua](Uiua/2024): 1/25 | [ Nix](Nix/2024): 1/25) |Mo|Tu|We|Th|Fr|Sa|Su| |-|-|-|-|-|-|-| -|||||||[**1**](https://adventofcode.com/2024/day/1) [](Rust/2024/01.rs "Rust solution for 2024/01") [](Haskell/2024/01.hs "Haskell solution for 2024/01") [](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQCAuKeKJoEBcbi4KCuKKgygvK-KJoSjDly8r4oq4PTop4oqZwqR8LyvijLUt4oip4o2GKcKw4oqf4o2JCg== "Uiua solution for 2024/01")| +|||||||[**1**](https://adventofcode.com/2024/day/1) [](Rust/2024/01.rs "Rust solution for 2024/01") [](Haskell/2024/01.hs "Haskell solution for 2024/01") [](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQCAuKeKJoEBcbi4KCuKKgygvK-KJoSjDly8r4oq4PTop4oqZwqR8LyvijLUt4oip4o2GKcKw4oqf4o2JCg== "Uiua solution for 2024/01") [](Nix/2024/01.nix "Nix solution for 2024/01")| |[**2**](https://adventofcode.com/2024/day/2)|[**3**](https://adventofcode.com/2024/day/3)|[**4**](https://adventofcode.com/2024/day/4)|[**5**](https://adventofcode.com/2024/day/5)|[**6**](https://adventofcode.com/2024/day/6)|[**7**](https://adventofcode.com/2024/day/7)|[**8**](https://adventofcode.com/2024/day/8)| |[**9**](https://adventofcode.com/2024/day/9)|[**10**](https://adventofcode.com/2024/day/10)|[**11**](https://adventofcode.com/2024/day/11)|[**12**](https://adventofcode.com/2024/day/12)|[**13**](https://adventofcode.com/2024/day/13)|[**14**](https://adventofcode.com/2024/day/14)|[**15**](https://adventofcode.com/2024/day/15)| |[**16**](https://adventofcode.com/2024/day/16)|[**17**](https://adventofcode.com/2024/day/17)|[**18**](https://adventofcode.com/2024/day/18)|[**19**](https://adventofcode.com/2024/day/19)|[**20**](https://adventofcode.com/2024/day/20)|[**21**](https://adventofcode.com/2024/day/21)|[**22**](https://adventofcode.com/2024/day/22)|