PostsBlogReposAbout
Samuel1 Dec 2024
I just completed "Historian Hysteria" - Day 1 - Advent of Code 2024 #AdventOfCode adventofcode.com/2024/day/1
5 2 120
Samuel1 Dec 2024
haskell spoilers
Adult content
import Data.List

main :: IO ()
main = do
  input <- readFile "input.txt"
  let (left, right) = listPairsToPairList $ map words (lines input)
  print $ sum $ distPairs (sort left, sort right)

listPairsToPairList :: [[String]] -> ([Int], [Int])
listPairsToPairList xs = (map (read . head) xs, map (read . last) xs)

distPairs :: ([Int], [Int]) -> [Int]
distPairs ([], []) = []
distPairs (x:xs, y:ys) = abs (x - y) : distPairs (xs, ys)
main :: IO ()
main = do
  input <- readFile "input.txt"
  let (left, right) = listPairsToPairList $ map words (lines input)
  print $ sumSimScore left right


listPairsToPairList :: [[String]] -> ([Int], [Int])
listPairsToPairList [] = ([], [])
listPairsToPairList xs = (map (read . head) xs, map (read . last) xs)

simScore :: Int -> [Int] -> Int
simScore _ [] = 0
simScore x (y:ys)
  | x == y = x + simScore x ys
  | otherwise = simScore x ys

sumSimScore :: [Int] -> [Int] -> Int
sumSimScore [] _ = 0
sumSimScore (x:xs) ys = simScore x ys + sumSimScore xs ys
5 3 30
Samuel
@samuel.fm
beautiful language :)
9
1 December 2024 at 2:04 pmView on Bluesky