From 752e5c1f3443a891aae44c3290d33a9e28fb618f Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sat, 10 Dec 2022 13:42:45 +0530 Subject: [PATCH] day 10 Signed-off-by: Amneesh Singh --- README | 1 + day10.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 README create mode 100644 day10.hs diff --git a/README b/README new file mode 100644 index 0000000..6f67ec1 --- /dev/null +++ b/README @@ -0,0 +1 @@ +My advent of code solutions for 2022. I am trying to not use String wherever possible and use Data.Text instead. That makes the programs appear bigger than they are LOC-wise. diff --git a/day10.hs b/day10.hs new file mode 100644 index 0000000..992d004 --- /dev/null +++ b/day10.hs @@ -0,0 +1,40 @@ +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE ViewPatterns #-} + +import Data.Text (Text) +import qualified Data.Text as T (lines, pack, unlines, words) +import qualified Data.Text.IO as T (putStr) +import qualified Data.Text.Read as T (decimal, signed) +import Lib (readFile') + +main :: IO () +main = do + vals <- + scanl1 (+) + . reverse + . foldl + ( \xs x -> + let o : n = T.words x + dec (T.signed T.decimal . head -> Right (m, "")) = m + in if o == "noop" then 0 : xs else dec n : 0 : xs + ) + [1] + . T.lines + <$> readFile' "day10.in" + putStr "Q1: " + print $ q1 vals + putStrLn "Q2: " + T.putStr $ q2 vals + +q1 :: [Int] -> Int +q1 v = foldr (\x xs -> xs + (v !! (x - 1) * x)) 0 [20, 60 .. 220] + +q2 :: [Int] -> Text +q2 v = + T.unlines $ + map + ( \x -> + T.pack $ + map (\y -> if abs (v !! (x + y) - y) <= 1 then '#' else '.') [0 .. 39] + ) + [0, 40 .. 200]