Skip to content

Instantly share code, notes, and snippets.

@evinism
Created December 3, 2022 22:22
Show Gist options
  • Save evinism/6868d8fcdd3c47f441a3f2c21b3488f8 to your computer and use it in GitHub Desktop.
Save evinism/6868d8fcdd3c47f441a3f2c21b3488f8 to your computer and use it in GitHub Desktop.
import qualified Data.Set as Set
import Data.Char
main = do
input <- readFile "input.txt"
print $ fn input
fn :: String -> Int
fn = sum . (fmap procline) . lines
procline :: String -> Int
procline line = sum $ fmap scorechar $ Set.toList intersection
where
left = Set.fromList $ take ((length line) `div` 2) line
right = Set.fromList $ drop ((length line) `div` 2) line
intersection = Set.intersection left right
scorechar :: Char -> Int
scorechar c =
if c >= 'a'
then
(ord c) - (ord 'a') + 1
else
(ord c) - (ord 'A') + 27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment