Skip to content

Commit

Permalink
Advent of code
Browse files Browse the repository at this point in the history
  • Loading branch information
joinemm committed Dec 15, 2024
1 parent 0f8ef1f commit 08d7f9c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions aoc2024/day1.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let
lib = import <nixpkgs/lib>;

divWith = f: lib.sort (a: b: a < b) (map (x: f x) nums);
abs = x: if x < 0 then (-x) else x;
sum = x: lib.foldl (a: b: a + b) 0 x;

input = lib.removeSuffix "\n" (builtins.readFile ./input.txt);
nums = map (x: map (x: lib.toInt x) (lib.splitString " " x)) (lib.splitString "\n" input);
left = divWith builtins.head;
right = divWith lib.last;
distances = lib.zipListsWith (a: b: abs (a - b)) left right;
similar = map (n: (builtins.mul n (lib.count (x: x == n) right))) left;
in
{
part1 = sum distances;
part2 = sum similar;
}

0 comments on commit 08d7f9c

Please sign in to comment.