aboutsummaryrefslogtreecommitdiff
path: root/Puzzle17.hs
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-18 14:23:53 +0100
committerYuchen Pei <me@ypei.me>2017-12-18 14:23:53 +0100
commit9a8ef4e20beb9b1295094d8ae5ec35d90878ac42 (patch)
tree9638dd2970b86ebde1c26e8548c673a3885b2846 /Puzzle17.hs
parent8c64712575451975c0418958d97687229a983741 (diff)
finished day 15 and day 17 part 2
Diffstat (limited to 'Puzzle17.hs')
-rw-r--r--Puzzle17.hs12
1 files changed, 8 insertions, 4 deletions
diff --git a/Puzzle17.hs b/Puzzle17.hs
index ff2d0d7..0cdaf54 100644
--- a/Puzzle17.hs
+++ b/Puzzle17.hs
@@ -1,3 +1,5 @@
+{-# LANGUAGE BangPatterns #-}
+
insert :: Int -> Int -> [Int] -> [Int]
insert k x xs = let (ys, zs) = splitAt k xs in ys ++ (x:zs)
@@ -5,20 +7,22 @@ step :: Int -> ([Int], Int, Int) -> ([Int], Int, Int)
step n (xs, pos, l) = let newpos = (pos + n) `rem` l + 1 in (insert newpos l xs, newpos, l + 1)
step' :: Int -> (Int, Int, Int) -> (Int, Int, Int)
-step' n (x, pos, l) = let newpos = (pos + n) `rem` l + 1 in
+step' n (!x, !pos, !l) = let newpos = (pos + n) `rem` l + 1 in
(if newpos == 1 then l else x, newpos, l + 1)
f :: Int -> Int -> ([Int], Int, Int)
f n m = foldl1 (.) (replicate m (step n)) ([0], 0, 1)
-f' :: Int -> Int -> (Int, Int, Int)
-f' n m = foldl1 (.) (replicate m (step' n)) (-1, 0, 1)
+f' :: Int -> Int -> (Int, Int, Int) -> (Int, Int, Int)
+f' n 0 acc = acc
+--f' n m = step' n (f' n (m - 1))
+f' n m !acc = f' n (m - 1) (step' n acc)
solve1 :: Int -> Int
solve1 n = let (xs, pos, l) = f n 2017 in xs !! (pos + 1)
solve2 :: Int -> Int
-solve2 n = let (x, pos, l) = f' n 50000000 in x
+solve2 n = let (x, pos, l) = f' n 50000000 (-1, 0, 1) in x
input0 :: Int
input0 = 3