Skip to content

Commit ae154cf

Browse files
committed
part 2
1 parent 00d115c commit ae154cf

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

Aoc2024/Day07/Main.lean

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ def main : IO Unit := do
1616
IO.println s!"Example: {<- parseAndSolvePart2 exampleInput}"
1717
let answerPart2 <- parseAndSolvePart2 puzzleInput
1818
IO.println s!"Puzzle: {answerPart2}"
19-
assert! (answerPart2 == -1)
19+
assert! (answerPart2 == 637696070419031)

Aoc2024/Day07/Solve.lean

+9-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ import Aoc2024.Day07.Types
55

66
-- Part 1
77

8+
private def concatenate (a : Int) (b : Int) : Int := (reprStr a ++ reprStr b).toInt!
9+
810
private def evalNumbers (numbers : List Int) (operators : List Operator): Int :=
911
match numbers with
1012
| [] => 0
1113
| h :: t =>
1214
t.zip operators |>.foldl (fun acc (n, op) => match op with
1315
| .add => acc + n
1416
| .multiply => acc * n
17+
| .concatenation => concatenate acc n
1518
) h
1619

1720
#guard evalNumbers [11, 6, 16, 20] [.add, .multiply, .add] == 292
@@ -25,21 +28,22 @@ private def replicateM (n : Nat) (options : List α) : List (List α) :=
2528
#guard replicateM 0 [1, 2] == [[]]
2629
#guard replicateM 3 [1, 2] == [[1, 1, 1], [2, 1, 1], [1, 2, 1], [2, 2, 1], [1, 1, 2], [2, 1, 2], [1, 2, 2], [2, 2, 2]]
2730

28-
private def canEquationPossiblyBeTrue (equation : Equation) : Bool :=
29-
let operators := replicateM equation.numbers.length [.add, .multiply]
31+
private def canEquationPossiblyBeTrue (operators : List Operator) (equation : Equation) : Bool :=
32+
let operators := replicateM equation.numbers.length operators
3033
operators.any λ ops => evalNumbers equation.numbers ops == equation.testValue
3134

3235
private def solvePart1 (equations : List Equation) : Int :=
33-
equations.filter canEquationPossiblyBeTrue |>.sumBy (λ e => e.testValue)
36+
equations.filter (canEquationPossiblyBeTrue [.add, .multiply]) |>.sumBy (·.testValue)
3437

3538
def parseAndSolvePart1 (s : String): Except String Int := parseEquations s |>.map solvePart1
3639

3740
#guard parseAndSolvePart1 exampleInput == Except.ok 3749
3841

3942
-- Part 2
4043

41-
private def solvePart2 (equations : List Equation) : Int := sorry
44+
private def solvePart2 (equations : List Equation) : Int :=
45+
equations.filter (canEquationPossiblyBeTrue [.add, .multiply, .concatenation]) |>.sumBy (·.testValue)
4246

4347
def parseAndSolvePart2 (s : String): Except String Int := parseEquations s |>.map solvePart2
4448

45-
-- #guard parseAndSolvePart2 exampleInput == Except.ok -1
49+
#guard parseAndSolvePart2 exampleInput == Except.ok 11387

Aoc2024/Day07/Types.lean

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ deriving Repr, BEq, Hashable, Inhabited
66
inductive Operator where
77
| add
88
| multiply
9+
| concatenation
910
deriving Repr, BEq, Hashable, Inhabited
10-
11-
def Operator.all : List Operator := [Operator.add, Operator.multiply]

0 commit comments

Comments
 (0)