Skip to content

Commit

Permalink
a bit better day1 sol
Browse files Browse the repository at this point in the history
  • Loading branch information
mikolajkapica committed Dec 4, 2024
1 parent 5dca5f7 commit 2db17a3
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 37 deletions.
3 changes: 0 additions & 3 deletions Aoc.lean

This file was deleted.

30 changes: 0 additions & 30 deletions Aoc/1/Basic.lean

This file was deleted.

33 changes: 33 additions & 0 deletions Aoc/Day1/Solution.lean
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
instance : Monad List where
pure := List.singleton
bind := List.flatMap

def List.sequence {m α} [Monad m] (lst: List (m α)) : m (List α) := List.mapM id lst

def parse (raw : String) : Option (List (Int × Int)) :=
raw.splitOn "\n"
|> List.map (String.splitOn · " ")
|> List.map (List.map String.toInt?)
|> List.map List.sequence
|> List.filter Option.isSome
|> List.sequence
|> Option.map (List.map (λ x => match x with | [a, b] => some (a, b) | _ => none))
|> Option.map List.sequence
|> Option.join

def solve (input: List (Int × Int)) : Nat :=
input.unzip
|> λ (a, b) => List.map (λ (x, y) => (x - y).natAbs) (a.mergeSort.zip b.mergeSort)
|> List.sum

def fakeRawInput: String := "3 4\n4 3\n2 5\n1 3\n3 9\n3 3"
#eval solve <$> parse fakeRawInput

def fakeParsedInput: List (Int × Int) := [(3, 4), (4, 3), (2, 5), (1, 3), (3, 9), (3, 3)]
#eval solve fakeParsedInput

def parseAndSolve : String → Option Nat :=
Option.map solve ∘ parse

def rawInput := IO.FS.readFile "Aoc/Day1/input.txt"
#eval parseAndSolve <$> rawInput
File renamed without changes.
4 changes: 0 additions & 4 deletions Main.lean

This file was deleted.

0 comments on commit 2db17a3

Please sign in to comment.