From 6c33fcf371ca6d7c86e6cb79cc7de5d8af41181b Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 18 Dec 2016 10:45:50 +0000 Subject: [PATCH] Done day 18 --- advent18.hs | 42 +++++++++++++ day18.html | 172 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 214 insertions(+) create mode 100644 advent18.hs create mode 100644 day18.html diff --git a/advent18.hs b/advent18.hs new file mode 100644 index 0000000..04ab345 --- /dev/null +++ b/advent18.hs @@ -0,0 +1,42 @@ +import Data.List (iterate, tails) + +-- input = "..^^." +-- input = ".^^.^.^^^^" +input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^." + + +main :: IO () +main = do + part1 + part2 + + +part1 :: IO () +-- part1 = putStrLn $ unlines $ map (showRow) $ take 10 $ iterate nextRow $ readRow input +part1 = print $ length $ filter (not) $ concat $ take 40 $ iterate nextRow $ readRow input + +part2 :: IO () +part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input + +readRow :: String -> [Bool] +readRow = map (=='^') + +showRow :: [Bool] -> String +showRow = map (\c -> if c then '^' else '.') + +extended :: [Bool] -> [Bool] +extended row = [False] ++ row ++ [False] + +nextRow :: [Bool] -> [Bool] +nextRow = map (isTrap) . segments . extended + +segments :: [a] -> [[a]] +segments = filter ((==3) . length) . map (take 3) . tails + +isTrap :: [Bool] -> Bool +isTrap segment + | segment == [True, True, False] = True + | segment == [False, True, True] = True + | segment == [True, False, False] = True + | segment == [False, False, True] = True + | otherwise = False diff --git a/day18.html b/day18.html new file mode 100644 index 0000000..73e17c0 --- /dev/null +++ b/day18.html @@ -0,0 +1,172 @@ + + + + +Day 18 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 36*

   <y>2016</y>

+ + + +
+

--- Day 18: Like a Rogue ---

As you enter this room, you hear a loud click! Some of the tiles in the floor here seem to be pressure plates for traps, and the trap you just triggered has run out of... whatever it tried to do to you. You doubt you'll be so lucky next time.

+

Upon closer examination, the traps and safe tiles in this room seem to follow a pattern. The tiles are arranged into rows that are all the same width; you take note of the safe tiles (.) and traps (^) in the first row (your puzzle input).

+

The type of tile (trapped or safe) in each row is based on the types of the tiles in the same position, and to either side of that position, in the previous row. (If either side is off either end of the row, it counts as "safe" because there isn't a trap embedded in the wall.)

+

For example, suppose you know the first row (with tiles marked by letters) and want to determine the next row (with tiles marked by numbers):

+
ABCDE
+12345
+
+

The type of tile 2 is based on the types of tiles A, B, and C; the type of tile 5 is based on tiles D, E, and an imaginary "safe" tile. Let's call these three tiles from the previous row the left, center, and right tiles, respectively. Then, a new tile is a trap only in one of the following situations:

+
    +
  • Its left and center tiles are traps, but its right tile is not.
  • +
  • Its center and right tiles are traps, but its left tile is not.
  • +
  • Only its left tile is a trap.
  • +
  • Only its right tile is a trap.
  • +
+

In any other situation, the new tile is safe.

+

Then, starting with the row ..^^., you can determine the next row by applying those rules to each new tile:

+
    +
  • The leftmost character on the next row considers the left (nonexistent, so we assume "safe"), center (the first ., which means "safe"), and right (the second ., also "safe") tiles on the previous row. Because all of the trap rules require a trap in at least one of the previous three tiles, the first tile on this new row is also safe, ..
  • +
  • The second character on the next row considers its left (.), center (.), and right (^) tiles from the previous row. This matches the fourth rule: only the right tile is a trap. Therefore, the next tile in this new row is a trap, ^.
  • +
  • The third character considers .^^, which matches the second trap rule: its center and right tiles are traps, but its left tile is not. Therefore, this tile is also a trap, ^.
  • +
  • The last two characters in this new row match the first and third rules, respectively, and so they are both also traps, ^.
  • +
+

After these steps, we now know the next row of tiles in the room: .^^^^. Then, we continue on to the next row, using the same rules, and get ^^..^. After determining two new rows, our map looks like this:

+
..^^.
+.^^^^
+^^..^
+
+

Here's a larger example with ten tiles per row and ten rows:

+
.^^.^.^^^^
+^^^...^..^
+^.^^.^.^^.
+..^^...^^^
+.^^^^.^^.^
+^^..^.^^..
+^^^^..^^^.
+^..^^^^.^^
+.^^^..^.^^
+^^.^^^..^^
+
+

In ten rows, this larger example has 38 safe tiles.

+

Starting with the map in your puzzle input, in a total of 40 rows (including the starting row), how many safe tiles are there?

+
+

Your puzzle answer was 1926.

--- Part Two ---

How many safe tiles are there in a total of 400000 rows?

+
+

Your puzzle answer was 19986699.

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