Compare commits
No commits in common. "c8b7557a0605d8a7c46abfab66293e105d9f8aa1" and "862f7537b45e65423577b49958f54981ec9d9aa6" have entirely different histories.
c8b7557a06
...
862f7537b4
|
|
@ -1,5 +0,0 @@
|
||||||
{
|
|
||||||
"cSpell.words": [
|
|
||||||
"octos"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
module Day11 where
|
|
||||||
import Data.Array
|
|
||||||
( Ix, Array, (!), (//), assocs, bounds, listArray )
|
|
||||||
import Data.Char (digitToInt)
|
|
||||||
import Data.Maybe ( catMaybes, mapMaybe )
|
|
||||||
import Data.List (unfoldr)
|
|
||||||
-- import GHC.Ix (Ix)
|
|
||||||
|
|
||||||
data Point = P Int Int deriving (Eq, Ord, Ix, Show)
|
|
||||||
|
|
||||||
flashOctos:: [Point] -> Array Point Int -> [Point] -> (Array Point Int, [Point])
|
|
||||||
flashOctos [] octos points = (octos, points)
|
|
||||||
flashOctos (p@(P x y):xs) octos points = if p `elem` points || octos ! p <= 9
|
|
||||||
then flashOctos xs octos points
|
|
||||||
else flashOctos (xs ++ filter (\p -> octos ! p >= 9) neighs) (octos // increments) (p:points)
|
|
||||||
where
|
|
||||||
neighs = catMaybes $ (\x' y' -> if (x == x' && y == y') || x' < 1 || x' > mx || y' < 1 || y' > my then Nothing else Just (P x' y')) <$> [x-1..x+1] <*> [y-1..y+1]
|
|
||||||
(P 1 1, P mx my) = bounds octos
|
|
||||||
increments = map(\p -> (p, (octos ! p) + 1)) neighs
|
|
||||||
|
|
||||||
stepOctos :: Array Point Int -> (Array Point Int, Int)
|
|
||||||
stepOctos octos = (field // map (\p -> (p,0)) toReset, length toReset)
|
|
||||||
where
|
|
||||||
(field,toReset) = flashOctos points octos' []
|
|
||||||
points = mapMaybe (\(p,x) -> if x > 9 then Just p else Nothing) $ assocs octos'
|
|
||||||
octos' = fmap (+ 1) octos
|
|
||||||
|
|
||||||
part2 :: Array Point Int -> Int -> Int
|
|
||||||
part2 a i = if c == length a' then i else part2 a' (i+1)
|
|
||||||
where (a', c) = stepOctos a
|
|
||||||
|
|
||||||
main :: IO ()
|
|
||||||
main = do
|
|
||||||
input <- map (map digitToInt) . lines <$> getContents
|
|
||||||
|
|
||||||
putStr "part 1: "
|
|
||||||
let octos = listArray (P 1 1, P (length input) (length (head input))) (concat input)
|
|
||||||
let (_,count) = foldr (\_ (o, count) -> let (o', c') = stepOctos o in (o', count+c')) (octos, 0) [1..100]
|
|
||||||
print count
|
|
||||||
|
|
||||||
putStr "part 2: " >> print (part2 octos 1)
|
|
||||||
|
|
||||||
|
|
||||||
return ()
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
1443582148
|
|
||||||
6553734851
|
|
||||||
1451741246
|
|
||||||
8835218864
|
|
||||||
1662317262
|
|
||||||
1731656623
|
|
||||||
1128178367
|
|
||||||
5842351665
|
|
||||||
6677326843
|
|
||||||
7381433267
|
|
||||||
|
|
@ -1,43 +0,0 @@
|
||||||
module Day12 where
|
|
||||||
import Data.Map ( Map, fromList, unionWith, empty, (!) )
|
|
||||||
import Data.List.Split (splitOn)
|
|
||||||
import qualified Data.Char
|
|
||||||
|
|
||||||
type Path = (String,String)
|
|
||||||
type System = Map String [String]
|
|
||||||
|
|
||||||
readPath :: String -> Path
|
|
||||||
readPath s = (from,to) where [from,to] = splitOn "-" s
|
|
||||||
|
|
||||||
addPath :: Path -> System -> System
|
|
||||||
addPath (from,to) = unionWith (++) (Data.Map.fromList [(from,[to]), (to,[from])])
|
|
||||||
|
|
||||||
isLower :: String -> Bool
|
|
||||||
isLower = Data.Char.isLower . head
|
|
||||||
isUpper :: String -> Bool
|
|
||||||
isUpper = Data.Char.isUpper . head
|
|
||||||
|
|
||||||
pathFinder :: System -> [String] -> [[String]]
|
|
||||||
pathFinder sys p@("end":xs) = [p]
|
|
||||||
pathFinder sys p@(x:xs) = concatMap (\next -> pathFinder sys (next:p)) elig
|
|
||||||
where elig = filter (\cave -> isUpper cave || cave `notElem` p) (sys ! x)
|
|
||||||
|
|
||||||
pathFinder2 :: System -> [String] -> [[String]]
|
|
||||||
pathFinder2 sys p@("end":xs) = [p]
|
|
||||||
pathFinder2 sys p@(x:xs) = concatMap (\next -> f next sys (next:p)) elig
|
|
||||||
where
|
|
||||||
f cave = if isUpper cave || cave `notElem` p then pathFinder2 else pathFinder
|
|
||||||
elig = filter (/= "start") (sys ! x)
|
|
||||||
|
|
||||||
main :: IO ()
|
|
||||||
main = do
|
|
||||||
input <- map readPath . lines <$> getContents
|
|
||||||
let system = foldr addPath Data.Map.empty input
|
|
||||||
|
|
||||||
putStrLn "part 1: "
|
|
||||||
let paths = map reverse $ pathFinder system ["start"]
|
|
||||||
print $ length paths
|
|
||||||
|
|
||||||
putStrLn "part 2: "
|
|
||||||
let paths = map reverse $ pathFinder2 system ["start"]
|
|
||||||
print $ length paths
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
ma-start
|
|
||||||
YZ-rv
|
|
||||||
MP-rv
|
|
||||||
vc-MP
|
|
||||||
QD-kj
|
|
||||||
rv-kj
|
|
||||||
ma-rv
|
|
||||||
YZ-zd
|
|
||||||
UB-rv
|
|
||||||
MP-xe
|
|
||||||
start-MP
|
|
||||||
zd-end
|
|
||||||
ma-UB
|
|
||||||
ma-MP
|
|
||||||
UB-xe
|
|
||||||
end-UB
|
|
||||||
ju-MP
|
|
||||||
ma-xe
|
|
||||||
zd-UB
|
|
||||||
start-xe
|
|
||||||
YZ-end
|
|
||||||
Loading…
Reference in New Issue