-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.lean
More file actions
30 lines (22 loc) · 1.02 KB
/
Copy pathDay01.lean
File metadata and controls
30 lines (22 loc) · 1.02 KB
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
import Batteries.Data.String
import Mathlib.Data.List.Sort
def parse (content : String) : List Int × List Int :=
let numbers := content
|>.replace " " " "
|>.split (fun c => c == ' ' || c == '\n')
let rec helper : List Int × List Int → List String → List Int × List Int
| res, x1 :: x2 :: xs => helper ⟨x1.toInt! :: res.fst, x2.toInt! :: res.snd⟩ xs
| res, _ => ⟨res.fst.mergeSort, res.snd.mergeSort⟩
helper ⟨[], []⟩ numbers
def part1 (ns : List Int) (ms : List Int) : Nat := ns
|>.zipWith (· - ·) ms
|>.foldl (· + ·.natAbs) (0 : Nat)
def part2 (ns : List Int) (ms : List Int) : Nat := ns
|>.foldl (fun acc n => acc + (n.toNat * (ms |>.filter (· == n) |>.length))) (0 : Nat)
def main : IO Unit := do
let content ← IO.FS.readFile "input1.txt"
let parsed := parse content
let resultPart1 := part1.uncurry parsed
let resultPart2 := part2.uncurry parsed
IO.println s!"Part 1: {resultPart1}"
IO.println s!"Part 2: {resultPart2}"