Improvements for day 1 and day 2

This commit is contained in:
2023-12-02 18:12:26 +01:00
parent eedd910d67
commit 357c6dd0c3
4 changed files with 37 additions and 37 deletions

View File

@@ -1,32 +1,33 @@
module Part2 where
import Commons
import Data.Char
import Data.Char (isDigit)
import Data.List (isPrefixOf)
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 line | "one" `isPrefixOf` line = '1'
| "two" `isPrefixOf` line = '2'
| "three" `isPrefixOf` line = '3'
| "four" `isPrefixOf` line = '4'
| "five" `isPrefixOf` line = '5'
| "six" `isPrefixOf` line = '6'
| "seven" `isPrefixOf` line = '7'
| "eight" `isPrefixOf` line = '8'
| "nine" `isPrefixOf` line = '9'
getCalibrationValueLeft (h: _) | isDigit h = 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 line | "eno" `isPrefixOf` line = '1'
| "owt" `isPrefixOf` line = '2'
| "eerht" `isPrefixOf` line = '3'
| "ruof" `isPrefixOf` line = '4'
| "evif" `isPrefixOf` line = '5'
| "xis" `isPrefixOf` line = '6'
| "neves" `isPrefixOf` line = '7'
| "thgie" `isPrefixOf` line = '8'
| "enin" `isPrefixOf` line = '9'
getCalibrationValueRight (h: _) | isDigit h = h
getCalibrationValueRight (_: t) = getCalibrationValueRight t