From e2d83ba1d644aeda3bb558d8132bf7141e3f0b8d Mon Sep 17 00:00:00 2001 From: Alexander Kobjolke Date: Sun, 3 Dec 2023 20:31:06 +0100 Subject: [PATCH] day3: Solve part1 --- aoc.cabal | 8 +++++-- data/Y2023/D03/riddle | 10 ++++++++ package.yaml | 1 + src/AoC.hs | 2 +- src/AoC/Y2023/D03.hs | 56 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/aoc.cabal b/aoc.cabal index 24b74f2..73b6095 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -63,7 +63,8 @@ library DerivingStrategies ghc-options: -Weverything -Wno-implicit-prelude -Wno-missing-export-lists -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missed-specialisations -Wno-all-missed-specialisations -Wno-unsafe -Wno-safe -Wno-missing-safe-haskell-mode -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-prepositive-qualified-module -fdefer-typed-holes -Wno-unused-packages build-depends: - base + array + , base , containers , megaparsec , relude @@ -89,6 +90,7 @@ executable aoc ghc-options: -Weverything -Wno-implicit-prelude -Wno-missing-export-lists -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missed-specialisations -Wno-all-missed-specialisations -Wno-unsafe -Wno-safe -Wno-missing-safe-haskell-mode -Wno-missing-local-signatures -Wno-monomorphism-restriction -Wno-prepositive-qualified-module -fdefer-typed-holes -Wno-unused-packages build-depends: aoc + , array , base , containers , megaparsec @@ -117,7 +119,8 @@ test-suite doctest build-tool-depends: doctest:doctest build-depends: - base + array + , base , containers , megaparsec , process @@ -152,6 +155,7 @@ test-suite spec build-depends: QuickCheck , aoc + , array , base , containers , hspec diff --git a/data/Y2023/D03/riddle b/data/Y2023/D03/riddle index e69de29..b20187f 100644 --- a/data/Y2023/D03/riddle +++ b/data/Y2023/D03/riddle @@ -0,0 +1,10 @@ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. diff --git a/package.yaml b/package.yaml index dfbee0d..673461c 100644 --- a/package.yaml +++ b/package.yaml @@ -19,6 +19,7 @@ dependencies: mixin: - (Relude as Prelude, Relude, Relude.Unsafe, Relude.Extra.Enum) - containers + - array - text - megaparsec diff --git a/src/AoC.hs b/src/AoC.hs index 3431b23..169afa1 100644 --- a/src/AoC.hs +++ b/src/AoC.hs @@ -29,4 +29,4 @@ handleError e = do hPutStrLn stderr $ "I ran into an issue: " <> show e defaultMain :: IO () -defaultMain = Exception.catch (runAoC Y2023 D01 >>= print) handleError +defaultMain = Exception.catch (runAoC Y2023 D03 >>= print) handleError diff --git a/src/AoC/Y2023/D03.hs b/src/AoC/Y2023/D03.hs index c509d93..80e00ee 100644 --- a/src/AoC/Y2023/D03.hs +++ b/src/AoC/Y2023/D03.hs @@ -1,6 +1,58 @@ -module AoC.Y2023.D03 (solve) where +module AoC.Y2023.D03 where import AoC.Riddle +import Data.Array +import Data.Char (isDigit) +import System.Posix.Internals (const_fd_cloexec) + +type Pos = (Int, Int) +type Puzzle = Array Pos Char + +toArray :: [[Char]] -> Puzzle +toArray xxs = + array + ((1, 1), (h, w)) + [ ((y, x), c) + | (y, xs) <- zip [1 ..] xxs + , (x, c) <- zip [1 ..] xs + ] + where + w = case xxs of + [] -> 0 + xs : _ -> length xs + h = length xxs + +parse :: Text -> Puzzle +parse = toArray . fmap toString . lines + +symbols :: Puzzle -> [(Pos, Char)] +symbols = filter (isSymbol <$> snd) . assocs + where + isSymbol :: Char -> Bool + isSymbol c = not (isDigit c || c == '.') + +neighborDeltas :: [Pos] +neighborDeltas = + [ (-1, -1) + , (-1, 0) + , (-1, 1) + , (0, -1) + , (0, 1) + , (1, -1) + , (1, 0) + , (1, 1) + ] + +neighbors :: Puzzle -> Pos -> [(Pos, Char)] +neighbors = undefined + +part1 :: Puzzle -> Integer +part1 = const 42 solve :: (MonadIO m) => Text -> m (Either Text Solution) -solve _ = pure $ Left "not yet implemented" +solve input = do + let + puzzle = parse input + p1 = part1 puzzle + print $ symbols puzzle + pure $ Right [p1]