From 472ed5b6094c2b27092085084ce66f40c93c57ed Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 26 Dec 2016 12:15:16 +0000 Subject: [PATCH 1/1] Done day 25 --- adventofcode1625/adventofcode1625.cabal | 48 ++++++ adventofcode1625/app/advent25.hs | 206 ++++++++++++++++++++++++ data/advent25.txt | 30 ++++ day25.html | 148 +++++++++++++++++ stack.yaml | 1 + 5 files changed, 433 insertions(+) create mode 100644 adventofcode1625/adventofcode1625.cabal create mode 100644 adventofcode1625/app/advent25.hs create mode 100644 data/advent25.txt create mode 100644 day25.html diff --git a/adventofcode1625/adventofcode1625.cabal b/adventofcode1625/adventofcode1625.cabal new file mode 100644 index 0000000..5523756 --- /dev/null +++ b/adventofcode1625/adventofcode1625.cabal @@ -0,0 +1,48 @@ +name: adventofcode1625 +version: 0.1.0.0 +synopsis: Initial project template from stack +description: Please see README.md +homepage: https://github.com/neilnjae/adventofcode16#readme +license: BSD3 +license-file: LICENSE +author: Neil Smith +maintainer: noone@njae.me.uk +copyright: 2016 Neil Smith +category: None +build-type: Simple +extra-source-files: README.md +cabal-version: >=1.10 + +library + hs-source-dirs: src + build-depends: base >= 4.7 && < 5 + default-language: Haskell2010 + +executable advent25 + hs-source-dirs: app + main-is: advent25.hs + ghc-options: -O2 -threaded -rtsopts -with-rtsopts=-N + build-depends: base + , adventofcode1625 + , adventofcode16 + , parsec + , parsec-numbers + , mtl + default-language: Haskell2010 + +test-suite adventofcode1625-test + type: exitcode-stdio-1.0 + hs-source-dirs: test + main-is: Spec.hs + build-depends: base + , adventofcode1625 + , adventofcode16 + , parsec + , parsec-numbers + , mtl + ghc-options: -threaded -rtsopts -with-rtsopts=-N + default-language: Haskell2010 + +source-repository head + type: git + location: https://github.com/neilnjae/adventofcode16 diff --git a/adventofcode1625/app/advent25.hs b/adventofcode1625/app/advent25.hs new file mode 100644 index 0000000..077201f --- /dev/null +++ b/adventofcode1625/app/advent25.hs @@ -0,0 +1,206 @@ +module Main(main) where + +import Text.Parsec hiding (State) +import Text.ParserCombinators.Parsec.Number +import Control.Monad.State.Lazy + +-- import Control.Monad.Writer +import Control.Monad.Reader +import Debug.Trace + +data Location = Literal Int | Register Char deriving (Show, Eq) +data Instruction = Cpy Location Location + | Inc Location + | Dec Location + | Jnz Location Location + | Tgl Location + | Out Location + deriving (Show, Eq) + +data Machine = Machine { a :: Int + , b :: Int + , c :: Int + , d :: Int + , pc :: Int + , instructions :: [Instruction] + , execCount :: Int + } + deriving (Show, Eq) + +data AppConfig = AppConfig { cfgMaxRun :: Int } deriving (Show) + + +type App = ReaderT AppConfig (State Machine) String + + +testInstructions1 = "\ +\cpy 5 d\n\ +\cpy 7 c\n\ +\cpy 362 b\n\ +\inc d\n\ +\dec b\n\ +\jnz b -2\n\ +\dec c\n\ +\jnz c -5\n\ +\out d" + +testInstructions2 = "jnz 1 0" + +target :: String +target = cycle "01" + +emptyMachine :: Machine +emptyMachine = Machine {a=0, b=0, c=0, d=0, pc=0, instructions=[], execCount=0} + +main :: IO () +main = do + text <- readFile "data/advent25.txt" + -- let text = testInstructions1 + let instructions = successfulParse $ parseIfile text + part1 instructions + + +part1 :: [Instruction] -> IO () +part1 instrs = + print $ head validInputs + where m0 = emptyMachine {instructions=instrs} + inputs = [0..] + validInputs = filter (validMachine) inputs + validMachine i = valid $ evalMachine m0 i + -- let config = AppConfig {cfgMaxRun = 500000} + -- m0 = emptyMachine {instructions=instrs} + -- -- res = runState (runReaderT (runMachine "") config) st + -- res = evalState (runReaderT (runMachine "") config) m0 + -- in print res -- (signal, st') + + +valid :: String -> Bool +valid output = all (\p -> fst p == snd p) $ zip target output + +evalMachine :: Machine -> Int -> String +evalMachine machine0 input = evalState (runReaderT (runMachine "") config) m + where m = machine0 {a = input} + config = AppConfig {cfgMaxRun = 500000} + +runMachine :: String -> App -- State Machine String +runMachine output = + do cfg <- ask + m <- get + if (pc m) >= (length $ instructions m) || execCount m > cfgMaxRun cfg + then return output + else do thisOutput <- executeStep + runMachine (output ++ thisOutput) + + +executeStep :: App -- State Machine String +executeStep = + do m <- get + let i = (instructions m)!!(pc m) + let output = generateOutput i m + put (executeInstructionPeep i m) {execCount = (execCount m) + 1} + -- put (executeInstruction i m) {execCount = (execCount m) + 1} + return output + +generateOutput :: Instruction -> Machine -> String +generateOutput (Out a) m = show $ evaluate m a +generateOutput _ _ = "" + +executeInstructionPeep :: Instruction -> Machine -> Machine +executeInstructionPeep i m = + if sample == sampleTarget + -- then trace ("Peeping 1 " ++ (show m) ++ " to " ++ (show m1)) m1 + then m1 + else executeInstruction i m + where sample = take (length sampleTarget) $ drop (pc m) $ instructions m + sampleTarget = [ Inc (Register 'd') + , Dec (Register 'b') + , Jnz (Register 'b') (Literal (-2)) + , Dec (Register 'c') + , Jnz (Register 'c') (Literal (-5)) ] + m1 = m {d = d m + c m * b m, c = 0, b = 0, pc = pc m + (length sample)} + + +executeInstruction :: Instruction -> Machine -> Machine +executeInstruction (Inc r@(Register _)) m = m' {pc=pc1} + where pc1 = (pc m) + 1 + v = evaluate m r + m' = writeValue m r (v+1) +executeInstruction (Inc (Literal _)) m = m {pc=pc1} + where pc1 = (pc m) + 1 +executeInstruction (Dec r@(Register _)) m = m' {pc=pc1} + where pc1 = (pc m) + 1 + v = evaluate m r + m' = writeValue m r (v-1) +executeInstruction (Dec (Literal _)) m = m {pc=pc1} + where pc1 = (pc m) + 1 +executeInstruction (Cpy s d@(Register _)) m = m' {pc=pc1} + where pc1 = (pc m) + 1 + v = evaluate m s + m' = writeValue m d v +executeInstruction (Cpy s (Literal _)) m = m {pc=pc1} + where pc1 = (pc m) + 1 +executeInstruction (Jnz s d) m + | v == 0 = m {pc=pc1} + | otherwise = m {pc=pcj} + where pc1 = (pc m) + 1 + ed = evaluate m d + pcj = (pc m) + ed + v = evaluate m s +executeInstruction (Tgl a) m + | v < (length $ instructions m) = m {instructions = (replace (instructions m) i' v), + pc=pc1} + | otherwise = m {pc=pc1} + where pc1 = pc m + 1 + v = evaluate m a + pc m + i = (instructions m)!!v + i' = case i of + Inc x -> Dec x + Dec x -> Inc x + Tgl x -> Inc x + Cpy x y -> Jnz x y + Jnz x y -> Cpy x y + replace xs x i = take i xs ++ [x] ++ drop (i+1) xs +executeInstruction (Out _) m = m {pc = pc m + 1} + + +evaluate :: Machine -> Location -> Int +evaluate _ (Literal i) = i +evaluate m (Register r) = + case r of + 'a' -> (a m) + 'b' -> (b m) + 'c' -> (c m) + 'd' -> (d m) + +writeValue :: Machine -> Location -> Int -> Machine +writeValue m (Literal i) _ = m +writeValue m (Register r) v = + case r of + 'a' -> m {a=v} + 'b' -> m {b=v} + 'c' -> m {c=v} + 'd' -> m {d=v} + + +instructionFile = instructionLine `sepEndBy` newline +instructionLine = incL <|> decL <|> cpyL <|> jnzL <|> tglL <|> outL + +incL = Inc <$> (string "inc" *> spaces *> register) +decL = Dec <$> (string "dec" *> spaces *> register) +cpyL = Cpy <$> (string "cpy" *> spaces *> location) <*> (spaces *> register) +jnzL = Jnz <$> (string "jnz" *> spaces *> location) <*> (spaces *> location) +tglL = Tgl <$> (string "tgl" *> spaces *> location) +outL = Out <$> (string "out" *> spaces *> location) + +location = (Literal <$> int) <|> register +register = Register <$> (oneOf "abcd") + +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/data/advent25.txt b/data/advent25.txt new file mode 100644 index 0000000..ae27d4b --- /dev/null +++ b/data/advent25.txt @@ -0,0 +1,30 @@ +cpy a d +cpy 7 c +cpy 362 b +inc d +dec b +jnz b -2 +dec c +jnz c -5 +cpy d a +jnz 0 0 +cpy a b +cpy 0 a +cpy 2 c +jnz b 2 +jnz 1 6 +dec b +dec c +jnz c -4 +inc a +jnz 1 -7 +cpy 2 b +jnz c 2 +jnz 1 4 +dec b +dec c +jnz 1 -4 +jnz 0 0 +out b +jnz a -19 +jnz 1 -21 \ No newline at end of file diff --git a/day25.html b/day25.html new file mode 100644 index 0000000..e4cb6e0 --- /dev/null +++ b/day25.html @@ -0,0 +1,148 @@ + + + + +Day 25 - Advent of Code 2016 + + + + + + +

Advent of Code

Neil Smith (AoC++) 50*

      /*2016*/

+ + + +
+

--- Day 25: Clock Signal ---

You open the door and find yourself on the roof. The city sprawls away from you for miles and miles.

+

There's not much time now - it's already Christmas, but you're nowhere near the North Pole, much too far to deliver these stars to the sleigh in time.

+

However, maybe the huge antenna up here can offer a solution. After all, the sleigh doesn't need the stars, exactly; it needs the timing data they provide, and you happen to have a massive signal generator right here.

+

You connect the stars you have to your prototype computer, connect that to the antenna, and begin the transmission.

+

Nothing happens.

+

You call the service number printed on the side of the antenna and quickly explain the situation. "I'm not sure what kind of equipment you have connected over there," he says, "but you need a clock signal." You try to explain that this is a signal for a clock.

+

"No, no, a clock signal - timing information so the antenna computer knows how to read the data you're sending it. An endless, alternating pattern of 0, 1, 0, 1, 0, 1, 0, 1, 0, 1...." He trails off.

+

You ask if the antenna can handle a clock signal at the frequency you would need to use for the data from the stars. "There's no way it can! The only antenna we've installed capable of that is on top of a top-secret Easter Bunny installation, and you're definitely not-" You hang up the phone.

+

You've extracted the antenna's clock signal generation assembunny code (your puzzle input); it looks mostly compatible with code you worked on just recently.

+

This antenna code, being a signal generator, uses one extra instruction:

+
    +
  • out x transmits x (either an integer or the value of a register) as the next value for the clock signal.
  • +
+

The code takes a value (via register a) that describes the signal to generate, but you're not sure how it's used. You'll have to find the input to produce the right signal through experimentation.

+

What is the lowest positive integer that can be used to initialize register a and cause the code to output a clock signal of 0, 1, 0, 1... repeating forever?

+
+

Your puzzle answer was 196.

--- Part Two ---

The antenna is ready. Now, all you need is the fifty stars required to generate the signal for the sleigh, but you don't have enough.

+

You look toward the sky in desperation... suddenly noticing that a lone star has been installed at the top of the antenna! Only 49 more to go.

+
+

If you like, you can .

+

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

+

At this point, all that is left is for you to admire your advent calendar.

+

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

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file diff --git a/stack.yaml b/stack.yaml index b51ba57..cf9451a 100644 --- a/stack.yaml +++ b/stack.yaml @@ -5,6 +5,7 @@ packages: - adventofcode1601 - adventofcode1602 - adventofcode1624 +- adventofcode1625 system-ghc: true extra-deps: - astar-0.3.0.0 -- 2.34.1