Day 10B solution

This commit is contained in:
Natty 2023-12-12 00:08:49 +01:00
parent a2ed67872a
commit 26cec0dba3
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
2 changed files with 44 additions and 20 deletions

View File

@ -9,12 +9,15 @@ import Data.Bifunctor (first, second)
import Data.Bits (Bits (..))
import Data.Foldable (find)
import Data.Map qualified as M
import Data.Maybe (catMaybes, fromJust, fromMaybe, isJust)
import Data.Set qualified as S
import Data.Maybe (catMaybes, fromJust, fromMaybe)
import GHC.OldList (intercalate)
parsePipe :: Char -> Integer
parsePipe = fromMaybe 0 . (M.fromList (zip "|-JLF7S" [0b1100, 0b0011, 0b1010, 0b1001, 0b0101, 0b0110, 0b1111]) M.!?)
toPipe :: Integer -> Char
toPipe = fromMaybe '.' . (M.fromList (zip [0b1100, 0b0011, 0b1010, 0b1001, 0b0101, 0b0110, 0b1111] "┃━┛┗┏┓╋") M.!?)
flipDirs :: Integer -> Integer
flipDirs =
sum
@ -38,14 +41,35 @@ main = do
$ c
let start = fst . fromJust $ find ((== 0b1111) . snd) (M.assocs pipes)
let path = (start :) . takeWhile (/= start) . (fst <$>) . tail $ iterate (head . filter ((/= 0) . snd) . findAdj) (start, 0b1111)
let pipeStart = sum
$ flip (zipWith ((*) . toInteger . fromEnum)) [0b0010, 0b0001, 0b1000, 0b0100]
$ flip elem (fst <$> findAdj (start, 0b1111))
<$> (($ start) <$> ([first, second] <*> [(-1 +), (1 +)]))
let go v (((> 0) . (.&. 0b1100) -> True) : xs) = go (not v) xs
go v (_ : xs) = fromEnum v + go v xs
go _ [] = 0
let path =
((start, 0b1111) :)
. takeWhile ((/= start) . fst)
. tail
$ iterate (head . filter ((/= 0) . snd) . findAdj) (start, pipeStart)
let pathPipes = S.fromList path
let piping = [[fromMaybe 0 $ S.lookupIndex (x, y) pathPipes *> (pipes M.!? (x, y)) | x <- [0 .. maximum $ length <$> lns]] | y <- [0 .. length lns]]
let countSpaces = go 0 False
where
go _ v (0b1100 : xs) = go 0 (not v) xs
go p v (0b0011 : xs) = go p v xs
go _ v (0 : xs) = fromEnum v + go 0 v xs
go p v (x : xs)
| p == 0 = go x v xs
| p .&. x .&. 0b1100 > 0 = go 0 v xs
| otherwise = go 0 (not v) xs
go _ _ [] = 0
print $ piping
print $ sum $ go False <$> piping
let pathPipes = M.fromList path
let piping = [[
(\c -> if c == 0b1111 then pipeStart else c)
$ fromMaybe 0
$ pathPipes M.!? (x, y)
*> pipes M.!? (x, y)
| x <- [0 .. maximum (length <$> lns) - 1]] | y <- [0 .. length lns - 1]]
putStrLn $ intercalate "\n" $ flip (zipWith (++)) (show . countSpaces <$> piping) $ fmap toPipe <$> piping
print $ sum $ countSpaces <$> piping

20
day10b/test.txt generated
View File

@ -1,10 +1,10 @@
.F----7F7F7F7F-7....
.|F--7||||||||FJ....
.||.FJ||||||||L7....
FJL7L7LJLJ||LJ.L-7..
L--J.L7...LJS7F-7L7.
....F-J..F7FJ|L7L7L7
....L7.F7||L7|.L7L7|
.....|FJLJ|FJ|F7|.LJ
....FJL-7.||.||||...
....L---J.LJ.LJLJ...
FF7FSF7F7F7F7F7F---7
L|LJ||||||||||||F--J
FL-7LJLJ||||||LJL-77
F--JF--7||LJLJ7F7FJ-
L---JF-JLJ.||-FJLJJ7
|F|F-JF---7F7-L7L|7|
|FFJF7L7F-JF7|JL---7
7-L-JL7||F7|L7F-7F7|
L.L7LFJ|||||FJL7||LJ
L7JLJL-JLJLJL--JLJ.L