Add Day 4, Puzzle 2 solution
This commit is contained in:
@@ -5,6 +5,6 @@
|
|||||||
|01 |★ ★ |07 | |
|
|01 |★ ★ |07 | |
|
||||||
|02 |★ ★ |08 | |
|
|02 |★ ★ |08 | |
|
||||||
|03 |★ ★ |09 | |
|
|03 |★ ★ |09 | |
|
||||||
|04 |★ |10 | |
|
|04 |★ ★ |10 | |
|
||||||
|05 | |11 | |
|
|05 | |11 | |
|
||||||
|06 | |12 | |
|
|06 | |12 | |
|
||||||
|
|||||||
50
src/Day04.hs
50
src/Day04.hs
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
module Day04
|
module Day04
|
||||||
( day04_1,
|
( day04_1,
|
||||||
|
day04_2,
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
@@ -20,28 +21,49 @@ move position U = (fst position, snd position - 1)
|
|||||||
move position RU = (fst position + 1, snd position - 1)
|
move position RU = (fst position + 1, snd position - 1)
|
||||||
move position L = (fst position - 1, snd position)
|
move position L = (fst position - 1, snd position)
|
||||||
move position R = (fst position + 1, snd position)
|
move position R = (fst position + 1, snd position)
|
||||||
move position LD = (fst position -1, snd position + 1)
|
move position LD = (fst position - 1, snd position + 1)
|
||||||
move position D = (fst position, snd position + 1)
|
move position D = (fst position, snd position + 1)
|
||||||
move position RD = (fst position + 1, snd position + 1)
|
move position RD = (fst position + 1, snd position + 1)
|
||||||
|
|
||||||
getValue :: Grid -> Position -> Char
|
getValue :: Grid -> Position -> Char
|
||||||
getValue grid position
|
getValue grid position
|
||||||
| not $ isInside position (length (head grid)) (length grid) = '.'
|
| not $ isInside position (length (head grid)) (length grid) = '.'
|
||||||
| otherwise = (grid !! snd position) !! fst position
|
| otherwise = (grid !! snd position) !! fst position
|
||||||
|
|
||||||
getAdjacent :: Grid -> Position -> Int
|
getAdjacent :: Grid -> Position -> Int
|
||||||
getAdjacent grid position = length . filter (== '@') $ map (getValue grid . move position) [LU .. RD]
|
getAdjacent grid position = length . filter (== '@') $ map (getValue grid . move position) [LU .. RD]
|
||||||
|
|
||||||
isAccessible :: Grid -> Position -> Bool
|
isAccessible :: Grid -> Position -> Bool
|
||||||
isAccessible grid position
|
isAccessible grid position
|
||||||
| getValue grid position == '.' = False
|
| getValue grid position == '.' = False
|
||||||
| otherwise = getAdjacent grid position < 4
|
| otherwise = getAdjacent grid position < 4
|
||||||
|
|
||||||
countAccessible :: Grid -> Int
|
countAccessible :: Grid -> Int
|
||||||
countAccessible grid = let w = length $ head grid
|
countAccessible grid =
|
||||||
h = length grid
|
let w = length $ head grid
|
||||||
positions = [ (x, y) | x <- [0..w - 1], y <- [0..h - 1]]
|
h = length grid
|
||||||
in length . filter id $ map (isAccessible grid) positions
|
positions = [(x, y) | x <- [0 .. w - 1], y <- [0 .. h - 1]]
|
||||||
|
in length . filter id $ map (isAccessible grid) positions
|
||||||
|
|
||||||
|
removePaper :: Grid -> Position -> Grid
|
||||||
|
removePaper grid position = take (snd position) grid ++ newRow ++ drop (snd position + 1) grid
|
||||||
|
where
|
||||||
|
newRow = [take (fst position) (grid !! snd position) ++ ['.'] ++ drop (fst position + 1) (grid !! snd position)]
|
||||||
|
|
||||||
|
removeAccessible :: Grid -> Grid
|
||||||
|
removeAccessible grid =
|
||||||
|
let w = length $ head grid
|
||||||
|
h = length grid
|
||||||
|
positions = filter (isAccessible grid) [(x, y) | x <- [0 .. w - 1], y <- [0 .. h - 1]]
|
||||||
|
in removeRecursive grid positions
|
||||||
|
where
|
||||||
|
removeRecursive grid' [] = grid'
|
||||||
|
removeRecursive grid' (p : ps) = removeRecursive (removePaper grid' p) ps
|
||||||
|
|
||||||
|
countAccessibleRecursive :: Grid -> Int -> Int
|
||||||
|
countAccessibleRecursive grid tot
|
||||||
|
| countAccessible grid == 0 = tot
|
||||||
|
| otherwise = countAccessibleRecursive (removeAccessible grid) (tot + countAccessible grid)
|
||||||
|
|
||||||
parseInput :: IO [String]
|
parseInput :: IO [String]
|
||||||
parseInput = lines <$> readFile "input/day4.txt"
|
parseInput = lines <$> readFile "input/day4.txt"
|
||||||
@@ -53,3 +75,11 @@ day04_1 = do
|
|||||||
putStrLn $
|
putStrLn $
|
||||||
"Day 4, Puzzle 1 solution: "
|
"Day 4, Puzzle 1 solution: "
|
||||||
++ show result
|
++ show result
|
||||||
|
|
||||||
|
day04_2 :: IO ()
|
||||||
|
day04_2 = do
|
||||||
|
grid <- parseInput
|
||||||
|
let result = countAccessibleRecursive grid 0
|
||||||
|
putStrLn $
|
||||||
|
"Day 4, Puzzle 2 solution: "
|
||||||
|
++ show result
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ module Main (main) where
|
|||||||
import Day01 (day01_1, day01_2)
|
import Day01 (day01_1, day01_2)
|
||||||
import Day02 (day02_1, day02_2)
|
import Day02 (day02_1, day02_2)
|
||||||
import Day03 (day03_1, day03_2)
|
import Day03 (day03_1, day03_2)
|
||||||
import Day04 (day04_1)
|
import Day04 (day04_1, day04_2)
|
||||||
import System.Environment (getArgs)
|
import System.Environment (getArgs)
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
@@ -26,8 +26,10 @@ main = do
|
|||||||
day03_1
|
day03_1
|
||||||
day03_2
|
day03_2
|
||||||
"4" : "1" : _ -> day04_1
|
"4" : "1" : _ -> day04_1
|
||||||
|
"4" : "2" : _ -> day04_2
|
||||||
"4" : _ -> do
|
"4" : _ -> do
|
||||||
day04_1
|
day04_1
|
||||||
|
day04_2
|
||||||
"all" : _ -> do
|
"all" : _ -> do
|
||||||
day01_1
|
day01_1
|
||||||
day01_2
|
day01_2
|
||||||
@@ -36,4 +38,5 @@ main = do
|
|||||||
day03_1
|
day03_1
|
||||||
day03_2
|
day03_2
|
||||||
day04_1
|
day04_1
|
||||||
|
day04_2
|
||||||
_ -> error "Not implemented"
|
_ -> error "Not implemented"
|
||||||
|
|||||||
Reference in New Issue
Block a user