Skip to content

Commit 2dba885

Browse files
committed
a bit better day1 sol
1 parent 5dca5f7 commit 2dba885

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

Aoc.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-- This module serves as the root of the `Aoc` library.
22
-- Import modules here that should be built as part of the library.
3-
import Aoc.Basic
3+
import Aoc.Day1.Basic

Aoc/1/Basic.lean

Lines changed: 0 additions & 30 deletions
This file was deleted.

Aoc/Day1/Solution.lean

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
instance : Monad List where
2+
pure := List.singleton
3+
bind := List.flatMap
4+
5+
def List.sequence {m α} [Monad m] (lst: List (m α)) : m (List α) := List.mapM id lst
6+
7+
def parse (raw : String) : Option (List (Int × Int)) :=
8+
raw.splitOn "\n"
9+
|> List.map (String.splitOn · " ")
10+
|> List.map (List.map String.toInt?)
11+
|> List.map List.sequence
12+
|> List.filter Option.isSome
13+
|> List.sequence
14+
|> Option.map (List.map (λ x => match x with | [a, b] => some (a, b) | _ => none))
15+
|> Option.map List.sequence
16+
|> Option.join
17+
18+
def solve (input: List (Int × Int)) : Nat :=
19+
input.unzip
20+
|> λ (a, b) => List.map (λ (x, y) => (x - y).natAbs) (a.mergeSort.zip b.mergeSort)
21+
|> List.sum
22+
23+
def fakeRawInput: String := "3 4\n4 3\n2 5\n1 3\n3 9\n3 3"
24+
#eval solve <$> parse fakeRawInput
25+
26+
def fakeParsedInput: List (Int × Int) := [(3, 4), (4, 3), (2, 5), (1, 3), (3, 9), (3, 3)]
27+
#eval solve fakeParsedInput
28+
29+
def parseAndSolve : String → Option Nat :=
30+
Option.map solve ∘ parse
31+
32+
def rawInput := IO.FS.readFile "Aoc/Day1/input.txt"
33+
#eval parseAndSolve <$> rawInput
File renamed without changes.

0 commit comments

Comments
 (0)