From 87f0c0d9d1b99e24ad59de51252f603b36b53681 Mon Sep 17 00:00:00 2001 From: Agatha Lovelace Date: Thu, 2 Dec 2021 21:24:58 +0200 Subject: [PATCH] Day 2 --- Day2.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ Day2_1.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 Day2.hs create mode 100644 Day2_1.hs diff --git a/Day2.hs b/Day2.hs new file mode 100644 index 0000000..fa76759 --- /dev/null +++ b/Day2.hs @@ -0,0 +1,43 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Day2 where + +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.Text (splitOn, Text, unpack, pack) + + +data Submarine = Submarine { + horizontal :: Int + , depth :: Int + } deriving Show + +type Movement = (String, Int) + +-- "forward 5" -> ("forward", 5) +parseInstruction :: String -> Movement +parseInstruction instr = (kind, magnitude) + where + split = map unpack $ splitOn " " $ pack instr + kind = head split + magnitude = read $ split !! 1 + +move :: Submarine -> Movement -> Submarine +move sub ("forward", magnitude) = sub { horizontal = horizontal sub + magnitude } +move sub ("down", magnitude) = sub { depth = depth sub + magnitude } +move sub ("up", magnitude) = sub { depth = depth sub - magnitude } + +(++) :: Submarine -> Submarine -> Submarine +x ++ y = Submarine { horizontal = horizontal x + horizontal y, depth = depth x + depth y } + +main :: IO () +main = do + input <- openFile "inputs/2.txt" ReadMode + contents <- hGetContents input + + let submarine = Submarine { horizontal = 0, depth = 0 } + let instructions = parseInstruction <$> lines contents + let sub = foldl move submarine instructions + + print $ horizontal sub * depth sub + + hClose input \ No newline at end of file diff --git a/Day2_1.hs b/Day2_1.hs new file mode 100644 index 0000000..3f300c9 --- /dev/null +++ b/Day2_1.hs @@ -0,0 +1,43 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Day2_1 where + +import System.IO (openFile, IOMode (ReadMode), hClose, hGetContents) +import Data.Text (splitOn, Text, unpack, pack) +import Data.List (mapAccumL) +import Prelude hiding ((++)) + + +data Submarine = Submarine { + horizontal :: Int + , depth :: Int + , aim :: Int + } deriving Show + +type Movement = (String, Int) + +-- "forward 5" -> ("forward", 5) +parseInstruction :: String -> Movement +parseInstruction instr = (kind, magnitude) + where + split = map unpack $ splitOn " " $ pack instr + kind = head split + magnitude = read $ split !! 1 + +move :: Submarine -> Movement -> Submarine +move sub ("forward", magnitude) = sub { depth = depth sub + (aim sub * magnitude), horizontal = horizontal sub + magnitude } +move sub ("down", magnitude) = sub { aim = aim sub + magnitude } +move sub ("up", magnitude) = sub { aim = aim sub - magnitude } + +main :: IO () +main = do + input <- openFile "inputs/2.txt" ReadMode + contents <- hGetContents input + + let submarine = Submarine { horizontal = 0, depth = 0, aim = 0 } + let instructions = parseInstruction <$> lines contents + let sub = foldl move submarine instructions + + print $ horizontal sub * depth sub + + hClose input \ No newline at end of file