aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-18 16:41:27 +0100
committerYuchen Pei <me@ypei.me>2017-12-18 16:41:27 +0100
commit4e4ede03629d26edf6f9f0e3bbf8098535b93cfc (patch)
tree6f5d6e85cd4d37a4ad2e0b7759cd785ff81769ba
parent88c7c35c5046f95f48afd786b89c7d42463e67b7 (diff)
attempted part 2 of day 18
-rw-r--r--Puzzle18.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/Puzzle18.hs b/Puzzle18.hs
index 4e92862..0f541e6 100644
--- a/Puzzle18.hs
+++ b/Puzzle18.hs
@@ -2,6 +2,8 @@ import Data.List.Split (splitOn)
import Data.Map (Map)
import qualified Data.Map as Map
+data State = Active | Inactive | Terminated deriving Eq
+
exec :: [[Char]] -> Int -> Map Char Int -> Int
exec rom addr regs
| op == "snd" = exec rom (addr + 1) (Map.insert '_' dval regs)
@@ -25,6 +27,30 @@ initRegs = Map.fromList $ zipWith (,) ['a'..'z'] (cycle [0])
solve1 :: [Char] -> Int
solve1 xs = exec (splitOn "\n" xs) 0 initRegs
+exec' me n myAddr myRegs myQueue myState theirAddr theirRegs theirQueue theirState
+ | myState == Terminated = n
+ | myAddr < 0 || myAddr >= length rom = exec' (1 - me) n theirAddr theirRegs theirQueue theirState myAddr myRegs myQueue Terminated
+ | op == "set" = exec' me n (myAddr + 1) (Map.insert dest val myRegs) myQueue myState theirAddr theirRegs theirQueue theirState
+ | op == "add" = exec' me n (myAddr + 1) (Map.insert dest (dval + val) myRegs) myQueue myState theirAddr theirRegs theirQueue theirState
+ | op == "mul" = exec' me n (myAddr + 1) (Map.insert dest (dval * val) myRegs) myQueue myState theirAddr theirRegs theirQueue theirState
+ | op == "mod" = exec' me n (myAddr + 1) (Map.insert dest (dval `mod` val) myRegs) myQueue myState theirAddr theirRegs theirQueue theirState
+ | op == "jgz" = exec' me n (if dval > 0 then myAddr + val else myAddr + 1) myRegs myQueue myState theirAddr theirRegs theirQueue theirState
+ | op == "snd" = exec' me (n + me) (myAddr + 1) myRegs myQueue myState theirAddr theirRegs (theirQueue ++ [sendval]) theirState
+ | op == "rcv" && null myQueue && null theirQueue && theirState == Inactive = n
+ | op == "rcv" && null myQueue = exec' (1 - me) n theirAddr theirRegs theirQueue theirState myAddr myRegs myQueue Inactive
+ | op == "rcv" = exec' me n (myAddr + 1) (Map.insert dest y myRegs) ys myState theirAddr theirRegs theirQueue theirState
+ where ins = rom !! myAddr
+ op:[dest]:xs = splitOn " " ins
+ dval = myRegs Map.! dest
+ sendval = if dest=='p' then me else dval
+ x = head xs
+ val = if head x `elem` alphabet then myRegs Map.! (head x) else read x
+ y:ys = myQueue
+
+solve2 = exec' 0 0 0 initRegs [] Active 0 initRegs [] Active
+
+rom = splitOn "\n" input
+
input0 = "set a 1\nadd a 2\nmul a a\nmod a 5\nsnd a\nset a 0\nrcv a\njgz a -1\nset a 1\njgz a -2"
input = "set i 31\nset a 1\nmul p 17\njgz p p\nmul a 2\nadd i -1\njgz i -2\nadd a -1\nset i 127\nset p 464\nmul p 8505\nmod p a\nmul p 129749\nadd p 12345\nmod p a\nset b p\nmod b 10000\nsnd b\nadd i -1\njgz i -9\njgz a 3\nrcv b\njgz b -1\nset f 0\nset i 126\nrcv a\nrcv b\nset p a\nmul p -1\nadd p b\njgz p 4\nsnd a\nset a b\njgz 1 3\nsnd b\nset f 1\nadd i -1\njgz i -11\nsnd a\njgz f -16\njgz a -19"