Skip to content

Commit

Permalink
Day_15(2023): solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheinxy committed Dec 15, 2023
1 parent 221082a commit 3c4ebe5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 1 deletion.
46 changes: 46 additions & 0 deletions 2023/Day_15/Day_15.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Main where

import Data.Char
import Data.List.Split
import Data.Map (Map, (!), adjust, fromList, toList)
import System.Environment

data Lens = Lens { label :: String, focal :: Int } deriving (Show)
type Boxes = Map Int [Lens]

type Input = [String]
type Output = Int

parseInput :: String -> Input
parseInput = splitOn "," . head . lines

getHash :: String -> Int
getHash = foldl (\acc x -> (acc + ord x) * 17 `rem` 256) 0

putInBoxes :: Boxes -> String -> Boxes
putInBoxes boxes = go . span (`notElem` "=-")
where go (lab, "-" ) = adjust (filter ((/= lab) . label)) (getHash lab) boxes
go (lab, '=' : n) | lab `elem` labels = adjust (const $ before ++ [Lens lab (read n)] ++ after) hash boxes
| otherwise = adjust (Lens lab (read n) :) hash boxes
where hash = getHash lab
elements = boxes ! getHash lab
labels = map label elements
(before, _:after) = span ((/= lab) . label) elements

partOne :: Input -> Output
partOne = sum . map getHash

partTwo :: Input -> Output
partTwo = sum . map getPower . toList . foldl putInBoxes (fromList [(i, []) | i <- [0 .. 255]])
where getPower (i, xs) = sum [(i + 1) * j * focal lens | (j, lens) <- zip [1 .. ] $ reverse xs]

compute :: Input -> String -> IO ()
compute input "parse" = print input
compute input "one" = print . partOne $ input
compute input "two" = print . partTwo $ input
compute input _ = error "Unknown part"

main = do
args <- getArgs
input <- parseInput <$> readFile (last args)
mapM (compute input) $ init args
1 change: 1 addition & 0 deletions 2023/Day_15/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Day 15
Loading

0 comments on commit 3c4ebe5

Please sign in to comment.