projects
/
advent-of-code-23.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
dd72089
)
More tinkering
author
Neil Smith
<NeilNjae@users.noreply.github.com>
Mon, 22 Jan 2024 21:52:53 +0000
(21:52 +0000)
committer
Neil Smith
<NeilNjae@users.noreply.github.com>
Mon, 22 Jan 2024 21:52:53 +0000
(21:52 +0000)
advent14/Main.hs
patch
|
blob
|
history
diff --git
a/advent14/Main.hs
b/advent14/Main.hs
index e7e8d5b53173018faff8bde55d43e6a625d30c2a..9a8627c6acbc8187447dc72c2882e6490bca503b 100644
(file)
--- a/
advent14/Main.hs
+++ b/
advent14/Main.hs
@@
-24,16
+24,7
@@
main =
do dataFileName <- getDataFileName
text <- readFile dataFileName
let (rGrid, cGrid) = readGrids text
do dataFileName <- getDataFileName
text <- readFile dataFileName
let (rGrid, cGrid) = readGrids text
- -- putStrLn $ showGrid rGrid cGrid
- -- let rGrid' = rollNorth rGrid cGrid
- -- putStrLn $ showGrid rGrid' cGrid
print $ part1 rGrid cGrid
print $ part1 rGrid cGrid
- -- let rGrid1 = rollCycle rGrid cGrid
- -- putStrLn $ showGrid rGrid1 cGrid
- -- let rGrid2 = rollCycle rGrid1 cGrid
- -- putStrLn $ showGrid rGrid2 cGrid
- -- let rGrid3 = rollCycle rGrid2 cGrid
- -- putStrLn $ showGrid rGrid3 cGrid
print $ part2 rGrid cGrid
print $ part2 rGrid cGrid
@@
-53,26
+44,26
@@
findRepeat rGrid cGrid = head $ dropWhile test $ iterate go (rGrid, M.empty, 0)
go (g, c, i) = (rollCycle g cGrid, M.insert g i c, (i + 1))
rollNorth, rollCycle :: Grid -> Grid -> Grid
go (g, c, i) = (rollCycle g cGrid, M.insert g i c, (i + 1))
rollNorth, rollCycle :: Grid -> Grid -> Grid
-rollNorth rGrid cGrid = roll
(V2 0 0) (V2 0 1) (V2 1 0)
cGrid rGrid
+rollNorth rGrid cGrid = roll
[(V2 0 0, V2 0 1, V2 1 0)]
cGrid rGrid
-rollCycle rGrid cGrid = foldl' go rGrid [ (V2 0 0, V2 0 1, V2 1 0)
- , (V2 0 0, V2 1 0, V2 0 1)
- , (V2 r 0, V2 0 1, V2 -1 0)
- , (V2 0 c, V2 1 0, V2 0 -1)
- ]
+rollCycle rGrid cGrid = roll [ (V2 0 0, V2 0 1, V2 1 0)
+ , (V2 0 0, V2 1 0, V2 0 1)
+ , (V2 r 0, V2 0 1, V2 -1 0)
+ , (V2 0 c, V2 1 0, V2 0 -1)
+ ]
+ cGrid rGrid
where (_, V2 r c) = U.bounds rGrid
where (_, V2 r c) = U.bounds rGrid
- go g (start, majorStep, minorStep) =
- roll start majorStep minorStep cGrid g
-roll ::
Position -> Position -> Position
-> Grid -> Grid -> Grid
-roll
start majorStep minorStep
cGrid rGrid =
+roll ::
[(Position, Position, Position)]
-> Grid -> Grid -> Grid
+roll
moveSpecs
cGrid rGrid =
A.runSTUArray $
do grid <- A.thaw rGrid
holes <- newSTRef Q.Empty
A.runSTUArray $
do grid <- A.thaw rGrid
holes <- newSTRef Q.Empty
- forM_ (takeWhile (inBounds rGrid) $ iterate (^+^ majorStep) start) $ \maj ->
- do writeSTRef holes Q.Empty
- forM_ (takeWhile (inBounds rGrid) $ iterate (^+^ minorStep) maj) $ \here ->
- rollPosition grid cGrid holes here
+ forM_ moveSpecs $ \(start, majorStep, minorStep) ->
+ forM_ (takeWhile (inBounds rGrid) $ iterate (^+^ majorStep) start) $ \maj ->
+ do writeSTRef holes Q.Empty
+ forM_ (takeWhile (inBounds rGrid) $ iterate (^+^ minorStep) maj) $ \here ->
+ rollPosition grid cGrid holes here
return grid
rollPosition :: (MGrid s) -> Grid -> (Gaps s) -> Position -> ST s ()
return grid
rollPosition :: (MGrid s) -> Grid -> (Gaps s) -> Position -> ST s ()
@@
-92,9
+83,8
@@
inBounds :: Grid -> Position -> Bool
inBounds g h = inRange (U.bounds g) h
getLoad :: Grid -> Int
inBounds g h = inRange (U.bounds g) h
getLoad :: Grid -> Int
-getLoad grid = sum
columnLoads
+getLoad grid = sum
[getColLoad c | c <- [0..cMax]]
where (_, V2 rMax cMax) = U.bounds grid
where (_, V2 rMax cMax) = U.bounds grid
- columnLoads = [getColLoad c | c <- [0..cMax]]
getColLoad c = sum [(rMax - r + 1) | r <- [0..rMax], grid U.! (V2 r c)]
readGrids :: String -> (Grid, Grid)
getColLoad c = sum [(rMax - r + 1) | r <- [0..rMax], grid U.! (V2 r c)]
readGrids :: String -> (Grid, Grid)
@@
-105,13
+95,8
@@
readGrids text = (rGrid, cGrid)
rGrid = U.listArray ((V2 0 0), (V2 r c)) $ fmap (== 'O') $ concat rows
cGrid = U.listArray ((V2 0 0), (V2 r c)) $ fmap (== '#') $ concat rows
rGrid = U.listArray ((V2 0 0), (V2 r c)) $ fmap (== 'O') $ concat rows
cGrid = U.listArray ((V2 0 0), (V2 r c)) $ fmap (== '#') $ concat rows
--- readElem :: Char -> Element
--- readElem '.' = Empty
--- readElem '#' = Cube
--- readElem 'O' = Round
-
-showGrid :: Grid -> Grid -> String
-showGrid rGrid cGrid = unlines rows
+showGrids :: Grid -> Grid -> String
+showGrids rGrid cGrid = unlines rows
where (_, V2 rMax cMax) = U.bounds rGrid
rows = [showRow r | r <- [0..rMax]]
showRow r = [showElem r c | c <- [0..cMax]]
where (_, V2 rMax cMax) = U.bounds rGrid
rows = [showRow r | r <- [0..rMax]]
showRow r = [showElem r c | c <- [0..cMax]]