Skip to content

Commit

Permalink
Day 13 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
kodfodrasz committed Dec 25, 2024
1 parent c26e782 commit f95539b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Kodfodrasz.AoC.Year2024.Tests/Day13Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let ``Parsing example input`` () =
[<Fact>]
let ``Answer 1 helper test 1`` () =
test <@
Some (80, 40) = solve {
Some (80L, 40L) = solve {
ButtonA = 94, 34
ButtonB = 22, 67
Prize = 8400, 5400 }
Expand All @@ -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 @>

[<Fact(Skip="TODO")>]
[<Fact(Skip="No example result for part 2")>]
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 @>
30 changes: 19 additions & 11 deletions Kodfodrasz.AoC.Year2024/Day13.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,8 +27,8 @@ let parseInput (input: string): Result<parsedInput,string> =
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
Expand All @@ -49,7 +49,7 @@ let parseInput (input: string): Result<parsedInput,string> =
|> 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
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f95539b

Please sign in to comment.