Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1592011

Browse files
committed
Cleaner day15
1 parent 7d659ea commit 1592011

File tree

2 files changed

+27
-45
lines changed

2 files changed

+27
-45
lines changed

day15/one.hs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import Text.Parsec
22
import Parsing
3-
import Generic
4-
import Data.List
5-
import Chart2d
63
import Data.Char
7-
import qualified Data.HashMap.Strict as M
84

95
main :: IO ()
106
main = optimisticInteract readD solve
@@ -19,13 +15,13 @@ readD = readStep `sepBy` char ','
1915

2016
solve input = unlines [
2117
show $ input
22-
, show $ map algo input
18+
, show $ map hash input
2319
, show $ answer
2420
]
2521
where
26-
answer = sum $ map algo input
22+
answer = sum $ map hash input
2723

28-
algo str = hash 0 str
24+
hash str = hash' 0 str
2925
where
30-
hash v (c:cs) = hash ((v + ord c) * 17 `rem` 256) cs
31-
hash v [] = v
26+
hash' v (c:cs) = hash' ((v + ord c) * 17 `rem` 256) cs
27+
hash' v [] = v

day15/two.hs

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,51 @@
11
import Text.Parsec
22
import Parsing
3-
import Generic
4-
import Data.List
5-
import Chart2d
63
import Data.Char
74
import qualified Data.HashMap.Strict as M
85

96
main :: IO ()
107
main = optimisticInteract readD solve
118

12-
readD :: Parser [String]
9+
readD :: Parser [(String, Maybe Int)]
1310
readD = readStep `sepBy` char ','
1411
where
1512
readStep = do
1613
many newline
17-
s <- many1 $ noneOf ",\n"
18-
many newline
19-
return s
14+
s <- many1 alphaNum
15+
c <- choice [readEq, readMin]
16+
return (s, c)
17+
18+
readEq = (Just . fromInteger) <$> (char '=' >> number)
19+
readMin = char '-' >> pure Nothing
2020

2121
solve input = unlines [
2222
show $ input
23-
, show $ M.toList . M.filter (not . null) $ algo input
2423
, show $ answer
2524
]
2625
where
27-
answer = sum . map score $ end
26+
answer = sum . map score . M.toList . M.map scoreContent $ algo input
2827
where
29-
score (box, l) = (1+box) * sum (zipWith (*) [1..] (map snd l))
28+
scoreContent c = sum . zipWith (*) [1..] $ map snd c
29+
score (box, contentScore) = (1+box) * contentScore
3030

31-
end = M.toList . M.filter (not . null) $ algo input
31+
hash str = foldl updateHash 0 str
32+
where
33+
updateHash v c = (v + ord c) * 17 `rem` 256
3234

33-
algo instrs = helper (M.fromList $ zip [0..255] (repeat [])) instrs
35+
algo instrs = helper startState instrs
3436
where
35-
helper m (i:is) = helper m' is
36-
where
37-
(box, op) = getBox 0 i
38-
label = getLabel i
39-
m' = case op of
40-
Just fl -> eqal m label fl box
41-
Nothing -> dash m label box
42-
helper m [] = m
37+
startState = (M.fromList $ zip [0..255] (repeat []))
4338

44-
dash m l box = M.insert box content' m
39+
helper m ((label,op):is) = helper m' is
4540
where
46-
content = m M.! box
47-
content' = filter ((/=l) . fst) content
41+
box = hash label
42+
m' = M.adjust (updateBox label op) box m
43+
helper m [] = m
4844

49-
eqal m l fl box = M.insert box content' m
45+
updateBox l (Just fl) content = insertLens content
5046
where
51-
content = m M.! box
52-
content' = insertLens content
53-
5447
insertLens ((ol, ofl):cs)
55-
| l == ol = (l, fl):cs
48+
| l == ol = (l, fl):cs
5649
| otherwise = (ol, ofl):insertLens cs
5750
insertLens [] = [(l,fl)]
58-
59-
getLabel i = takeWhile (not . (`elem` "=-")) i
60-
61-
getBox :: Int -> String -> (Int, Maybe Int)
62-
getBox v (c:cs)
63-
| c == '=' = (v, Just $ read cs)
64-
| c == '-' = (v, Nothing)
65-
| otherwise = getBox ((v + ord c) * 17 `rem` 256) cs
51+
updateBox l Nothing content = filter ((/=l) . fst) content

0 commit comments

Comments
 (0)