aoc2022/3-a.hs

20 lines
458 B
Haskell

import Data.Char
getCharPriority c
| isLower c = fromEnum c - 96
| isUpper c = fromEnum c - 64 + 26
| otherwise = undefined
findDuplicate s = go s 0 0
where
halfFloat = fromIntegral (length s) / 2
half = round halfFloat
go s i1 i2
| i1 >= half = go s 0 (i2+1)
| s!!i1 == s!!(i2 + half) = s!!i1
| otherwise = go s (i1+1) i2
main = interact $
show . sum
. map (getCharPriority . findDuplicate)
. lines