Skip to content

Commit

Permalink
day 15
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjercan committed Dec 15, 2023
1 parent a502540 commit d87ad43
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 3 deletions.
3 changes: 3 additions & 0 deletions aoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,6 @@ stack exec day13 < input/day13.input

echo -e "${IYellow}--- Day 14: Parabolic Reflector Dish ---${Color_Off}"
stack exec day14 < input/day14.input

echo -e "${IGreen}--- Day 15: Lens Library ---${Color_Off}"
stack exec day15 < input/day15.input
29 changes: 29 additions & 0 deletions aoc2023.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ executable day01
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day02
Expand All @@ -41,6 +42,7 @@ executable day02
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day03
Expand All @@ -53,6 +55,7 @@ executable day03
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day04
Expand All @@ -67,6 +70,7 @@ executable day04
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day05
Expand All @@ -79,6 +83,7 @@ executable day05
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day06
Expand All @@ -91,6 +96,7 @@ executable day06
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day07
Expand All @@ -103,6 +109,7 @@ executable day07
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day08
Expand All @@ -117,6 +124,7 @@ executable day08
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day09
Expand All @@ -129,6 +137,7 @@ executable day09
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day10
Expand All @@ -141,6 +150,7 @@ executable day10
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day11
Expand All @@ -153,6 +163,7 @@ executable day11
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day12
Expand All @@ -165,6 +176,7 @@ executable day12
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day13
Expand All @@ -177,6 +189,7 @@ executable day13
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day14
Expand All @@ -189,10 +202,13 @@ executable day14
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day15
main-is: Day15.hs
other-modules:
Util.Parser
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -main-is Day15
Expand All @@ -201,6 +217,7 @@ executable day15
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day16
Expand All @@ -213,6 +230,7 @@ executable day16
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day17
Expand All @@ -225,6 +243,7 @@ executable day17
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day18
Expand All @@ -237,6 +256,7 @@ executable day18
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day19
Expand All @@ -249,6 +269,7 @@ executable day19
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day20
Expand All @@ -261,6 +282,7 @@ executable day20
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day21
Expand All @@ -273,6 +295,7 @@ executable day21
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day22
Expand All @@ -285,6 +308,7 @@ executable day22
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day23
Expand All @@ -297,6 +321,7 @@ executable day23
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day24
Expand All @@ -309,6 +334,7 @@ executable day24
, containers
, parsec
, split
, vector
default-language: Haskell2010

executable day25
Expand All @@ -321,6 +347,7 @@ executable day25
, containers
, parsec
, split
, vector
default-language: Haskell2010

test-suite spec
Expand Down Expand Up @@ -367,6 +394,7 @@ test-suite spec
TestDay12
TestDay13
TestDay14
TestDay15
Paths_aoc2023
autogen-modules:
Paths_aoc2023
Expand All @@ -380,4 +408,5 @@ test-suite spec
, hspec
, parsec
, split
, vector
default-language: Haskell2010
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ dependencies:
- split
- parsec
- containers
- vector

ghc-options:
- -Wall
Expand Down Expand Up @@ -102,7 +103,7 @@ executables:
day15:
source-dirs: src
main: Day15
other-modules: []
other-modules: [Util.Parser]

day16:
source-dirs: src
Expand Down
55 changes: 53 additions & 2 deletions src/Day15.hs
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
module Day15 (main, part1, part2) where

import Data.Char (ord)
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as MV
import qualified Text.Parsec as P
import Util.Parser (Parser, parse)

data Operation = Insert String Int | Delete String deriving (Show)

csvP :: Parser [String]
csvP = P.sepBy (P.many1 $ P.noneOf ",\n") (P.char ',') <* P.spaces <* P.eof

insertP :: String -> Parser Operation
insertP s = do
_ <- P.char '='
n <- P.many1 P.digit
return $ Insert s (read n)

deleteP :: String -> Parser Operation
deleteP s = do
_ <- P.char '-'
return $ Delete s

operationP :: Parser Operation
operationP = do
s <- P.many1 P.letter
insertP s P.<|> deleteP s

operationsP :: Parser [Operation]
operationsP = P.sepBy operationP (P.char ',') <* P.spaces <* P.eof

hash :: String -> Int
hash = foldl (\acc c -> (acc + c) * 17 `mod` 256) 0 . map ord

part1 :: String -> String
part1 = id
part1 = show . sum . map hash . parse csvP

insertPair :: (String, Int) -> [(String, Int)] -> [(String, Int)]
insertPair (s, n) [] = [(s, n)]
insertPair (s, n) ((s', n') : xs)
| s == s' = (s, n) : xs
| otherwise = (s', n') : insertPair (s, n) xs

operation :: V.Vector [(String, Int)] -> Operation -> V.Vector [(String, Int)]
operation v (Insert s n) = V.modify (\v' -> MV.modify v' (insertPair (s, n)) (hash s)) v
operation v (Delete s) = V.modify (\v' -> MV.modify v' (filter (\(s', _) -> s /= s')) (hash s)) v

simulate :: [Operation] -> V.Vector [(String, Int)]
simulate = foldl operation (V.replicate 256 [])

power :: V.Vector [(String, Int)] -> Int
power = V.sum . V.imap (\i -> sum . zipWith (\j (_, x) -> (i + 1) * j * x) [1 ..])

input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"

part2 :: String -> String
part2 = const ""
part2 = show . power . simulate . parse operationsP

solve :: String -> String
solve input = "Part 1: " ++ part1 input ++ "\nPart 2: " ++ part2 input ++ "\n"
Expand Down
2 changes: 2 additions & 0 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import TestDay11
import TestDay12
import TestDay13
import TestDay14
import TestDay15

main :: IO ()
main = do
Expand All @@ -31,3 +32,4 @@ main = do
TestDay12.test
TestDay13.test
TestDay14.test
TestDay15.test
22 changes: 22 additions & 0 deletions test/TestDay15.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module TestDay15 (test) where

import Day15
import Test.Hspec

input :: String
input = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"

test1 :: Expectation
test1 = part1 input `shouldBe` "1320"

test2 :: Expectation
test2 = part2 input `shouldBe` "145"

test :: IO ()
test = hspec $ do
describe "day15" $ do
describe "part1" $ do
it "should work for the examples" test1

describe "part2" $ do
it "should work for the examples" test2

0 comments on commit d87ad43

Please sign in to comment.