Skip to content

Commit 2c37997

Browse files
committed
fix(2024/Day13): epsilon precision had wrong value
1 parent deffab2 commit 2c37997

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

2024/Day13/Day13.hs

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ import Data.Tuple.Extra
1313
import Debug.Trace
1414
import System.Environment
1515
import Text.Regex.TDFA ((=~))
16+
1617
-- TODO: Cleanup imports after day done
1718

1819
type Input = [((Double, Double), (Double, Double), (Double, Double))]
20+
1921
type Output = Int
2022

2123
parseInput :: String -> Input
2224
parseInput input = res
2325
where
24-
res = map (\[sb1, sb2, sprize] -> (getPos sb1, getPos sb2, getPos sprize)) . map lines $ splitOn "\n\n" input
26+
res = map (\[sb1, sb2, sprize] -> (getPos sb1, getPos sb2, getPos sprize)) . map lines $ splitOn "\n\n" input
2527

2628
t :: String -> [[String]]
2729
t s = s =~ ".* X.(.*), Y.(.*)" :: [[String]]
@@ -30,9 +32,8 @@ parseInput input = res
3032
where
3133
[[_, x, y]] = t s
3234

33-
3435
part1 :: Input -> Output
35-
part1 = sum . map (\(nx, ny) -> 3*nx+ny) . map fst . filter snd . map (uncurry3 solveArcade)
36+
part1 = sum . map (\(nx, ny) -> 3 * nx + ny) . map fst . filter snd . map (uncurry3 solveArcade)
3637
where
3738
-- Old version checking for valid combinations => Waayyyy too sloww for part2
3839
-- solveArcade pos seen b1 b2 prize
@@ -52,8 +53,8 @@ part1 = sum . map (\(nx, ny) -> 3*nx+ny) . map fst . filter snd . map (uncurry3
5253

5354
isAlmostInt :: Double -> Bool
5455
isAlmostInt x = abs (x - fromInteger (round x)) < eps
55-
where eps = 1e-4 --- REALLY sketchy : doubles are not precise enough to have a good eps (1e-10 or something close) so had to guess it and get 0.01 since 0.1 is too big and 0.0001 is too small
56-
56+
where
57+
eps = 1e-2 --- REALLY sketchy : doubles are not precise enough to have a good eps (1e-10 or something close) so had to guess it and get 1e-2 since 1e-1 is too big and 1e-4 is too small
5758
solveArcade (xb1, yb1) (xb2, yb2) (xp, yp)
5859
| isLeft inva = trace (show (fromLeft "" inva)) ((-1, -1), False)
5960
| isAlmostInt nb1 && isAlmostInt nb2 = ((round nb1, round nb2), True)
@@ -69,13 +70,12 @@ part1 = sum . map (\(nx, ny) -> 3*nx+ny) . map fst . filter snd . map (uncurry3
6970
res = Mat.multStd (fromRight (Mat.zero 1 1) inva) b
7071
(nb1, nb2) = (res ! (1, 1), res ! (2, 1))
7172

72-
7373
part2 :: Input -> Output
7474
part2 = part1 . map (\(b1, b2, p) -> (b1, b2, both ((+) 10000000000000) p))
7575

7676
main :: IO ()
7777
main = do
78-
args <- getArgs
78+
args <- getArgs
7979
content <- readFile (last args)
8080
let input = parseInput content
8181

0 commit comments

Comments
 (0)