This commit is contained in:
shu 2019-12-06 01:17:28 +01:00
parent c05e4b4081
commit dd2bf86d1c
2 changed files with 74 additions and 0 deletions

73
2019/day5/day5.hs Normal file

@ -0,0 +1,73 @@
import Data.List.Split
import qualified Data.List as L
import Data.Vector as V
import Data.Char
import System.Exit
main = do
content <- readFile "input"
let input = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
exec_step (input,0)
day5 :: Vector Int -> Vector Int
day5 x = V.take 20 x
type Tape = Vector Int
type TapeSection = Vector Int
type Pointer = Int
type TuringMachine = (Tape, Pointer)
opl x
| n`L.elem`"1278"=4
| n`L.elem`"56"=3
| n`L.elem`"34"=2
| otherwise=1
where n = L.last $ show x
parsemodes :: String -> [Bool]
parsemodes m = L.replicate (3 - L.length l) False L.++ l
where l = L.map (toEnum . digitToInt) m
paramch :: [Bool] -> TapeSection -> Tape -> TapeSection
paramch m opvec t = imap f (V.tail opvec)
where f i a = if (L.reverse m) !! i then a else t ! a
getopmodes opvec = (op_dedup,parsed_modes)
where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0
parsed_modes = parsemodes $ L.reverse modes
op_dedup = if L.last op == '0' then [L.head op] else op
step opvec (t,p) = do
let (op,m) = getopmodes opvec
input <- if op=="3"
then do
line <- getLine
return (read line)
else return(0)
let params = paramch m opvec t
newtm <- case op of
"1" -> return ((t // [((V.last opvec), (params ! 0) + (params ! 1))]),p)
"2" -> return ((t // [((V.last opvec), (params ! 0) * (params ! 1))]),p)
"3" -> return ((t // [((V.last opvec), input)]),p)
"4" -> do a <- print (V.last $ params)
return (t,p)
"5" -> return (t, if (params ! 0)/=0 then (params ! 1) else p)
"6" -> return (t, if (params ! 0)==0 then (params ! 1) else p)
"7" -> return ((t // [((V.last opvec), if (params ! 0)<(params ! 1)
then 1 else 0)]),p)
"8" -> return ((t // [((V.last opvec), if (params ! 0)==(params ! 1)
then 1 else 0)]),p)
"99" -> exitSuccess
return newtm
exec_step (t,p) = do
let command_length = opl $ t ! p
let opvec = slice p command_length t
newtm <- step opvec (t,p+command_length)
exec_step newtm

1
2019/day5/input Normal file

@ -0,0 +1 @@
3,225,1,225,6,6,1100,1,238,225,104,0,1101,40,71,224,1001,224,-111,224,4,224,1002,223,8,223,101,7,224,224,1,224,223,223,1102,66,6,225,1102,22,54,225,1,65,35,224,1001,224,-86,224,4,224,102,8,223,223,101,6,224,224,1,224,223,223,1102,20,80,225,101,92,148,224,101,-162,224,224,4,224,1002,223,8,223,101,5,224,224,1,224,223,223,1102,63,60,225,1101,32,48,225,2,173,95,224,1001,224,-448,224,4,224,102,8,223,223,1001,224,4,224,1,224,223,223,1001,91,16,224,101,-79,224,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1101,13,29,225,1101,71,70,225,1002,39,56,224,1001,224,-1232,224,4,224,102,8,223,223,101,4,224,224,1,223,224,223,1101,14,59,225,102,38,143,224,1001,224,-494,224,4,224,102,8,223,223,101,3,224,224,1,224,223,223,1102,30,28,224,1001,224,-840,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,107,677,226,224,1002,223,2,223,1005,224,329,1001,223,1,223,8,226,226,224,102,2,223,223,1006,224,344,101,1,223,223,7,226,677,224,1002,223,2,223,1005,224,359,101,1,223,223,1007,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,389,101,1,223,223,1008,226,226,224,1002,223,2,223,1005,224,404,1001,223,1,223,108,677,226,224,1002,223,2,223,1006,224,419,1001,223,1,223,1108,677,226,224,102,2,223,223,1006,224,434,1001,223,1,223,108,226,226,224,1002,223,2,223,1005,224,449,101,1,223,223,7,677,677,224,1002,223,2,223,1006,224,464,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,479,1001,223,1,223,107,226,226,224,102,2,223,223,1006,224,494,101,1,223,223,1007,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1107,226,677,224,102,2,223,223,1005,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,1107,677,226,224,102,2,223,223,1005,224,554,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,569,101,1,223,223,8,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,7,677,226,224,102,2,223,223,1006,224,599,101,1,223,223,1008,677,677,224,1002,223,2,223,1005,224,614,101,1,223,223,1008,677,226,224,102,2,223,223,1006,224,629,1001,223,1,223,1108,677,677,224,102,2,223,223,1006,224,644,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,659,1001,223,1,223,1107,226,226,224,102,2,223,223,1006,224,674,1001,223,1,223,4,223,99,226