Add Day 7, Puzzle 1 solution
This commit is contained in:
@@ -19,6 +19,7 @@ executable adventofcode2025
|
|||||||
main-is: Main.hs
|
main-is: Main.hs
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
build-depends: base >= 4.7 && < 5
|
build-depends: base >= 4.7 && < 5
|
||||||
|
, mtl
|
||||||
, split
|
, split
|
||||||
ghc-options: -Wall
|
ghc-options: -Wall
|
||||||
-Wcompat
|
-Wcompat
|
||||||
@@ -36,3 +37,4 @@ executable adventofcode2025
|
|||||||
Day04
|
Day04
|
||||||
Day05
|
Day05
|
||||||
Day06
|
Day06
|
||||||
|
Day07
|
||||||
|
|||||||
66
src/Day07.hs
Normal file
66
src/Day07.hs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
{-# OPTIONS_GHC -Wno-x-partial #-}
|
||||||
|
|
||||||
|
module Day07
|
||||||
|
( day07_1,
|
||||||
|
)
|
||||||
|
where
|
||||||
|
|
||||||
|
import Control.Monad.State
|
||||||
|
import Data.List (elemIndex)
|
||||||
|
import Data.Maybe (fromJust, isJust)
|
||||||
|
|
||||||
|
type Coord = (Int, Int)
|
||||||
|
|
||||||
|
type Grid = [String]
|
||||||
|
|
||||||
|
parseInput :: IO [String]
|
||||||
|
parseInput = do
|
||||||
|
lines <$> readFile "input/day7.txt"
|
||||||
|
|
||||||
|
getStart :: Grid -> Int -> Coord
|
||||||
|
getStart [] _ = (-1, -1)
|
||||||
|
getStart (g : grid) row
|
||||||
|
| isJust start = (row, fromJust start)
|
||||||
|
| otherwise = getStart grid (row + 1)
|
||||||
|
where
|
||||||
|
start = elemIndex 'S' g
|
||||||
|
|
||||||
|
isInside :: Coord -> Grid -> Bool
|
||||||
|
isInside coord grid =
|
||||||
|
let w = length $ head grid
|
||||||
|
h = length grid
|
||||||
|
in fst coord >= 0 && fst coord < h && snd coord >= 0 && snd coord < w
|
||||||
|
|
||||||
|
mark :: Coord -> State Grid ()
|
||||||
|
mark coord = state $ \grid ->
|
||||||
|
( (),
|
||||||
|
take (fst coord) grid ++ [take (snd coord) (grid !! fst coord) ++ ['x'] ++ drop (snd coord + 1) (grid !! fst coord)] ++ drop (fst coord + 1) grid
|
||||||
|
)
|
||||||
|
|
||||||
|
beamSplitRecursive :: Coord -> State Grid ()
|
||||||
|
beamSplitRecursive start = do
|
||||||
|
grid <- get
|
||||||
|
if not $ isInside start grid
|
||||||
|
then return ()
|
||||||
|
else
|
||||||
|
if grid !! fst start !! snd start == 'S' || grid !! fst start !! snd start == '.'
|
||||||
|
then beamSplitRecursive (fst start + 1, snd start)
|
||||||
|
else if grid !! fst start !! snd start == 'x' then return ()
|
||||||
|
else do
|
||||||
|
mark start
|
||||||
|
beamSplitRecursive (fst start, snd start - 1)
|
||||||
|
beamSplitRecursive (fst start, snd start + 1)
|
||||||
|
|
||||||
|
beamSplit :: State Grid ()
|
||||||
|
beamSplit = do
|
||||||
|
grid <- get
|
||||||
|
let start = getStart grid 0
|
||||||
|
beamSplitRecursive start
|
||||||
|
|
||||||
|
day07_1 :: IO ()
|
||||||
|
day07_1 = do
|
||||||
|
input <- parseInput
|
||||||
|
let grid = execState beamSplit input
|
||||||
|
putStrLn $
|
||||||
|
"Day 7, Puzzle 1 solution: "
|
||||||
|
++ show (length $ concatMap (filter (== 'x')) grid)
|
||||||
@@ -6,6 +6,7 @@ import Day03 (day03_1, day03_2)
|
|||||||
import Day04 (day04_1, day04_2)
|
import Day04 (day04_1, day04_2)
|
||||||
import Day05 (day05_1, day05_2)
|
import Day05 (day05_1, day05_2)
|
||||||
import Day06 (day06_1, day06_2)
|
import Day06 (day06_1, day06_2)
|
||||||
|
import Day07 (day07_1)
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -42,6 +43,9 @@ main = do
|
|||||||
"6" : _ -> do
|
"6" : _ -> do
|
||||||
day06_1
|
day06_1
|
||||||
day06_2
|
day06_2
|
||||||
|
"7" : "1" : _ -> day07_1
|
||||||
|
"7" : _ -> do
|
||||||
|
day07_1
|
||||||
"all" : _ -> do
|
"all" : _ -> do
|
||||||
day01_1
|
day01_1
|
||||||
day01_2
|
day01_2
|
||||||
@@ -55,4 +59,5 @@ main = do
|
|||||||
day05_2
|
day05_2
|
||||||
day06_1
|
day06_1
|
||||||
day06_2
|
day06_2
|
||||||
|
day07_1
|
||||||
_ -> error "Not implemented"
|
_ -> error "Not implemented"
|
||||||
|
|||||||
Reference in New Issue
Block a user