Skip to content

Commit

Permalink
Nix/2024/01: add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Defelo committed Dec 1, 2024
1 parent d4b8e7f commit bd2ac09
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
29 changes: 29 additions & 0 deletions Nix/2024/01.nix
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 8 additions & 1 deletion Nix/lib/lists.nix
Original file line number Diff line number Diff line change
@@ -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);

Expand Down Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions Nix/lib/math.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lib: {
abs = a:
if a < 0
then -a
else a;
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
|[2021](https://adventofcode.com/2021/leaderboard)|**136**|438|13.41% (of 1014)|0.0532% (of &ge;255548)|
|[2020](https://adventofcode.com/2020/leaderboard)|**621**|46|65.23% (of 952)|0.3146% (of &ge;197402)|

## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 1/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 1/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 1/25)
## [2024](https://adventofcode.com/2024) ([<img height=18 src=".assets/rs.svg"> Rust](Rust/2024): 1/25 | [<img height=18 src=".assets/hs.svg"> Haskell](Haskell/2024): 1/25 | [<img height=18 src=".assets/ua.png"> Uiua](Uiua/2024): 1/25 | [<img height=18 src=".assets/nix.svg"> Nix](Nix/2024): 1/25)
|Mo|Tu|We|Th|Fr|Sa|Su|
|-|-|-|-|-|-|-|
|||||||[**1**](https://adventofcode.com/2024/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2024/01.rs "Rust solution for 2024/01") [<img height=12 src=".assets/hs.svg">](Haskell/2024/01.hs "Haskell solution for 2024/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQCAuKeKJoEBcbi4KCuKKgygvK-KJoSjDly8r4oq4PTop4oqZwqR8LyvijLUt4oip4o2GKcKw4oqf4o2JCg== "Uiua solution for 2024/01")|
|||||||[**1**](https://adventofcode.com/2024/day/1) [<img height=12 src=".assets/rs.svg">](Rust/2024/01.rs "Rust solution for 2024/01") [<img height=12 src=".assets/hs.svg">](Haskell/2024/01.hs "Haskell solution for 2024/01") [<img height=12 src=".assets/ua.png">](https://uiua.org/pad?src=JnNjCgriipwo4oqc4ouV4omgQCAuKeKJoEBcbi4KCuKKgygvK-KJoSjDly8r4oq4PTop4oqZwqR8LyvijLUt4oip4o2GKcKw4oqf4o2JCg== "Uiua solution for 2024/01") [<img height=12 src=".assets/nix.svg">](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)|
Expand Down

0 comments on commit bd2ac09

Please sign in to comment.