This commit is contained in:
2023-12-01 17:25:07 +01:00
commit 787691d640
5 changed files with 87 additions and 0 deletions

36
day01/Part2.hs Normal file
View 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))