From cb0d65fa6f7a250713467b9e2369d2ce85aaef25 Mon Sep 17 00:00:00 2001 From: Matt Russell Date: Tue, 3 Dec 2024 13:49:09 +0000 Subject: [PATCH] wip --- Aoc2024/Day03/Solve.lean | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Aoc2024/Day03/Solve.lean b/Aoc2024/Day03/Solve.lean index be29cfa..0eaad99 100644 --- a/Aoc2024/Day03/Solve.lean +++ b/Aoc2024/Day03/Solve.lean @@ -5,23 +5,24 @@ import Aoc2024.Day03.Types def Instruction.value : Instruction -> Int | Instruction.mul n m => n * m - | Instruction.do => 1 - | Instruction.dont => 1 + | _ => 1 def solvePart1 (s : String) : Int := findInstructions s |>.sumBy (·.value) #guard solvePart1 exampleInputPart1 == 161 -private def filterInstructions (enabled : Bool) : List Instruction -> List Instruction +private def findEnabledMulInstructions (enabled : Bool) : List Instruction -> List Instruction | [] => [] - | List.cons instruction rest => + | instruction :: rest => match instruction with | Instruction.mul m n => - if enabled then List.cons (Instruction.mul m n) (filterInstructions enabled rest) - else filterInstructions enabled rest - | Instruction.do => filterInstructions true rest - | Instruction.dont => filterInstructions false rest + if enabled then + Instruction.mul m n ::findEnabledMulInstructions enabled rest + else + findEnabledMulInstructions enabled rest + | Instruction.do => findEnabledMulInstructions true rest + | Instruction.dont => findEnabledMulInstructions false rest -def solvePart2 (s : String) : Int := findInstructions s |> filterInstructions true |> List.sumBy (·.value) +def solvePart2 (s : String) : Int := findInstructions s |> findEnabledMulInstructions true |>.sumBy (·.value) #guard solvePart2 exampleInputPart2 == 48