mirror of
https://codeberg.org/JasterV/aoc2024-haskell.git
synced 2026-04-26 18:10:05 +00:00
day 9 part one
This commit is contained in:
parent
c081936a7d
commit
a881f45ca3
4 changed files with 63 additions and 0 deletions
|
|
@ -37,6 +37,7 @@ library
|
||||||
Day6.Guard
|
Day6.Guard
|
||||||
Day7
|
Day7
|
||||||
Day8
|
Day8
|
||||||
|
Day9
|
||||||
other-modules:
|
other-modules:
|
||||||
Paths_aoc2024
|
Paths_aoc2024
|
||||||
autogen-modules:
|
autogen-modules:
|
||||||
|
|
@ -65,6 +66,7 @@ test-suite aoc2024-test
|
||||||
Day6Spec
|
Day6Spec
|
||||||
Day7Spec
|
Day7Spec
|
||||||
Day8Spec
|
Day8Spec
|
||||||
|
Day9Spec
|
||||||
Paths_aoc2024
|
Paths_aoc2024
|
||||||
autogen-modules:
|
autogen-modules:
|
||||||
Paths_aoc2024
|
Paths_aoc2024
|
||||||
|
|
|
||||||
1
input/day9.txt
Normal file
1
input/day9.txt
Normal file
File diff suppressed because one or more lines are too long
45
src/Day9.hs
Normal file
45
src/Day9.hs
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
module Day9 (partOne, partTwo) where
|
||||||
|
|
||||||
|
import Data.Char (digitToInt, isDigit)
|
||||||
|
|
||||||
|
type FileId = Int
|
||||||
|
|
||||||
|
data Block = File FileId | Free
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
partOne :: String -> Int
|
||||||
|
partOne input = sum $ zipWith (*) [0 ..] $ compactFiles $ parseDisk input
|
||||||
|
|
||||||
|
partTwo :: String -> Int
|
||||||
|
partTwo _ = 0
|
||||||
|
|
||||||
|
-- Compacting
|
||||||
|
compactFiles :: [Block] -> [FileId]
|
||||||
|
compactFiles blocks = go enumeratedBlocks (reverse enumeratedBlocks) []
|
||||||
|
where
|
||||||
|
enumeratedBlocks :: [(Int, Block)]
|
||||||
|
enumeratedBlocks = zip [0 ..] blocks
|
||||||
|
|
||||||
|
go [] _ acc = reverse acc
|
||||||
|
go _ [] acc = reverse acc
|
||||||
|
go ((lIndex, lBlock) : xs) ((rIndex, rBlock) : ys) acc
|
||||||
|
| lIndex > rIndex = reverse acc
|
||||||
|
| otherwise = case (lBlock, rBlock) of
|
||||||
|
(File fileId, _) -> go xs ((rIndex, rBlock) : ys) (fileId : acc)
|
||||||
|
(Free, File fileId) -> go xs ys (fileId : acc)
|
||||||
|
(Free, Free) -> go ((lIndex, lBlock) : xs) ys acc
|
||||||
|
|
||||||
|
-- Parsing
|
||||||
|
|
||||||
|
data ParseStep = ParseFile | ParseFree
|
||||||
|
|
||||||
|
parseDisk :: String -> [Block]
|
||||||
|
parseDisk input = go (parseInts input) ParseFile 0 []
|
||||||
|
where
|
||||||
|
go [] _ _id acc = reverse acc
|
||||||
|
go (space : xs) _ fileId [] = go xs ParseFree (fileId + 1) (replicate space (File fileId))
|
||||||
|
go (space : xs) ParseFree fileId acc = go xs ParseFile fileId (replicate space Free ++ acc)
|
||||||
|
go (space : xs) ParseFile fileId acc = go xs ParseFree (fileId + 1) (replicate space (File fileId) ++ acc)
|
||||||
|
|
||||||
|
parseInts :: String -> [Int]
|
||||||
|
parseInts = map digitToInt . filter isDigit
|
||||||
15
test/Day9Spec.hs
Normal file
15
test/Day9Spec.hs
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
module Day9Spec (spec) where
|
||||||
|
|
||||||
|
import Day9 (partOne, partTwo)
|
||||||
|
import Test.Hspec
|
||||||
|
|
||||||
|
spec :: Spec
|
||||||
|
spec = do
|
||||||
|
describe "PartOne" $ do
|
||||||
|
it "works" $ do
|
||||||
|
partOne input `shouldBe` 1928
|
||||||
|
describe "PartTwo" $ do
|
||||||
|
it "works" $ do
|
||||||
|
partTwo input `shouldBe` 0
|
||||||
|
where
|
||||||
|
input = "2333133121414131402"
|
||||||
Loading…
Reference in a new issue