Skip to content

Commit

Permalink
Day 7 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kodfodrasz committed Dec 29, 2024
1 parent 220274d commit 6064163
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
17 changes: 15 additions & 2 deletions Kodfodrasz.AoC.Year2024.Tests/Day7Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,24 @@ let ``Answer 1 for example input`` () =
let expected: Result<_, string> = Ok 3749L
actual = expected @>

[<Fact(Skip="TODO")>]
[<Fact>]
let ``Answer 2 helper function: check2`` () =
let input = parseInput exampleInput |> Result.get

test <@ true = Day7.check2 input[0] @>
test <@ true = Day7.check2 input[1] @>
test <@ true = Day7.check2 input[3] @>
test <@ true = Day7.check2 input[4] @>
test <@ false = Day7.check2 input[5] @>
test <@ true = Day7.check2 input[6] @>
test <@ false = Day7.check2 input[7] @>
test <@ true = Day7.check2 input[8] @>

[<Fact>]
let ``Answer 2 for example input`` () =
let input = parseInput exampleInput

test
<@ let actual = Result.bind answer2 input
let expected: Result<_, string> = Ok 31
let expected: Result<_, string> = Ok 11387L
actual = expected @>
26 changes: 25 additions & 1 deletion Kodfodrasz.AoC.Year2024/Day7.fs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,32 @@ let answer1 (data : parsedInput) =
|> Array.sumBy(fun eqn -> eqn.TestValue)
|> Ok

let check2 (eqn : CalibrationEquation) =
let concat (a: int64) (b:int64) =
sprintf "%i%i" a b |> int64

let rec check goal acc terms =
match terms with
| [] -> goal = acc
| head :: tail ->
if acc > goal then false
else
if (check goal (concat acc head) tail) then
true
elif (check goal (acc + head) tail) then
true
else
check goal (acc * head) tail
match eqn.Terms with
| [] -> false
| h :: t -> check eqn.TestValue h t

let answer2 (data : parsedInput) =
failwith "TODO"
data
|> List.toArray
|> Array.Parallel.filter check2
|> Array.sumBy(fun eqn -> eqn.TestValue)
|> Ok

type Solver() =
inherit SolverBase("Bridge Repair")
Expand Down

0 comments on commit 6064163

Please sign in to comment.