-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.lean
32 lines (28 loc) · 821 Bytes
/
01.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def Input := List Int × List Int
def parse (input : String) : Input :=
input
|>.trim
|>.splitOn "\n"
|>.map (
fun line => line
|>.splitOn " "
|>.filter (not ∘ String.isEmpty)
|>.map String.toInt!
|> fun nums => (nums.head!, nums.getLast!)
)
|>.unzip
def part1 (input : Input) : Nat :=
let fst := input.fst.mergeSort (· ≤ ·)
let snd := input.snd.mergeSort (· ≤ ·)
fst
|>.zip snd
|>.map (fun (a, b) => a - b |>.natAbs)
|>.foldl Nat.add 0
def part2 (input : Input) : Int :=
input.fst
|>.map (fun a => a * (input.snd.count a))
|>.foldl Int.add 0
def main (args : List String) : IO Unit := do
let input ← parse <$> (System.FilePath.mk args[0]! |> IO.FS.readFile)
IO.println $ part1 input
IO.println $ part2 input