Skip to content

Commit

Permalink
do the math directly
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 13, 2024
1 parent 3065c7b commit c9f3bed
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
1 change: 0 additions & 1 deletion solutions/solutions.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -1176,4 +1176,3 @@ executable sln_2024_12
executable sln_2024_13
import: day
main-is: 2024/13.hs
build-depends: hmatrix
35 changes: 15 additions & 20 deletions solutions/src/2024/13.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ Prize: X=18641, Y=10279
module Main (main) where

import Advent (format)
import Numeric.LinearAlgebra (Matrix, linearSolve, atIndex, (><))

-- | >>> :main
-- 29877
Expand All @@ -51,23 +50,19 @@ main =
print (sum (map (cost 0) input))
print (sum (map (cost 10000000000000) input))

cost :: Int -> (Int, Int, Int, Int, Int, Int) -> Int
cost extra (ax, ay, bx, by, x, y) =
case linearSolve m v of
Just tu
| t * ax + u * bx == x'
, t * ay + u * by == y' -> 3 * t + u
where
t = round (tu `atIndex` (0,0))
u = round (tu `atIndex` (1,0))
_ -> 0
cost :: Int -> (Int, Int, Int, Int, Int, Int) -> Integer
cost extra (ax, ay, bx, by, x, y)
| det == 0 = error "colinearity not supported"
| a >= 0, b >= 0, ar == 0, br == 0 = 3 * a + b
| otherwise = 0
where
x' = extra + x
y' = extra + y

m :: Matrix Double
m = (2><2) [ fromIntegral ax, fromIntegral bx,
fromIntegral ay, fromIntegral by]

v = (2><1) [ fromIntegral x',
fromIntegral y']
ax' = toInteger ax
ay' = toInteger ay
bx' = toInteger bx
by' = toInteger by
x' = toInteger (x + extra)
y' = toInteger (y + extra)

det = ax' * by' - ay' * bx'
(a, ar) = (by' * x' - bx' * y') `quotRem` det
(b, br) = (ax' * y' - ay' * x') `quotRem` det

0 comments on commit c9f3bed

Please sign in to comment.