diff options
author | Yuchen Pei <me@ypei.me> | 2017-12-17 11:19:35 +0100 |
---|---|---|
committer | Yuchen Pei <me@ypei.me> | 2017-12-17 11:19:35 +0100 |
commit | 8c64712575451975c0418958d97687229a983741 (patch) | |
tree | 42c5aeb8bf744df84acd382c63580e2eadc5b9fe | |
parent | 892e5a8ac9cebf916926d74f64d402088f84430b (diff) |
finished Day 17 part1
- part 2 crashes the computer
-rw-r--r-- | .Puzzle17.hs.swp | bin | 0 -> 12288 bytes | |||
-rwxr-xr-x | Puzzle17 | bin | 0 -> 21872 bytes | |||
-rw-r--r-- | Puzzle17.hi | bin | 0 -> 1162 bytes | |||
-rw-r--r-- | Puzzle17.hs | 28 | ||||
-rw-r--r-- | Puzzle17.o | bin | 0 -> 8432 bytes |
5 files changed, 28 insertions, 0 deletions
diff --git a/.Puzzle17.hs.swp b/.Puzzle17.hs.swp Binary files differnew file mode 100644 index 0000000..4873b94 --- /dev/null +++ b/.Puzzle17.hs.swp diff --git a/Puzzle17 b/Puzzle17 Binary files differnew file mode 100755 index 0000000..79e5c36 --- /dev/null +++ b/Puzzle17 diff --git a/Puzzle17.hi b/Puzzle17.hi Binary files differnew file mode 100644 index 0000000..070faf5 --- /dev/null +++ b/Puzzle17.hi diff --git a/Puzzle17.hs b/Puzzle17.hs new file mode 100644 index 0000000..ff2d0d7 --- /dev/null +++ b/Puzzle17.hs @@ -0,0 +1,28 @@ +insert :: Int -> Int -> [Int] -> [Int] +insert k x xs = let (ys, zs) = splitAt k xs in ys ++ (x:zs) + +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 + (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) + +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 + +input0 :: Int +input0 = 3 +input :: Int +input = 354 + +main = (putStrLn . show . solve2) input diff --git a/Puzzle17.o b/Puzzle17.o Binary files differnew file mode 100644 index 0000000..76d835f --- /dev/null +++ b/Puzzle17.o |