From 5ba9f9b0b8d1d4ca19dfd43cecc51074f0685eac Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Wed, 21 Dec 2016 15:40:54 +0000 Subject: [PATCH] Day 21 done --- advent21.hs | 193 +++++++++++++++++++++++++++++++++++++++++++++++++++ advent21.txt | 100 ++++++++++++++++++++++++++ day21.html | 155 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 448 insertions(+) create mode 100644 advent21.hs create mode 100644 advent21.txt create mode 100644 day21.html diff --git a/advent21.hs b/advent21.hs new file mode 100644 index 0000000..fdb1381 --- /dev/null +++ b/advent21.hs @@ -0,0 +1,193 @@ +import Text.Parsec hiding (State) +import Text.ParserCombinators.Parsec.Number +-- import Control.Applicative ((<*), (*>), (<*>)) +import Data.Maybe (fromJust) +import Data.List (elemIndex) + +import Control.Monad.Identity +import Control.Monad.State +import Control.Monad.Writer + +data Instruction = SwapPosition Int Int + | SwapLetter Char Char + | RotateSteps Int + | RotateLetter Char + | Reverse Int Int + | Move Int Int + deriving (Show, Eq) + +data Log = Log { + action :: String + } deriving (Show) + +data Password = Password { + password :: String + } deriving (Show) + + +type App = WriterT [Log] (StateT Password Identity) + +infixl 9 ?? + +(??) :: Eq a => [a] -> a -> Int +(??) items item = fromJust $ elemIndex item items + + +initial = "abcdefgh" +final = "fbgdceah" + +testInstructions = "\ +\swap position 4 with position 0\n\ +\swap letter d with letter b\n\ +\reverse positions 0 through 4\n\ +\rotate left 1 step\n\ +\move position 1 to position 4\n\ +\move position 3 to position 0\n\ +\rotate based on position of letter b\n\ +\rotate based on position of letter d\n" + +main :: IO () +main = do + -- let ti = successfulParse $ parseIfile testInstructions + -- part1 ti "abcde" + -- part2 (reverse ti) "decab" + text <- readFile "advent21.txt" + let instructions = successfulParse $ parseIfile text + part1 instructions initial + part2 (reverse instructions) final + +part1 :: [Instruction] -> String -> IO () +part1 instructions start = + let state = Password {password = start} + in print $ runIdentity (runStateT (runWriterT (apply instructions)) state) + -- in putStrLn $ password $ runIdentity (execStateT (runWriterT (apply instructions)) state) + +part2 :: [Instruction] -> String -> IO () +part2 instructions end = + let state = Password {password = end} + in print $ runIdentity (runStateT (runWriterT (unApply instructions)) state) + -- in putStrLn $ password $ runIdentity (execStateT (runWriterT (apply instructions)) state) + + +apply :: [Instruction] -> App () +apply [] = return () +apply (i:is) = + do st <- get + let p0 = password st + let p1 = applyInstruction i p0 + put st {password = p1} + tell [Log (p0 ++ " -> " ++ p1 ++ " : " ++ (show i))] + apply is + + +applyInstruction :: Instruction -> String -> String +applyInstruction (SwapPosition from to) p0 + | from == to = p0 + | otherwise = prefix ++ [p0!!end] ++ midfix ++ [p0!!start] ++ suffix + where start = minimum [from, to] + end = maximum [from, to] + prefix = take start p0 + midfix = take (end-start-1) $ drop (start+1) p0 + suffix = drop (end+1) p0 + +applyInstruction (SwapLetter l0 l1) p0 = applyInstruction (SwapPosition (p0??l0) (p0??l1)) p0 + +applyInstruction (RotateSteps n) p0 = (drop n' p0) ++ (take n' p0) + where n' = if n < 0 + then (-1 * n) + else (length p0) - n + +applyInstruction (RotateLetter l) p0 = applyInstruction (RotateSteps n) p0 + where n = (1 + (p0??l) + if (p0??l) >= 4 then 1 else 0) `mod` (length p0) + +applyInstruction (Reverse from to) p0 + | from == to = p0 + | otherwise = prefix ++ (reverse midfix) ++ suffix + where start = minimum [from, to] + end = maximum [from, to] + prefix = take start p0 + midfix = take (end-start+1) $ drop start p0 + suffix = drop (end+1) p0 + +applyInstruction (Move from to) p0 + | from == to = p0 + | otherwise = prefix ++ [p0!!from] ++ suffix + where without = take from p0 ++ drop (from+1) p0 + prefix = take to without + suffix = drop (to) without + + +unApply :: [Instruction] -> App () +unApply [] = return () +unApply (i:is) = + do st <- get + let p0 = password st + let p1 = unApplyInstruction i p0 + put st {password = p1} + tell [Log (p1 ++ " <- " ++ p0 ++ " : " ++ (show i))] + unApply is + +unApplyInstruction :: Instruction -> String -> String +unApplyInstruction (SwapPosition from to) p0 = applyInstruction (SwapPosition from to) p0 +unApplyInstruction (SwapLetter l0 l1) p0 = applyInstruction (SwapLetter l0 l1) p0 +unApplyInstruction (RotateSteps n) p0 = applyInstruction (RotateSteps (-1 * n)) p0 +unApplyInstruction (Reverse from to) p0 = applyInstruction (Reverse from to) p0 +unApplyInstruction (Move from to) p0 = applyInstruction (Move to from) p0 +unApplyInstruction (RotateLetter l) p0 = applyInstruction (RotateSteps n) p0 + where n = case (p0??l) of + 0 -> -1 + 1 -> -1 + 2 -> 2 + 3 -> -2 + 4 -> 1 + 5 -> -3 + 6 -> 0 + 7 -> -4 + -- where n = case (p0??l) of + -- 0 -> -1 + -- 1 -> -1 + -- 2 -> 1 + -- 3 -> -2 + -- 4 -> 1 + + +instructionFile = instructionLine `endBy` newline +instructionLine = choice [ swapL + , rotateL + , reverseL + , moveL + ] + +swapL = (try (string "swap ")) *> (swapPosL <|> swapLetterL) + +swapPosL = SwapPosition <$> (string "position" *> spaces *> int) + <*> (spaces *> string "with position" *> spaces *> int) + +swapLetterL = SwapLetter <$> (string "letter" *> spaces *> letter) + <*> (spaces *> string "with letter" *> spaces *> letter) + +rotateL = (try (string "rotate ")) *> (rotateDirL <|> rotateLetterL) + +rotateDirL = rotateStepify <$> ((string "left") <|> (string "right")) + <*> (spaces *> int <* spaces <* skipMany letter) + where rotateStepify dir n = case dir of + "left" -> (RotateSteps (-1 * n)) + "right" -> (RotateSteps n) +rotateLetterL = RotateLetter <$> (string "based on position of letter " *> letter) + +reverseL = Reverse <$> (string "reverse positions" *> spaces *> int) + <*> (spaces *> (string "through") *> spaces *> int) + +moveL = Move <$> (string "move position" *> spaces *> int) + <*> (spaces *> (string "to position") *> spaces *> int) + + +parseIfile :: String -> Either ParseError [Instruction] +parseIfile input = parse instructionFile "(unknown)" input + +parseIline :: String -> Either ParseError Instruction +parseIline input = parse instructionLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a diff --git a/advent21.txt b/advent21.txt new file mode 100644 index 0000000..b445817 --- /dev/null +++ b/advent21.txt @@ -0,0 +1,100 @@ +rotate based on position of letter a +swap letter g with letter d +move position 1 to position 5 +reverse positions 6 through 7 +move position 5 to position 4 +rotate based on position of letter b +reverse positions 6 through 7 +swap letter h with letter f +swap letter e with letter c +reverse positions 0 through 7 +swap position 6 with position 4 +rotate based on position of letter e +move position 2 to position 7 +swap position 6 with position 4 +rotate based on position of letter e +reverse positions 2 through 3 +rotate right 2 steps +swap position 7 with position 1 +move position 1 to position 2 +move position 4 to position 7 +move position 5 to position 0 +swap letter e with letter f +move position 4 to position 7 +reverse positions 1 through 7 +rotate based on position of letter g +move position 7 to position 4 +rotate right 6 steps +rotate based on position of letter g +reverse positions 0 through 5 +reverse positions 0 through 7 +swap letter c with letter f +swap letter h with letter f +rotate right 7 steps +rotate based on position of letter g +rotate based on position of letter c +swap position 1 with position 4 +move position 7 to position 3 +reverse positions 2 through 6 +move position 7 to position 0 +move position 7 to position 1 +move position 6 to position 7 +rotate right 5 steps +reverse positions 0 through 6 +move position 1 to position 4 +rotate left 3 steps +swap letter d with letter c +move position 4 to position 5 +rotate based on position of letter f +rotate right 1 step +move position 7 to position 6 +swap position 6 with position 0 +move position 6 to position 2 +rotate right 1 step +swap position 1 with position 6 +move position 2 to position 6 +swap position 2 with position 1 +reverse positions 1 through 7 +move position 4 to position 1 +move position 7 to position 0 +swap position 6 with position 7 +rotate left 1 step +reverse positions 0 through 4 +rotate based on position of letter c +rotate based on position of letter b +move position 2 to position 1 +rotate right 0 steps +swap letter b with letter d +swap letter f with letter c +swap letter d with letter a +swap position 7 with position 6 +rotate right 0 steps +swap position 0 with position 3 +swap position 2 with position 5 +swap letter h with letter f +reverse positions 2 through 3 +rotate based on position of letter c +rotate left 2 steps +move position 0 to position 5 +swap position 2 with position 3 +rotate right 1 step +rotate left 2 steps +move position 0 to position 4 +rotate based on position of letter c +rotate based on position of letter g +swap position 3 with position 0 +rotate right 3 steps +reverse positions 0 through 2 +move position 1 to position 2 +swap letter e with letter c +rotate right 7 steps +move position 0 to position 7 +rotate left 2 steps +reverse positions 0 through 4 +swap letter e with letter b +reverse positions 2 through 7 +rotate right 5 steps +swap position 2 with position 4 +swap letter d with letter g +reverse positions 3 through 4 +reverse positions 4 through 5 diff --git a/day21.html b/day21.html new file mode 100644 index 0000000..d0e4684 --- /dev/null +++ b/day21.html @@ -0,0 +1,155 @@ + + + + +Day 21 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 42*

       y(2016)

+ + + +
+

--- Day 21: Scrambled Letters and Hash ---

The computer system you're breaking into uses a weird scrambling function to store its passwords. It shouldn't be much trouble to create your own scrambled password so you can add it to the system; you just have to implement the scrambler.

+

The scrambling function is a series of operations (the exact list is provided in your puzzle input). Starting with the password to be scrambled, apply each operation in succession to the string. The individual operations behave as follows:

+
    +
  • swap position X with position Y means that the letters at indexes X and Y (counting from 0) should be swapped.
  • +
  • swap letter X with letter Y means that the letters X and Y should be swapped (regardless of where they appear in the string).
  • +
  • rotate left/right X steps means that the whole string should be rotated; for example, one right rotation would turn abcd into dabc.
  • +
  • rotate based on position of letter X means that the whole string should be rotated to the right based on the index of letter X (counting from 0) as determined before this instruction does any rotations. Once the index is determined, rotate the string to the right one time, plus a number of times equal to that index, plus one additional time if the index was at least 4.
  • +
  • reverse positions X through Y means that the span of letters at indexes X through Y (including the letters at X and Y) should be reversed in order.
  • +
  • move position X to position Y means that the letter which is at index X should be removed from the string, then inserted such that it ends up at index Y.
  • +
+

For example, suppose you start with abcde and perform the following operations:

+
    +
  • swap position 4 with position 0 swaps the first and last letters, producing the input for the next step, ebcda.
  • +
  • swap letter d with letter b swaps the positions of d and b: edcba.
  • +
  • reverse positions 0 through 4 causes the entire string to be reversed, producing abcde.
  • +
  • rotate left 1 step shifts all letters left one position, causing the first letter to wrap to the end of the string: bcdea.
  • +
  • move position 1 to position 4 removes the letter at position 1 (c), then inserts it at position 4 (the end of the string): bdeac.
  • +
  • move position 3 to position 0 removes the letter at position 3 (a), then inserts it at position 0 (the front of the string): abdec.
  • +
  • rotate based on position of letter b finds the index of letter b (1), then rotates the string right once plus a number of times equal to that index (2): ecabd.
  • +
  • rotate based on position of letter d finds the index of letter d (4), then rotates the string right once, plus a number of times equal to that index, plus an additional time because the index was at least 4, for a total of 6 right rotations: decab.
  • +
+

After these steps, the resulting scrambled password is decab.

+

Now, you just need to generate a new scrambled password and you can access the system. Given the list of scrambling operations in your puzzle input, what is the result of scrambling abcdefgh?

+
+

Your puzzle answer was aefgbcdh.

--- Part Two ---

You scrambled the password correctly, but you discover that you can't actually modify the password file on the system. You'll need to un-scramble one of the existing passwords by reversing the scrambling process.

+

What is the un-scrambled version of the scrambled password fbgdceah?

+
+

Your puzzle answer was egcdahbf.

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