Skip to content

Commit

Permalink
day3 part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
mdr committed Dec 3, 2024
1 parent 0fb84af commit dec694c
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Aoc2024/Day03/Examples.lean
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
def exampleInput :=
"..."
"xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"
4 changes: 2 additions & 2 deletions Aoc2024/Day03/Main.lean
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import Aoc2024.CustomMonadLift
def solve (name: String) (inputPath: String) : IO Unit := do
IO.println name
let input <- IO.FS.readFile inputPath
IO.println s!"Part 1: {<- parseAndSolvePart1 input}"
IO.println s!"Part 2: {<- parseAndSolvePart2 input}"
IO.println s!"Part 1: {solvePart1 input}"
-- IO.println s!"Part 2: {<- parseAndSolvePart2 input}"

def main : IO Unit := do
IO.println "Day 03"
Expand Down
25 changes: 22 additions & 3 deletions Aoc2024/Day03/Solve.lean
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import Aoc2024.Utils
import Aoc2024.Day03.Examples
import Aoc2024.Day03.Parser
import Regex
open Regex (Match Captures)

private def solvePart1 (things : List Int) : Int := sorry
def mulRegex := regex% r"mul\((\d+),(\d+)\)"

def parseAndSolvePart1 (s : String): Except String Int := parseThings s |>.map solvePart1
def matchToNat (m : Match) : Option Nat := m.toNat?

-- #guard parseAndSolvePart1 exampleInput == Except.ok 2
def optMatchToNat (m : Option Match) : Option Nat := m >>= matchToNat

def matchesToNatPair (ms : Array (Option Match)): Option (Nat × Nat) :=
match ms.toList with
| [some m1, some m2] => Prod.mk <$> matchToNat m1 <*> matchToNat m2
| _ => none

def capturesToNatPair (captures: Captures) : Option (Nat × Nat) :=
matchesToNatPair captures.groups

def findMatches (s : String) : List (Nat × Nat) :=
Regex.all_captures s mulRegex |>.toList |>.bind (fun captures => captures |> capturesToNatPair |> Option.toList)

#guard findMatches "mul(1,2) mul(3,4)" = [(1, 2), (3, 4)]

def solvePart1 (s : String) : Int := findMatches s |>.sumBy (fun (n1, n2) => n1 * n2)

#guard solvePart1 exampleInput = 161

private def solvePart2 (things : List Int) : Int := sorry

Expand Down
1 change: 1 addition & 0 deletions Aoc2024/Day03/inputs/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
6 changes: 6 additions & 0 deletions Aoc2024/Day03/inputs/input.txt

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Aoc2024/Utils.lean
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ def sepBy (p : Parser α) (sep : Parser β) : Parser (List α) :=

namespace List

def sum (xs: List Int) : Int := xs.foldl .add 0

def sumBy (f : α → Int) (xs : List α) : Int := xs.foldl (λ acc x => acc + f x) 0

def toSet {α:Type} [BEq α] [Hashable α] (xs: List α) : HashSet α :=
Expand All @@ -57,6 +59,9 @@ namespace List

end List

#guard [].sum = 0
#guard [1, 2, 3].sum = 6

#guard [1, 2, 3].sumBy (λ x => x * x) = 14

#guard [].differences == []
Expand Down
28 changes: 24 additions & 4 deletions lake-manifest.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
{"version": "1.1.0",
"packagesDir": ".lake/packages",
"packages":
[{"url": "https://github.com/leanprover-community/batteries",
[{"url": "https://github.com/fgdorais/lean4-unicode-basic.git",
"type": "git",
"subDir": null,
"scope": "leanprover-community",
"rev": "7805acf1864ba1a2e359f86a8f092ccf1438ad83",
"scope": "",
"rev": "b41bc9cec7f433d6e1d74ff3b59edaaf58ad2915",
"name": "UnicodeBasic",
"manifestFile": "lake-manifest.json",
"inputRev": "v1.1.0",
"inherited": true,
"configFile": "lakefile.lean"},
{"url": "https://github.com/leanprover-community/batteries",
"type": "git",
"subDir": null,
"scope": "",
"rev": "31a10a332858d6981dbcf55d54ee51680dd75f18",
"name": "batteries",
"manifestFile": "lake-manifest.json",
"inputRev": "v4.13.0",
"inherited": true,
"configFile": "lakefile.toml"},
{"url": "https://github.com/bergmannjg/regex",
"type": "git",
"subDir": null,
"scope": "",
"rev": "b42eb7a7b1338cb18873fc1e5a985600026c2f56",
"name": "Regex",
"manifestFile": "lake-manifest.json",
"inputRev": "main",
"inherited": false,
"configFile": "lakefile.toml"}],
"configFile": "lakefile.lean"}],
"name": "aoc2024",
"lakeDir": ".lake"}
4 changes: 2 additions & 2 deletions lakefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ name = "dayXX"
root = "Aoc2024.DayXX.Main"

[[require]]
name = "batteries"
scope = "leanprover-community"
name = "Regex"
git = "https://github.com/bergmannjg/regex"
rev = "main"
2 changes: 1 addition & 1 deletion lean-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
leanprover/lean4:v4.15.0-rc1
leanprover/lean4:v4.13.0

0 comments on commit dec694c

Please sign in to comment.