From f95539b173ded84451babf23e4710639f141f0ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Gergely?= Date: Wed, 25 Dec 2024 23:05:37 +0100 Subject: [PATCH] Day 13 Part 2 --- Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs | 8 +++--- Kodfodrasz.AoC.Year2024/Day13.fs | 30 +++++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs b/Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs index 633f7a9..9ec21a9 100644 --- a/Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs +++ b/Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs @@ -49,7 +49,7 @@ let ``Parsing example input`` () = [] let ``Answer 1 helper test 1`` () = test <@ - Some (80, 40) = solve { + Some (80L, 40L) = solve { ButtonA = 94, 34 ButtonB = 22, 67 Prize = 8400, 5400 } @@ -70,14 +70,14 @@ let ``Answer 1 for example input`` () = test <@ let actual = Result.bind answer1 input - let expected: Result<_, string> = Ok 480 + let expected: Result<_, string> = Ok 480L actual = expected @> -[] +[] 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 31L actual = expected @> diff --git a/Kodfodrasz.AoC.Year2024/Day13.fs b/Kodfodrasz.AoC.Year2024/Day13.fs index 1028807..2a9d4d3 100644 --- a/Kodfodrasz.AoC.Year2024/Day13.fs +++ b/Kodfodrasz.AoC.Year2024/Day13.fs @@ -7,9 +7,9 @@ open Kodfodrasz.AoC open MathNet.Numerics.LinearAlgebra type Entry = - { ButtonA : int*int - ButtonB : int*int - Prize : int*int } + { ButtonA : int64 * int64 + ButtonB : int64 * int64 + Prize : int64 * int64 } type parsedInput = Entry array @@ -27,8 +27,8 @@ let parseInput (input: string): Result = assert (matchesButtonA.Count = matchesButtonB.Count) assert (matchesButtonA.Count = matchesPrize.Count) - let getVals (m : Match) : int*int = - (m.Groups["x"].Value |> int), (m.Groups["y"].Value |> int) + let getVals (m : Match) : int64 * int64 = + (m.Groups["x"].Value |> int64), (m.Groups["y"].Value |> int64) let buttonA = matchesButtonA @@ -49,7 +49,7 @@ let parseInput (input: string): Result = |> Seq.toArray |> Ok -let solve (entry:Entry) : (int*int) option = +let solve (entry:Entry) : (int64 * int64) option = let v e = let x = fst e |> double let y = snd e |> double @@ -62,8 +62,8 @@ let solve (entry:Entry) : (int*int) option = let M = DenseMatrix.ofColumns [va; vb] let x = M.Solve(vp) - let a = x[0] |> Double.Round |> int - let b = x[1] |> Double.Round |> int + let a = x[0] |> Double.Round |> int64 + let b = x[1] |> Double.Round |> int64 if (a * fst entry.ButtonA + b * fst entry.ButtonB) = fst entry.Prize && (a * snd entry.ButtonA + b * snd entry.ButtonB) = snd entry.Prize @@ -73,13 +73,21 @@ let solve (entry:Entry) : (int*int) option = let answer1 (data : parsedInput) = data |> Seq.choose solve - |> Seq.map (fun (a, b) -> 3 * a + b) + |> Seq.map (fun (a, b) -> 3L * a + b) |> Seq.sum |> Ok let answer2 (data : parsedInput) = - failwith "TODO" - + data + |> Seq.map(fun e -> + let px = 10000000000000L + fst e.Prize + let py = 10000000000000L + snd e.Prize + { e with Prize = px, py } + ) + |> Seq.choose solve + |> Seq.map (fun (a, b) -> 3L * a + b) + |> Seq.sum + |> Ok type Solver() = inherit SolverBase("Claw Contraption") with