From 14702303f1b665a4685e5fadf56f23571f1dd8dc Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sat, 17 Dec 2016 10:48:33 +0000 Subject: [PATCH] Day 17 --- advent17.hs | 86 +++++++++++++++++++++++++++ day17.html | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 250 insertions(+) create mode 100644 advent17.hs create mode 100644 day17.html diff --git a/advent17.hs b/advent17.hs new file mode 100644 index 0000000..d255a09 --- /dev/null +++ b/advent17.hs @@ -0,0 +1,86 @@ +import Data.List (subsequences, (\\), sort, sortBy) +import Data.Ord (comparing) +import Data.ByteString.Char8 (pack) +import Crypto.Hash (hash, Digest, MD5) + + +type Position = (Int, Int) +data Agendum = Agendum {position :: Position, path :: String, hsh :: String} deriving (Show, Eq) +type Agenda = [Agendum] + +-- input = "hijkl" +-- input = "ihgpwlah" + +input = "qljzarfv" -- my input + + + +main :: IO () +main = do + part1 + part2 + +initialAgenda = [Agendum {position=(1, 1), path="", hsh=(getHash "")}] + + +part1 :: IO () +part1 = print $ path $ extractJust $ bfs initialAgenda + +part2 :: IO () +part2 = print $ bfs2 initialAgenda 0 + + +getHash :: String -> String +getHash path = show (hash $ pack (input ++ path) :: Digest MD5) + + +extractJust :: Maybe Agendum -> Agendum +extractJust Nothing = head initialAgenda +extractJust (Just x) = x + + +bfs :: Agenda -> Maybe Agendum +bfs [] = Nothing +bfs (current:agenda) = + if isGoal current then Just current + else bfs (agenda ++ (successors current)) + + +bfs2 :: Agenda -> Int -> Int +bfs2 [] l = l +bfs2 (current:agenda) l = + if isGoal current then bfs2 agenda (length $ path $ current) + else bfs2 (agenda ++ (successors current)) l + + +isGoal :: Agendum -> Bool +isGoal agendum = (position agendum) == (4, 4) + +isLegalPos :: Position -> Bool +isLegalPos p = fst p >= 1 && fst p <= 4 && snd p >= 1 && snd p <= 4 + +successors :: Agendum -> Agenda +successors state = [Agendum {position = step p0 ld, + path = path0 ++ [ld], + hsh = getHash (path0 ++ [ld])} | ld <- legalDoors ] + where + p0 = position state + path0 = path state + h0 = hsh state + doors = openDoors h0 + legalDoors = filter (isLegalPos . (step p0)) doors + +openDoors hash = (u hash) ++ (d hash) ++ (l hash) ++ (r hash) + +u hash = if hash!!0 `elem` "bcdef" then "U" else "" +d hash = if hash!!1 `elem` "bcdef" then "D" else "" +l hash = if hash!!2 `elem` "bcdef" then "L" else "" +r hash = if hash!!3 `elem` "bcdef" then "R" else "" + +step (r, c) 'U' = (r-1, c) +step (r, c) 'D' = (r+1, c) +step (r, c) 'L' = (r, c-1) +step (r, c) 'R' = (r, c+1) + + + diff --git a/day17.html b/day17.html new file mode 100644 index 0000000..6b440f1 --- /dev/null +++ b/day17.html @@ -0,0 +1,164 @@ + + + + +Day 17 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 34*

   $year=2016;

+ + + +
+

--- Day 17: Two Steps Forward ---

You're trying to access a secure vault protected by a 4x4 grid of small rooms connected by doors. You start in the top-left room (marked S), and you can access the vault (marked V) once you reach the bottom-right room:

+
#########
+#S| | | #
+#-#-#-#-#
+# | | | #
+#-#-#-#-#
+# | | | #
+#-#-#-#-#
+# | | |  
+####### V
+
+

Fixed walls are marked with #, and doors are marked with - or |.

+

The doors in your current room are either open or closed (and locked) based on the hexadecimal MD5 hash of a passcode (your puzzle input) followed by a sequence of uppercase characters representing the path you have taken so far (U for up, D for down, L for left, and R for right).

+

Only the first four characters of the hash are used; they represent, respectively, the doors up, down, left, and right from your current position. Any b, c, d, e, or f means that the corresponding door is open; any other character (any number or a) means that the corresponding door is closed and locked.

+

To access the vault, all you need to do is reach the bottom-right room; reaching this room opens the vault and all doors in the maze.

+

For example, suppose the passcode is hijkl. Initially, you have taken no steps, and so your path is empty: you simply find the MD5 hash of hijkl alone. The first four characters of this hash are ced9, which indicate that up is open (c), down is open (e), left is open (d), and right is closed and locked (9). Because you start in the top-left corner, there are no "up" or "left" doors to be open, so your only choice is down.

+

Next, having gone only one step (down, or D), you find the hash of hijklD. This produces f2bc, which indicates that you can go back up, left (but that's a wall), or right. Going right means hashing hijklDR to get 5745 - all doors closed and locked. However, going up instead is worthwhile: even though it returns you to the room you started in, your path would then be DU, opening a different set of doors.

+

After going DU (and then hashing hijklDU to get 528e), only the right door is open; after going DUR, all doors lock. (Fortunately, your actual passcode is not hijkl).

+

Passcodes actually used by Easter Bunny Vault Security do allow access to the vault if you know the right path. For example:

+
    +
  • If your passcode were ihgpwlah, the shortest path would be DDRRRD.
  • +
  • With kglvqrro, the shortest path would be DDUDRLRRUDRD.
  • +
  • With ulqzkmiv, the shortest would be DRURDRUDDLLDLUURRDULRLDUUDDDRR.
  • +
+

Given your vault's passcode, what is the shortest path (the actual path, not just the length) to reach the vault?

+
+

Your puzzle answer was DRLRDDURDR.

--- Part Two ---

You're curious how robust this security solution really is, and so you decide to find longer and longer paths which still provide access to the vault. You remember that paths always end the first time they reach the bottom-right room (that is, they can never pass through it, only end in it).

+

For example:

+
    +
  • If your passcode were ihgpwlah, the longest path would take 370 steps.
  • +
  • With kglvqrro, the longest path would be 492 steps long.
  • +
  • With ulqzkmiv, the longest path would be 830 steps long.
  • +
+

+

What is the length of the longest path that reaches the vault?

+
+

Your puzzle answer was 500.

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.

+

Your puzzle input was qljzarfv.

+

You can also this puzzle.

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