projects
/
advent-of-code-22.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
af51352
)
Tidying
author
Neil Smith
<NeilNjae@users.noreply.github.com>
Mon, 12 Dec 2022 11:37:31 +0000
(13:37 +0200)
committer
Neil Smith
<NeilNjae@users.noreply.github.com>
Mon, 12 Dec 2022 11:37:31 +0000
(13:37 +0200)
advent09/Main.hs
patch
|
blob
|
history
diff --git
a/advent09/Main.hs
b/advent09/Main.hs
index 1e23f78ead14ea5a81a5e708cb1c7dcffc2d4c62..1314035a7f53cf49c01f5614f3cd1e09e448965e 100644
(file)
--- a/
advent09/Main.hs
+++ b/
advent09/Main.hs
@@
-33,12
+33,12
@@
main =
print $ part1 steps
print $ part2 steps
print $ part1 steps
print $ part2 steps
-part1 :: Path -> Int
-part1 steps = S.size $ rope
'
^. trace
- where rope
'
= ropeSteps (newRope 1) steps
+part1
, part2
:: Path -> Int
+part1 steps = S.size $ rope ^. trace
+ where rope = ropeSteps (newRope 1) steps
-part2 steps = S.size $ rope
'
^. trace
- where rope
'
= ropeSteps (newRope 9) steps
+part2 steps = S.size $ rope ^. trace
+ where rope = ropeSteps (newRope 9) steps
expandPath :: [Direction] -> Path
expandPath :: [Direction] -> Path
@@
-48,7
+48,6
@@
expandPath = concatMap expandStep
expandStep (D n) = replicate n (V2 0 -1)
expandStep (R n) = replicate n (V2 1 0)
expandStep (D n) = replicate n (V2 0 -1)
expandStep (R n) = replicate n (V2 1 0)
-
manhattan :: Position -> Position -> Int
manhattan p1 p2 = max dx dy
where V2 dx dy = abs $ p1 ^-^ p2
manhattan :: Position -> Position -> Int
manhattan p1 p2 = max dx dy
where V2 dx dy = abs $ p1 ^-^ p2
@@
-67,21
+66,17
@@
ropeSteps rope steps = foldl' ropeStep rope steps
ropeStep :: Rope -> Position -> Rope
ropeStep rope step = rope & headK .~ h
ropeStep :: Rope -> Position -> Rope
ropeStep rope step = rope & headK .~ h
- & knots .~ (reverse kts
'
)
- & trace %~ S.insert
(head kts')
+ & knots .~ (reverse kts)
+ & trace %~ S.insert
kt
where h = (rope ^. headK) ^+^ step
where h = (rope ^. headK) ^+^ step
- kts = rope ^. knots
- (_, kts') = foldl' knotStep (h, []) kts
-
-
--- foldl' (f) (hr, []) knots
+ (kt, kts) = foldl' knotStep (h, []) $ rope ^. knots -- kts
+knotStep :: (Position, [Position]) -> Position -> (Position, [Position])
knotStep (h, ks) kt = (kt', (kt':ks))
where kt' = if kt `touching` h
then kt
else kt ^+^ (kt `towards` h)
knotStep (h, ks) kt = (kt', (kt':ks))
where kt' = if kt `touching` h
then kt
else kt ^+^ (kt `towards` h)
-
-- Parse the input file
pathP :: Parser [Direction]
-- Parse the input file
pathP :: Parser [Direction]