From 787691d6402edb42c4f8b0e509f59aee35820572 Mon Sep 17 00:00:00 2001 From: RhiobeT Date: Fri, 1 Dec 2023 17:25:07 +0100 Subject: [PATCH] Day 01 --- .gitignore | 6 ++++++ day01/Commons.hs | 20 ++++++++++++++++++++ day01/Main.hs | 11 +++++++++++ day01/Part1.hs | 14 ++++++++++++++ day01/Part2.hs | 36 ++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 .gitignore create mode 100644 day01/Commons.hs create mode 100644 day01/Main.hs create mode 100644 day01/Part1.hs create mode 100644 day01/Part2.hs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8c8fb3b --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +* + +!.gitignore + +!**/ +!*.hs diff --git a/day01/Commons.hs b/day01/Commons.hs new file mode 100644 index 0000000..e9b63c9 --- /dev/null +++ b/day01/Commons.hs @@ -0,0 +1,20 @@ +module Commons where + +import System.IO +import Text.Read + +type CalibrationLine = String + +type CalibrationDocument = [CalibrationLine] + +parseLine :: IO CalibrationLine +parseLine = do line <- getLine + return line + +parse :: IO CalibrationDocument +parse = do done <- isEOF + if done + then return [] + else do line <- parseLine + doc <- parse + return (line:doc) diff --git a/day01/Main.hs b/day01/Main.hs new file mode 100644 index 0000000..af9e660 --- /dev/null +++ b/day01/Main.hs @@ -0,0 +1,11 @@ +module Main where + +import Commons +import Part1 +import Part2 + +main = do doc <- parse + let part1Res = sum (Part1.getCalibrationValues doc) + putStrLn (show (part1Res)) + let part2Res = sum (Part2.getCalibrationValues doc) + putStrLn (show (part2Res)) diff --git a/day01/Part1.hs b/day01/Part1.hs new file mode 100644 index 0000000..d45008b --- /dev/null +++ b/day01/Part1.hs @@ -0,0 +1,14 @@ +module Part1 where + +import Commons + +getCalibrationValuePart :: CalibrationLine -> Char +getCalibrationValuePart (h: _) | h >= '0' && h <= '9' = h +getCalibrationValuePart (_: t) = getCalibrationValuePart t + +getCalibrationValue :: CalibrationLine -> Integer +getCalibrationValue line = read [(getCalibrationValuePart line), (getCalibrationValuePart (reverse line))] + +getCalibrationValues :: CalibrationDocument -> [Integer] +getCalibrationValues (h: []) = [(getCalibrationValue h)] +getCalibrationValues (h: t) = ((getCalibrationValue h): (getCalibrationValues t)) diff --git a/day01/Part2.hs b/day01/Part2.hs new file mode 100644 index 0000000..d03c572 --- /dev/null +++ b/day01/Part2.hs @@ -0,0 +1,36 @@ +module Part2 where + +import Commons + +getCalibrationValueLeft :: CalibrationLine -> Char +getCalibrationValueLeft ('o': 'n': 'e': _) = '1' +getCalibrationValueLeft ('t': 'w': 'o': _) = '2' +getCalibrationValueLeft ('t': 'h': 'r': 'e': 'e': _) = '3' +getCalibrationValueLeft ('f': 'o': 'u': 'r': _) = '4' +getCalibrationValueLeft ('f': 'i': 'v': 'e': _) = '5' +getCalibrationValueLeft ('s': 'i': 'x': _) = '6' +getCalibrationValueLeft ('s': 'e': 'v': 'e': 'n': _) = '7' +getCalibrationValueLeft ('e': 'i': 'g': 'h': 't': _) = '8' +getCalibrationValueLeft ('n': 'i': 'n': 'e': _) = '9' +getCalibrationValueLeft (h: _) | h >= '0' && h <= '9' = h +getCalibrationValueLeft (_: t) = getCalibrationValueLeft t + +getCalibrationValueRight :: CalibrationLine -> Char +getCalibrationValueRight ('e': 'n': 'o': _) = '1' +getCalibrationValueRight ('o': 'w': 't': _) = '2' +getCalibrationValueRight ('e': 'e': 'r': 'h': 't': _) = '3' +getCalibrationValueRight ('r': 'u': 'o': 'f': _) = '4' +getCalibrationValueRight ('e': 'v': 'i': 'f': _) = '5' +getCalibrationValueRight ('x': 'i': 's': _) = '6' +getCalibrationValueRight ('n': 'e': 'v': 'e': 's': _) = '7' +getCalibrationValueRight ('t': 'h': 'g': 'i': 'e': _) = '8' +getCalibrationValueRight ('e': 'n': 'i': 'n': _) = '9' +getCalibrationValueRight (h: _) | h >= '0' && h <= '9' = h +getCalibrationValueRight (_: t) = getCalibrationValueRight t + +getCalibrationValue :: CalibrationLine -> Integer +getCalibrationValue line = read [(getCalibrationValueLeft line), (getCalibrationValueRight (reverse line))] + +getCalibrationValues :: CalibrationDocument -> [Integer] +getCalibrationValues (h: []) = [(getCalibrationValue h)] +getCalibrationValues (h: t) = ((getCalibrationValue h): (getCalibrationValues t))