diff options
author | Yuchen Pei <me@ypei.me> | 2017-12-18 14:23:53 +0100 |
---|---|---|
committer | Yuchen Pei <me@ypei.me> | 2017-12-18 14:23:53 +0100 |
commit | 9a8ef4e20beb9b1295094d8ae5ec35d90878ac42 (patch) | |
tree | 9638dd2970b86ebde1c26e8548c673a3885b2846 | |
parent | 8c64712575451975c0418958d97687229a983741 (diff) |
finished day 15 and day 17 part 2
-rw-r--r-- | .Puzzle17.hs.swp | bin | 12288 -> 0 bytes | |||
-rwxr-xr-x | Puzzle15 | bin | 22680 -> 17928 bytes | |||
-rw-r--r-- | Puzzle15.hi | bin | 1201 -> 3295 bytes | |||
-rw-r--r-- | Puzzle15.hs | 49 | ||||
-rw-r--r-- | Puzzle15.o | bin | 12328 -> 7144 bytes | |||
-rwxr-xr-x | Puzzle17 | bin | 21872 -> 17736 bytes | |||
-rw-r--r-- | Puzzle17.hi | bin | 1162 -> 3281 bytes | |||
-rw-r--r-- | Puzzle17.hs | 12 | ||||
-rw-r--r-- | Puzzle17.o | bin | 8432 -> 6904 bytes |
9 files changed, 43 insertions, 18 deletions
diff --git a/.Puzzle17.hs.swp b/.Puzzle17.hs.swp Binary files differBinary files differdeleted file mode 100644 index 4873b94..0000000 --- a/.Puzzle17.hs.swp +++ /dev/null diff --git a/Puzzle15.hi b/Puzzle15.hi Binary files differindex 2df21fd..c3b5b63 100644 --- a/Puzzle15.hi +++ b/Puzzle15.hi diff --git a/Puzzle15.hs b/Puzzle15.hs index 104e2aa..1a1d596 100644 --- a/Puzzle15.hs +++ b/Puzzle15.hs @@ -1,24 +1,45 @@ +{-# LANGUAGE BangPatterns #-} + import Data.Int -stepA :: Int64 -> Int64 -stepA x = x * 16807 `mod` 2147483647 -stepB :: Int64 -> Int64 -stepB x = x * 48271 `mod` 2147483647 +stepA :: Int -> Int +stepA !x = x * 16807 `rem` 2147483647 +stepB :: Int -> Int +stepB !x = x * 48271 `rem` 2147483647 + +stepA' :: Int -> Int +stepA' !x = until ((==0) . flip rem 4) stepA (stepA x) -toInt16 :: Int64 -> Int16 +stepB' :: Int -> Int +stepB' !x = until ((==0) . flip rem 8) stepB (stepB x) + +toInt16 :: Int -> Int16 toInt16 = fromIntegral -stepAB :: ((Int64, Int64), Int) -> ((Int64, Int64), Int) -stepAB ((x, y), n) = ((stepA x, stepB y), if (toInt16 x) == (toInt16 y) then n + 1 else n) +stepAB :: ((Int, Int), Int) -> ((Int, Int), Int) +stepAB ((!x, !y), !n) = ((stepA x, stepB y), if (toInt16 x) == (toInt16 y) then n + 1 else n) + +stepAB' :: ((Int, Int), Int) -> ((Int, Int), Int) +stepAB' ((!x, !y), !n) = ((stepA' x, stepB' y), if (toInt16 x) == (toInt16 y) then n + 1 else n) -solve1' :: ((Int64, Int64), Int) -> Int -> ((Int64, Int64), Int) -solve1' z 0 = z -solve1' z m = let !w = stepAB z in let !m' = m - 1 in solve1' w m' +f :: Int -> ((Int, Int), Int) -> ((Int, Int), Int) +f 0 acc = acc +f m !acc = f (m - 1) (stepAB acc) -solve1 :: (Int64, Int64) -> Int -solve1 (x, y) = snd $ solve1' ((x, y), 0) 40000000 +f' :: Int -> ((Int, Int), Int) -> ((Int, Int), Int) +f' 0 acc = acc +f' m !acc = f' (m - 1) (stepAB' acc) -input :: (Int64, Int64) +solve1 :: (Int, Int) -> Int +solve1 (x, y) = snd $ f 40000000 ((x, y), 0) + +solve2 :: (Int, Int) -> Int +solve2 (x, y) = snd $ f' 5000000 ((x, y), 0) + +input :: (Int, Int) input = (783, 325) -main = (putStrLn . show . solve1) input +input0 = (65, 8921) :: (Int, Int) + +--main = (putStrLn . show . solve1) input +main = (putStrLn . show . solve2) input Binary files differBinary files differdiff --git a/Puzzle17.hi b/Puzzle17.hi Binary files differindex 070faf5..062ceab 100644 --- a/Puzzle17.hi +++ b/Puzzle17.hi 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 Binary files differ |