Day 01
This commit is contained in:
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
*
|
||||||
|
|
||||||
|
!.gitignore
|
||||||
|
|
||||||
|
!**/
|
||||||
|
!*.hs
|
||||||
20
day01/Commons.hs
Normal file
20
day01/Commons.hs
Normal file
@@ -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)
|
||||||
11
day01/Main.hs
Normal file
11
day01/Main.hs
Normal file
@@ -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))
|
||||||
14
day01/Part1.hs
Normal file
14
day01/Part1.hs
Normal file
@@ -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))
|
||||||
36
day01/Part2.hs
Normal file
36
day01/Part2.hs
Normal file
@@ -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))
|
||||||
Reference in New Issue
Block a user