### Moved Intcode to a module

master
Micah Halter 1 year ago
parent
commit
4a0ee0b65c
15 changed files with 115 additions and 317 deletions
1. 55
00-utils/Intcode.hs
2. 53
3. 2
02-1202-program-alarm/run.sh
4. 43
5. 2
05-sunny-with-a-chance-of-asteroids/run.sh
6. 61
7. 2
07-amplification-circuit/run.sh
8. 67
9. 2
09-sensor-boost/run.sh
10. 51
11. 2
11-space-police/run.sh
12. 44
13. 2
13-care-package/run.sh
14. 44
15. 2
15-oxygen-system/run.sh

#### 2 02-1202-program-alarm/run.sh View File

 @ -1,3 +1,3 @@ #!/bin/sh   runhaskell answer.hs < input runhaskell -i$(dirname$(pwd))/00-utils answer.hs < input

 @ -1,43 +1,10 @@ import Data.Sequence import System.Environment import Data.List.Split   parse :: String -> Seq Int parse = fromList . map (read :: String -> Int) . splitOn ","   revDigits :: Int -> [Int] revDigits = Prelude.reverse . Prelude.map (read . return) . show   compute :: Seq Int -> Int -> IO (Seq Int) compute x y = do  let indexOfIndex y = index x $index x y  let digits = revDigits$ x index y  let op = (if Prelude.length digits > 1 then 10 * digits!!1 else 0) + digits!!0  let left = if Prelude.length digits > 2 && digits!!2 == 1 then x index (y+1) else indexOfIndex (y+1)  let right = if Prelude.length digits > 3 && digits!!3 == 1 then x index (y+2) else indexOfIndex (y+2)  let dest = if op == 3 then x index (y+1) else x index (y+3)  if op == 99  then return (x)  else if op == 1 || op == 2  then compute (update dest ((if op == 1 then (+) else (*)) left right) x) (y+4)  else if op == 3  then do  print "Input:"  input <- getLine  compute (update dest (read input::Int) x) (y+2)  else if op == 4  then do  print "Output:"  print left  compute x (y+2)  else if op == 5 || op == 6  then compute x (if (if op == 5 then (/=) else (==)) left 0 then right else (y+3))  else if op == 7 || op == 8  then compute (update dest (if (if op == 7 then (<) else (==))left right then 1 else 0) x) (y+4)  else  return (fromList []) import Intcode   main = do  input <- getArgs  let x = parse $input!!0  x compute 0 >>= print  let parsed = parseProg$ input!!0    print $flip (!!) 0$ computeTilHalt $startingState parsed [1]  print$ flip (!!) 0 $computeTilHalt$ startingState parsed [5]

#### 2 05-sunny-with-a-chance-of-asteroids/run.sh View File

 @ -1,3 +1,3 @@ #!/bin/sh   runhaskell answer.hs < input runhaskell -i$(dirname$(pwd))/00-utils answer.hs < input

#### 2 11-space-police/run.sh View File

 @ -1,3 +1,3 @@ #!/bin/sh   runhaskell answer.hs < input runhaskell -i$(dirname$(pwd))/00-utils answer.hs < input

 @ -3,46 +3,7 @@ import Data.List import Data.List.Split import qualified Data.Map.Strict as Map   parse :: String -> Map.Map Int Int parse str = Map.fromList $Data.List.zip [0..]$ map (read :: String -> Int) $splitOn "," str   revDigits :: Int -> [Int] revDigits = Prelude.reverse . Prelude.map (read . return) . show   compute :: (Map.Map Int Int, Int, Int) -> ([Int], [Int]) -> ((Map.Map Int Int, Int, Int), ([Int], [Int])) compute (x, y, z) (input_orig, output) =  if op == 99  then ((x, -1, z), (input_orig, output))  else if op == 1 || op == 2  then compute ((Map.insert dest ((if op == 1 then (+) else (*)) left right) x), (y+4), z) (input_orig, output)  else if op == 3  then compute ((Map.insert dest input x), (y+2), z) (inputs, output)  else if op == 4  then ((x, y+2, z), (input_orig, (left:output)))  else if op == 5 || op == 6  then compute (x, (if (if op == 5 then (/=) else (==)) left 0 then right else (y+3)), z) (input_orig, output)  else if op == 7 || op == 8  then compute ((Map.insert dest (if (if op == 7 then (<) else (==)) left right then 1 else 0) x), (y+4), z) (input_orig, output)  else if op == 9  then compute (x, y+2, z+left) (input_orig, output)  else  ((Map.empty, -1, z), (input_orig, output))  where  (input:inputs) = if Prelude.length input_orig > 0 then input_orig else (0:input_orig)  indexOfIndex y = Map.findWithDefault 0 (x Map.! y) x  digits = revDigits$ x Map.! y  op = (if Prelude.length digits > 1 then 10 * digits!!1 else 0) + digits!!0  left = if Prelude.length digits > 2 && digits!!2 == 1 then x Map.! (y+1)  else if Prelude.length digits > 2 && digits!!2 == 2 then x Map.! (z+(x Map.! (y+1)))  else indexOfIndex (y+1)  right = if Prelude.length digits > 3 && digits!!3 == 1 then x Map.! (y+2)  else if Prelude.length digits > 3 && digits!!3 == 2 then x Map.! (z+(x Map.! (y+2)))  else indexOfIndex (y+2)  dest = if op == 3 then (if Prelude.length digits > 2 && digits!!2 == 2 then z+(x Map.! (y+1)) else x Map.! (y+1))  else (if Prelude.length digits > 4 && digits!!4 == 2 then z else 0)+(x Map.! (y+3))   startingState :: Map.Map Int Int -> [Int] -> ((Map.Map Int Int, Int, Int), ([Int], [Int])) startingState prog input = ((prog, 0, 0), (input, [])) import Intcode   getElements :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> [((Int, Int), Int)] getElements ((prog, y, z), (inputs, outputs)) = if y == -1 then map (\[x, y, i] -> ((x, y), i)) $Data.List.Split.chunksOf 3$ Data.List.reverse outputs else getElements $compute (prog, y, z) (inputs, outputs) @ -53,10 +14,9 @@ solveA = Prelude.length . Data.List.filter (\(pos, i) -> i == 2) solveB :: [((Int, Int), Int)] -> Int solveB = snd . last     main = do  input <- getArgs  let parsed = parse$ input!!0  let parsed = parseProg $input!!0    let output = getElements$ startingState parsed []  print $solveA output #### 2 13-care-package/run.sh View File  @ -1,3 +1,3 @@ #!/bin/sh   runhaskell answer.hs < input runhaskell -i$(dirname $(pwd))/00-utils answer.hs < input #### 44 15-oxygen-system/answer.hs View File  @ -2,49 +2,9 @@ import System.Environment import Data.List as List import Data.Maybe import Data.Ord import Data.List.Split import qualified Data.Map.Strict as Map   parse :: String -> Map.Map Int Int parse str = Map.fromList$ List.zip [0..] $map (read :: String -> Int)$ splitOn "," str   revDigits :: Int -> [Int] revDigits = Prelude.reverse . Prelude.map (read . return) . show   compute :: (Map.Map Int Int, Int, Int) -> ([Int], [Int]) -> ((Map.Map Int Int, Int, Int), ([Int], [Int])) compute (x, y, z) (input_orig, output) =  if op == 99  then ((x, -1, z), (input_orig, output))  else if op == 1 || op == 2  then compute ((Map.insert dest ((if op == 1 then (+) else (*)) left right) x), (y+4), z) (input_orig, output)  else if op == 3  then compute ((Map.insert dest input x), (y+2), z) (inputs, output)  else if op == 4  then ((x, y+2, z), (input_orig, (left:output)))  else if op == 5 || op == 6  then compute (x, (if (if op == 5 then (/=) else (==)) left 0 then right else (y+3)), z) (input_orig, output)  else if op == 7 || op == 8  then compute ((Map.insert dest (if (if op == 7 then (<) else (==)) left right then 1 else 0) x), (y+4), z) (input_orig, output)  else if op == 9  then compute (x, y+2, z+left) (input_orig, output)  else  ((Map.empty, -1, z), (input_orig, output))  where  (input:inputs) = if Prelude.length input_orig > 0 then input_orig else (0:input_orig)  indexOfIndex y = Map.findWithDefault 0 (x Map.! y) x  digits = revDigits $x Map.! y  op = (if Prelude.length digits > 1 then 10 * digits!!1 else 0) + digits!!0  left = if Prelude.length digits > 2 && digits!!2 == 1 then x Map.! (y+1)  else if Prelude.length digits > 2 && digits!!2 == 2 then x Map.! (z+(x Map.! (y+1)))  else indexOfIndex (y+1)  right = if Prelude.length digits > 3 && digits!!3 == 1 then x Map.! (y+2)  else if Prelude.length digits > 3 && digits!!3 == 2 then x Map.! (z+(x Map.! (y+2)))  else indexOfIndex (y+2)  dest = if op == 3 then (if Prelude.length digits > 2 && digits!!2 == 2 then z+(x Map.! (y+1)) else x Map.! (y+1))  else (if Prelude.length digits > 4 && digits!!4 == 2 then z else 0)+(x Map.! (y+3))   startingState :: Map.Map Int Int -> [Int] -> ((Map.Map Int Int, Int, Int), ([Int], [Int])) startingState prog input = ((prog, 0, 0), (input, [])) import Intcode   getPoints :: ((Map.Map Int Int, Int, Int), ([Int], [Int])) -> Map.Map (Int, Int) Int -> (Int, Int) -> Map.Map (Int, Int) Int getPoints ((prog, y, z), (inputs, outputs)) map pos = if y == -1 then map else if lastObj == 0 then map else List.foldl Map.union Map.empty$ List.map (\(i, p) -> calcNext ((prog, y, z), (inputs, outputs)) map i p) (unmappedNeighbors map pos) @ -67,7 +27,7 @@ getMaxDis map pos item = snd $maximumBy (comparing snd)$ Map.elems $Map.filte   main = do  input <- getArgs  let parsed = parse$ input!!0  let parsed = parseProg $input!!0    let map = getPoints (startingState parsed []) (Map.singleton (0, 0) 1) (0, 0)  print$ getMaxDis map (0, 0) 2
 @ -1,3 +1,3 @@ #!/bin/sh   runhaskell answer.hs < input runhaskell -i$(dirname$(pwd))/00-utils answer.hs < input