From e7b2c8b88c502d51d85d304453e9b022bf0ea797 Mon Sep 17 00:00:00 2001 From: shu Date: Thu, 12 Dec 2019 17:45:43 +0100 Subject: [PATCH] Day 9: Formatting, Eta reduce --- 2019/day9/Intcode.hs | 5 +++-- 2019/day9/day9.hs | 14 ++++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/2019/day9/Intcode.hs b/2019/day9/Intcode.hs index fe54d84..27fa08e 100644 --- a/2019/day9/Intcode.hs +++ b/2019/day9/Intcode.hs @@ -88,7 +88,7 @@ step tm = case op of "1" -> tmBinop (+) "2" -> tmBinop (*) - "3" -> maybe tm{state = AwaitInput} getNewTM (input tm) + "3" -> maybe tm {state = AwaitInput} getNewTM (input tm) "4" -> tmn {output = V.last params : output tm} "5" -> tm @@ -133,7 +133,8 @@ step tm = {-without the following DeepSeq call, thunks build up eternally and the vectors won’t be garbage collected: >4GB RAM usage, god knows how much with larger tapes (my laptop crashed), now it’s a cozy ~20mB-} - getNewTM x = tmn {tape = DeepSeq.force (tape tm // [(target, x)]), state = Continue} + getNewTM x = + tmn {tape = DeepSeq.force (tape tm // [(target, x)]), state = Continue} target = fromInteger $ case m !! (length params - 1) of diff --git a/2019/day9/day9.hs b/2019/day9/day9.hs index 081583b..79bb18c 100644 --- a/2019/day9/day9.hs +++ b/2019/day9/day9.hs @@ -1,14 +1,14 @@ module Main where -import Intcode import Data.List.Split import qualified Data.Vector as V +import Intcode main :: IO () main = do content <- readFile "input" let program = V.fromList $ concatMap (map read . splitOn ",") (lines content) - let run x = + let run = runIntcode (TM { tape = tapePreprocess program @@ -17,7 +17,7 @@ main = do , output = [] , input = Nothing , state = Continue - }) x + }) let out1 = output $ run $ Just 1 let out2 = output $ run $ Just 2 print $ @@ -25,8 +25,10 @@ main = do ["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2] runIntcode :: TuringMachine -> Maybe Integer -> TuringMachine -runIntcode tm x = case state tmNew of +runIntcode tm x = + case state tmNew of Continue -> runIntcode tmNew x - AwaitInput -> runIntcode (tmNew{input = x}) x + AwaitInput -> runIntcode (tmNew {input = x}) x _ -> tmNew - where tmNew = execSteps tm + where + tmNew = execSteps tm