Skip to content

Commit

Permalink
cover more cases
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 13, 2024
1 parent f2280da commit b63f8d9
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions solutions/src/2024/13.hs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,28 @@ cost extra (ax, ay, bx, by, x, y)
x' = x + extra
y' = y + extra

colinear :: Integer -> Integer -> Integer -> Integer -> Integer -> Integer -> Integer
colinear ax ay bx by x y
| ax*by == bx*ay -- buttons are colinear with each other
, ax*y == x*ay -- output is colinear with buttons
, let (d, e, f) = integerGcde ax bx
-- Buttons weren't colinear with the output - 0 is failure
| ax * y /= ay * x || bx * y /= by * x = 0

| ax /= 0, bx /= 0 = colinear' ax bx x
| ay /= 0, by /= 0 = colinear' ay by y

| ax /= 0 = 3 * single ax x
| ay /= 0 = 3 * single ay y
| bx /= 0 = single bx x
| by /= 0 = single by y

| otherwise = 0

single :: Integer -> Integer -> Integer
single ax x
| (p, 0) <- x `quotRem` ax = p
| otherwise = 0

colinear' :: Integer -> Integer -> Integer -> Integer
colinear' ax bx x
| let (d, e, f) = integerGcde ax bx
, (h, 0) <- x `quotRem` d -- target x is a multiple of the gcd of the two button +x
, let a = e * h -- prototypical (but potentially negative) solution
, let b = f * h -- prototypical (but potentially negative) solution
Expand Down

0 comments on commit b63f8d9

Please sign in to comment.