Skip to content

Commit 933499d

Browse files
committed
Remove redundant period-finding
1 parent c1e32bf commit 933499d

File tree

1 file changed

+1
-11
lines changed

1 file changed

+1
-11
lines changed

day14/src/Day14.hs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,10 @@ split x (y:ys) | x == y = [] : split x ys
1313
in (y : y') : ys'
1414
split _ [] = [[]]
1515

16-
-- | Finds the period of the given function.
17-
period :: Ord a => (a -> a) -> a -> [a]
18-
period f = period' S.empty
19-
where period' acc x | x `S.member` acc = []
20-
| otherwise = x : period' (S.insert x acc) (f x)
21-
2216
-- | The cartesian product with itself.
2317
cartesianSquare :: [a] -> [(a, a)]
2418
cartesianSquare xs = [(x, x') | x <- xs, x' <- xs]
2519

26-
-- | Finds the nth element of the list (modulo the list's length) .
27-
(!!%) :: [a] -> Int -> a
28-
xs !!% n = xs !! (n `mod` length xs)
29-
3020
data Vec2 a = Vec2 { x :: a, y :: a }
3121
deriving (Show, Eq, Ord, Functor)
3222

@@ -63,7 +53,7 @@ step :: Robot -> Robot
6353
step r = Robot ((r.pos .+. r.vel) .%. boardSize) r.vel
6454

6555
stepN :: Int -> Robot -> Robot
66-
stepN n = (!!% n) . period step
56+
stepN n = (!! n) . iterate step
6757

6858
safetyFactor :: [Robot] -> Int
6959
safetyFactor rs = foldr1 (*) [length $ filter (\p -> p.x `xop` center.x && p.y `yop` center.y) ps | (xop, yop) <- cartesianSquare [(<), (>)]]

0 commit comments

Comments
 (0)