-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
45 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
lib: { | ||
abs = a: | ||
if a < 0 | ||
then -a | ||
else a; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters