-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay02.lean
More file actions
60 lines (47 loc) · 1.45 KB
/
Copy pathDay02.lean
File metadata and controls
60 lines (47 loc) · 1.45 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
abbrev Report := List Int
inductive Safety
| Safe
| Unsafe
deriving BEq
def parse (content : String) : List Report :=
let helper : String → Report
| "" => [0]
| list => list.splitOn |>.map (·.toInt!)
content
|>.splitOn "\n"
|>.map helper
def checkSafety : Safety → List Int → Nat
| Safety.Safe, _ :: [] => 1
| _, d1 :: d2 :: ds =>
if d1.sign != d2.sign
|| d1.natAbs ∉ [1, 2, 3]
|| d2.natAbs ∉ [1, 2, 3] then 0
else checkSafety Safety.Safe (d2 :: ds)
| _, _ => 0
def part1Aux (report : Report) : Nat := report
|>.zipWith (· - ·) report.tail
|> checkSafety Safety.Unsafe
def part1 (reports : List Report) : Nat := reports
|>.map part1Aux
|>.sum
def part2Aux (report : Report) : Nat :=
let isSafe (r : Report) : Bool := r
|>.zipWith (· - ·) r.tail
|> checkSafety Safety.Unsafe
|> (· == 1)
let rec helper : List Int → List (List Int)
| [] => [[]]
| d :: ds => ds :: (helper ds |>.map (d :: ·))
helper report
|>.any isSafe
|>.toNat
def part2 (reports : List Report) : Nat := reports
|>.map part2Aux
|>.sum
def main : IO Unit := do
let content ← IO.FS.readFile "input2.txt"
let parsed := parse content
let resultPart1 := part1 parsed
let resultPart2 := part2 parsed
IO.println s!"Part 1: {resultPart1}"
IO.println s!"Part 2: {resultPart2}"