Skip to content

Commit fcd0a95

Browse files
committed
wip
1 parent ae154cf commit fcd0a95

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

Aoc2024/Day07/Solve.lean

+11-24
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,31 @@ import Aoc2024.Day07.Types
55

66
-- Part 1
77

8-
private def concatenate (a : Int) (b : Int) : Int := (reprStr a ++ reprStr b).toInt!
9-
108
private def evalNumbers (numbers : List Int) (operators : List Operator): Int :=
11-
match numbers with
12-
| [] => 0
13-
| h :: t =>
14-
t.zip operators |>.foldl (fun acc (n, op) => match op with
15-
| .add => acc + n
16-
| .multiply => acc * n
17-
| .concatenation => concatenate acc n
18-
) h
9+
numbers.zip (.add :: operators) |>.foldl (λ acc (n, op) => op.fn acc n) 0
1910

2011
#guard evalNumbers [11, 6, 16, 20] [.add, .multiply, .add] == 292
2112

22-
private def replicateM (n : Nat) (options : List α) : List (List α) :=
23-
let rec loop (n : Nat) (acc : List (List α)) : List (List α) :=
24-
match n with
25-
| 0 => acc
26-
| n + 1 => loop n (acc.bind λ l => options.map (λ o => o :: l))
27-
loop n [[]]
28-
#guard replicateM 0 [1, 2] == [[]]
29-
#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]]
13+
private def Equation.isTrue (equation : Equation) (operators : List Operator) : Bool :=
14+
evalNumbers equation.numbers operators == equation.testValue
15+
16+
private def Equation.operatorPositions (equation : Equation) : Nat := equation.numbers.length - 1
3017

3118
private def canEquationPossiblyBeTrue (operators : List Operator) (equation : Equation) : Bool :=
32-
let operators := replicateM equation.numbers.length operators
33-
operators.any λ ops => evalNumbers equation.numbers ops == equation.testValue
19+
replicateM equation.operatorPositions operators |>.any equation.isTrue
20+
21+
private def solveGeneric (operators : List Operator) (equations : List Equation) : Int :=
22+
equations.filter (canEquationPossiblyBeTrue operators) |>.sumBy (·.testValue)
3423

35-
private def solvePart1 (equations : List Equation) : Int :=
36-
equations.filter (canEquationPossiblyBeTrue [.add, .multiply]) |>.sumBy (·.testValue)
24+
private def solvePart1 : List Equation -> Int := solveGeneric [.add, .multiply]
3725

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

4028
#guard parseAndSolvePart1 exampleInput == Except.ok 3749
4129

4230
-- Part 2
4331

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

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

Aoc2024/Day07/Types.lean

+7
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ inductive Operator where
88
| multiply
99
| concatenation
1010
deriving Repr, BEq, Hashable, Inhabited
11+
12+
private def concatenate (a : Int) (b : Int) : Int := (reprStr a ++ reprStr b).toInt!
13+
14+
def Operator.fn : Operator -> Int -> Int -> Int
15+
| .add, a, b => a + b
16+
| .multiply, a, b => a * b
17+
| .concatenation, a, b => concatenate a b

Aoc2024/Utils.lean

+9
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,12 @@ namespace Option
163163
#guard (some 42).getOrThrow "Error" == Except.ok 42
164164
#guard (none : Option Int).getOrThrow "Error" == Except.error "Error"
165165
end Option
166+
167+
def replicateM (n : Nat) (options : List α) : List (List α) :=
168+
let rec loop (n : Nat) (acc : List (List α)) : List (List α) :=
169+
match n with
170+
| 0 => acc
171+
| n + 1 => loop n (acc.bind λ l => options.map (λ o => o :: l))
172+
loop n [[]]
173+
#guard replicateM 0 [1, 2] == [[]]
174+
#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]]

0 commit comments

Comments
 (0)