aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <me@ypei.me>2017-12-19 12:34:59 +0100
committerYuchen Pei <me@ypei.me>2017-12-19 12:34:59 +0100
commit4595a84ff049cb25fa2189761419c495c1921305 (patch)
treed7a0617826e42cf309aa939e779900349e5b1674
parent4e4ede03629d26edf6f9f0e3bbf8098535b93cfc (diff)
finished Day 18.
-rw-r--r--Puzzle18.hs17
1 files changed, 11 insertions, 6 deletions
diff --git a/Puzzle18.hs b/Puzzle18.hs
index 0f541e6..c339005 100644
--- a/Puzzle18.hs
+++ b/Puzzle18.hs
@@ -24,6 +24,9 @@ alphabet = ['a'..'z']
initRegs :: Map Char Int
initRegs = Map.fromList $ zipWith (,) ['a'..'z'] (cycle [0])
+initRegs' :: Map Char Int
+initRegs' = Map.insert 'p' 1 initRegs
+
solve1 :: [Char] -> Int
solve1 xs = exec (splitOn "\n" xs) 0 initRegs
@@ -35,22 +38,24 @@ exec' me n myAddr myRegs myQueue myState theirAddr theirRegs theirQueue theirSta
| 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 == "snd" = exec' me (n + me) (myAddr + 1) myRegs myQueue myState theirAddr theirRegs (theirQueue ++ [dval]) theirState
+ | op == "rcv" && null myQueue && null theirQueue && theirState /= Active = 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
+ op:dest':xs = splitOn " " ins
+ dval = if head dest' `elem` alphabet then myRegs Map.! (head dest') else read dest'
+ dest = head dest'
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
+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"
+input1 = "snd 1\nsnd 2\nsnd p\nrcv a\nrcv b\nrcv c\nrcv d"
+
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"