From a14889eed39a71079594da4f4fe1a20adea4df3a Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 15 Dec 2016 11:03:43 +0000 Subject: [PATCH] Day 15 --- README.md | 4 +- advent15.hs | 37 ++++++++++++ advent15.test.txt | 3 + advent15.txt | 6 ++ advent15f.hs | 42 +++++++++++++ day15.html | 146 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+), 2 deletions(-) create mode 100644 advent15.hs create mode 100644 advent15.test.txt create mode 100644 advent15.txt create mode 100644 advent15f.hs create mode 100644 day15.html diff --git a/README.md b/README.md index 5663158..2e4886c 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ advent01 If you're profiling, compile and run with ``` -ghc -O2 --make advent01.hs -prof -auto-all -caf-all -fforce-recomp +ghc -O2 --make advent01.hs -prof -auto-all -caf-all -fforce-recomp -rstopts time ./advent01 +RTS -p -hy ``` @@ -44,4 +44,4 @@ Build this readme file wth pandoc -s README.md > README.html ``` -(Using the [Modest styles](https://github.com/markdowncss/modest).) \ No newline at end of file +(Using the [Modest style](https://github.com/markdowncss/modest).) \ No newline at end of file diff --git a/advent15.hs b/advent15.hs new file mode 100644 index 0000000..e3c97fb --- /dev/null +++ b/advent15.hs @@ -0,0 +1,37 @@ +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + +main :: IO () +main = do + text <- readFile "advent15.txt" + let disks = successfulParse $ parseIfile text + part1 disks + part2 disks + +part1 :: [[Int]] -> IO () +part1 disks = print $ head $ filter (canFall disks) [0..] + +part2 :: [[Int]] -> IO () +part2 disks = print $ head $ filter (canFall disks2) [0..5000000] + where disks2 = id $! map (take 5000000) $ disks ++ [drop 7 $ drop 0 $ cycle [0..(11-1)]] + +canFall :: [[Int]] -> Int -> Bool +canFall ds i = all (\d -> (d!!i) == 0) ds + + +instructionFile = instructionLine `endBy` newline +instructionLine = diskify <$> (string "Disc #" *> int) + <*> (string " has " *> int) + <*> (string " positions; at time=0, it is at position " *> int) + <* (string ".") + where diskify n size pos0 = drop n $ drop pos0 $ cycle [0..(size-1)] + +parseIfile :: String -> Either ParseError [[Int]] +parseIfile input = parse instructionFile "(unknown)" input + +parseIline :: String -> Either ParseError [Int] +parseIline input = parse instructionLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a diff --git a/advent15.test.txt b/advent15.test.txt new file mode 100644 index 0000000..3683605 --- /dev/null +++ b/advent15.test.txt @@ -0,0 +1,3 @@ +Disc #1 has 5 positions; at time=0, it is at position 4. +Disc #2 has 2 positions; at time=0, it is at position 1. + diff --git a/advent15.txt b/advent15.txt new file mode 100644 index 0000000..d2463d1 --- /dev/null +++ b/advent15.txt @@ -0,0 +1,6 @@ +Disc #1 has 5 positions; at time=0, it is at position 2. +Disc #2 has 13 positions; at time=0, it is at position 7. +Disc #3 has 17 positions; at time=0, it is at position 10. +Disc #4 has 3 positions; at time=0, it is at position 2. +Disc #5 has 19 positions; at time=0, it is at position 9. +Disc #6 has 7 positions; at time=0, it is at position 0. diff --git a/advent15f.hs b/advent15f.hs new file mode 100644 index 0000000..31866c0 --- /dev/null +++ b/advent15f.hs @@ -0,0 +1,42 @@ +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + +type Disk = (Int -> Bool) + +main :: IO () +main = do + text <- readFile "advent15.txt" + let disks = successfulParse $ parseIfile text + part1 disks + part2 disks + +part1 :: [Disk] -> IO () +part1 disks = print $ head $ filter (canFall disks) [0..] + +part2 :: [Disk] -> IO () +part2 disks = print $ head $ filter (canFall disks2) [0..] + -- where disks2 = disks ++ [(\i -> (11 + 7 + 0 + i) `mod` 11 == 0)] + where disks2 = disks ++ [diskify 7 11 0] + +canFall :: [Disk] -> Int -> Bool +canFall ds i = all (\d -> (d i)) ds + + +instructionFile = instructionLine `endBy` newline +instructionLine = diskify <$> (string "Disc #" *> int) + <*> (string " has " *> int) + <*> (string " positions; at time=0, it is at position " *> int) + <* (string ".") + +diskify :: Int -> Int -> Int -> (Int -> Bool) +diskify n size pos0 = (\i -> (size + n + pos0 + i) `mod` size == 0) + +parseIfile :: String -> Either ParseError [Disk] +parseIfile input = parse instructionFile "(unknown)" input + +parseIline :: String -> Either ParseError Disk +parseIline input = parse instructionLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a diff --git a/day15.html b/day15.html new file mode 100644 index 0000000..17dc477 --- /dev/null +++ b/day15.html @@ -0,0 +1,146 @@ + + + + +Day 15 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 30*

   int y=2016;

+ + + +
+

--- Day 15: Timing is Everything ---

The halls open into an interior plaza containing a large kinetic sculpture. The sculpture is in a sealed enclosure and seems to involve a set of identical spherical capsules that are carried to the top and allowed to bounce through the maze of spinning pieces.

+

Part of the sculpture is even interactive! When a button is pressed, a capsule is dropped and tries to fall through slots in a set of rotating discs to finally go through a little hole at the bottom and come out of the sculpture. If any of the slots aren't aligned with the capsule as it passes, the capsule bounces off the disc and soars away. You feel compelled to get one of those capsules.

+

The discs pause their motion each second and come in different sizes; they seem to each have a fixed number of positions at which they stop. You decide to call the position with the slot 0, and count up for each position it reaches next.

+

Furthermore, the discs are spaced out so that after you push the button, one second elapses before the first disc is reached, and one second elapses as the capsule passes from one disk to the one below it. So, if you push the button at time=100, then the capsule reaches the top disc at time=101, the second disc at time=102, the third disc at time=103, and so on.

+

The button will only drop a capsule at an integer time - no fractional seconds allowed.

+

For example, at time=0, suppose you see the following arrangement:

+
Disc #1 has 5 positions; at time=0, it is at position 4.
+Disc #2 has 2 positions; at time=0, it is at position 1.
+
+

If you press the button exactly at time=0, the capsule would start to fall; it would reach the first disc at time=1. Since the first disc was at position 4 at time=0, by time=1 it has ticked one position forward. As a five-position disc, the next position is 0, and the capsule falls through the slot.

+

Then, at time=2, the capsule reaches the second disc. The second disc has ticked forward two positions at this point: it started at position 1, then continued to position 0, and finally ended up at position 1 again. Because there's only a slot at position 0, the capsule bounces away.

+

If, however, you wait until time=5 to push the button, then when the capsule reaches each disc, the first disc will have ticked forward 5+1 = 6 times (to position 0), and the second disc will have ticked forward 5+2 = 7 times (also to position 0). In this case, the capsule would fall through the discs and come out of the machine.

+

However, your situation has more than two discs; you've noted their positions in your puzzle input. What is the first time you can press the button to get a capsule?

+
+

Your puzzle answer was 148737.

--- Part Two ---

After getting the first capsule (it contained a star! what great fortune!), the machine detects your success and begins to rearrange itself.

+

When it's done, the discs are back in their original configuration as if it were time=0 again, but a new disc with 11 positions and starting at position 0 has appeared exactly one second below the previously-bottom disc.

+

With this new disc, and counting again starting from time=0 with the configuration in your puzzle input, what is the first time you can press the button to get another capsule?

+
+

Your puzzle answer was 2353212.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1