|
1 | 1 | module Day9 (partOne, partTwo) where |
2 | 2 |
|
3 | 3 | import Data.Char (digitToInt, isDigit) |
| 4 | +import Data.Maybe |
4 | 5 |
|
5 | 6 | type FileId = Int |
6 | 7 |
|
7 | | -data Block = File FileId | Free |
| 8 | +type Space = Int |
| 9 | + |
| 10 | +type Position = Int |
| 11 | + |
| 12 | +data Block = FileBlock Position FileId | FreeBlock Position |
8 | 13 | deriving (Show, Eq) |
9 | 14 |
|
| 15 | +data Object = File Position Space FileId | FreeSpace Position Space |
| 16 | + |
10 | 17 | partOne :: String -> Int |
11 | | -partOne input = sum $ zipWith (*) [0 ..] $ compactFiles $ parseDisk input |
| 18 | +partOne input = calculateCheckSum compactFiles |
| 19 | + where |
| 20 | + blocks = concatMap getBlocks $ parseDisk input |
| 21 | + |
| 22 | + compactFiles :: [Block] |
| 23 | + compactFiles = go blocks (reverse blocks) [] |
| 24 | + where |
| 25 | + go [] _ acc = reverse acc |
| 26 | + go _ [] acc = reverse acc |
| 27 | + go (lBlock : xs) (rBlock : ys) acc |
| 28 | + | lIndex > rIndex = reverse acc |
| 29 | + | otherwise = case (lBlock, rBlock) of |
| 30 | + (FileBlock _ _, _) -> go xs (rBlock : ys) (lBlock : acc) |
| 31 | + (FreeBlock _, FileBlock _ _) -> go xs ys (rBlock : acc) |
| 32 | + (FreeBlock _, FreeBlock _) -> go (lBlock : xs) ys acc |
| 33 | + where |
| 34 | + lIndex = getBlockPosition lBlock |
| 35 | + rIndex = getBlockPosition rBlock |
12 | 36 |
|
13 | 37 | partTwo :: String -> Int |
14 | 38 | partTwo _ = 0 |
15 | 39 |
|
16 | | --- Compacting |
17 | | -compactFiles :: [Block] -> [FileId] |
18 | | -compactFiles blocks = go enumeratedBlocks (reverse enumeratedBlocks) [] |
19 | | - where |
20 | | - enumeratedBlocks :: [(Int, Block)] |
21 | | - enumeratedBlocks = zip [0 ..] blocks |
22 | | - |
23 | | - go [] _ acc = reverse acc |
24 | | - go _ [] acc = reverse acc |
25 | | - go ((lIndex, lBlock) : xs) ((rIndex, rBlock) : ys) acc |
26 | | - | lIndex > rIndex = reverse acc |
27 | | - | otherwise = case (lBlock, rBlock) of |
28 | | - (File fileId, _) -> go xs ((rIndex, rBlock) : ys) (fileId : acc) |
29 | | - (Free, File fileId) -> go xs ys (fileId : acc) |
30 | | - (Free, Free) -> go ((lIndex, lBlock) : xs) ys acc |
| 40 | +-- Blocks & Objects |
| 41 | + |
| 42 | +calculateCheckSum :: [Block] -> Int |
| 43 | +calculateCheckSum blocks = sum $ zipWith ((*) . fromMaybe 0 . getFileId) blocks [0 ..] |
| 44 | + |
| 45 | +getBlocks :: Object -> [Block] |
| 46 | +getBlocks (File position space fileId) = map (`FileBlock` fileId) [position .. (position + space - 1)] |
| 47 | +getBlocks (FreeSpace position space) = map FreeBlock [position .. (position + space - 1)] |
| 48 | + |
| 49 | +getFileId :: Block -> Maybe FileId |
| 50 | +getFileId (FileBlock _ fileId) = Just fileId |
| 51 | +getFileId _ = Nothing |
| 52 | + |
| 53 | +getBlockPosition :: Block -> Position |
| 54 | +getBlockPosition (FileBlock pos _) = pos |
| 55 | +getBlockPosition (FreeBlock pos) = pos |
31 | 56 |
|
32 | 57 | -- Parsing |
33 | 58 |
|
34 | 59 | data ParseStep = ParseFile | ParseFree |
35 | 60 |
|
36 | | -parseDisk :: String -> [Block] |
37 | | -parseDisk input = go (parseInts input) ParseFile 0 [] |
| 61 | +parseDisk :: String -> [Object] |
| 62 | +parseDisk input = go (parseInts input) ParseFile 0 0 [] |
38 | 63 | where |
39 | | - go [] _ _id acc = reverse acc |
40 | | - go (space : xs) _ fileId [] = go xs ParseFree (fileId + 1) (replicate space (File fileId)) |
41 | | - go (space : xs) ParseFree fileId acc = go xs ParseFile fileId (replicate space Free ++ acc) |
42 | | - go (space : xs) ParseFile fileId acc = go xs ParseFree (fileId + 1) (replicate space (File fileId) ++ acc) |
| 64 | + go :: [Int] -> ParseStep -> FileId -> Position -> [Object] -> [Object] |
| 65 | + go [] _ _ _ acc = reverse acc |
| 66 | + go (space : xs) _ fileId position [] = go xs ParseFree (fileId + 1) (position + space) [File position space fileId] |
| 67 | + go (space : xs) ParseFree fileId position acc = go xs ParseFile fileId (position + space) (FreeSpace position space : acc) |
| 68 | + go (space : xs) ParseFile fileId position acc = go xs ParseFree (fileId + 1) (position + space) (File position space fileId : acc) |
43 | 69 |
|
44 | 70 | parseInts :: String -> [Int] |
45 | 71 | parseInts = map digitToInt . filter isDigit |
0 commit comments