-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay9.hs
160 lines (141 loc) · 5.21 KB
/
Day9.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module Day9
( part1
, part2
) where
import Control.Monad.State (State, evalState, get, put)
import Data.Bifunctor (first, second)
import Data.ByteString (ByteString)
import Data.Either (fromRight)
import Data.Function (on)
import Data.IntMap as M (IntMap, delete, fromList,
insert, lookup, notMember,
null, size, (!))
import Data.IntSet as S (IntSet, delete, empty,
findMin, insert, null,
singleton)
import Data.List as L (delete, groupBy, minimumBy,
null, partition, sort)
import Data.Maybe (fromJust, isJust)
import Data.Ord (comparing)
import Data.Void (Void)
import Helpers.Parsers.ByteString (digitToInt)
import Text.Megaparsec (ParsecT, runParserT, (<|>))
import Text.Megaparsec.Byte (digitChar, eol)
data FileBlock = FileBlock
{ getIndex :: Index
, getPos :: Pos
, getLength :: Length
} deriving (Show, Eq)
data EmptyBlock = EmptyBlock
{ emptyPos :: Pos
, emptyLength :: Length
} deriving (Show, Eq)
type Index = Int
type Pos = Int
type Length = Int
type Files = [FileBlock]
type Blocks = [EmptyBlock]
instance Ord EmptyBlock where
compare e1 e2 =
compare (emptyLength e1) (emptyLength e2)
`mappend` compare (emptyPos e1) (emptyPos e2)
type BlockMap = IntMap IntSet
type Parser = ParsecT Void ByteString (State (Bool, Int, Int, Files))
parseInput :: Parser (Files, Blocks)
parseInput =
parseBlocks
<|> (do
(_, _, _, files) <- get
return (files, []))
parseBlocks :: Parser (Files, Blocks)
parseBlocks = do
(isEmpty, pos, index, files) <- get
blockLength <- digitToInt <$> digitChar
let pos' = pos + blockLength
state
| blockLength == 0 && isEmpty = put (False, pos, index, files)
| blockLength == 0 = put (True, pos, index + 1, files)
| isEmpty = put (False, pos', index, files)
| otherwise =
put (True, pos', index + 1, FileBlock index pos blockLength : files)
state
if isEmpty
then second (EmptyBlock pos blockLength :) <$> parseInput
else parseInput
sortDisk :: (Files, Blocks) -> Files
sortDisk (nextFile@(FileBlock index filePos fileLength):files, emptyBlock:blocks)
| emptyPos emptyBlock > filePos = nextFile : files
| otherwise = fileBlock : sortDisk (files', blocks')
where
availableSpace = min fileLength . emptyLength $ emptyBlock
fileBlock = FileBlock index (emptyPos emptyBlock) availableSpace
nextFile' = FileBlock index filePos (fileLength - availableSpace)
emptyBlock' =
EmptyBlock
(emptyPos emptyBlock + availableSpace)
(emptyLength emptyBlock - availableSpace)
files'
| availableSpace == fileLength = files
| otherwise = nextFile' : files
blocks'
| availableSpace == emptyLength emptyBlock = blocks
| otherwise = emptyBlock' : blocks
buildBlockMap :: Blocks -> BlockMap
buildBlockMap =
M.fromList
. map
(foldr
(\(EmptyBlock pos el) (_, s) -> (el, S.insert pos s))
(0, S.empty))
. groupBy ((==) `on` emptyLength)
. sort
defragment :: (Files, BlockMap) -> Files
defragment (file:files, blocks)
| L.null files = []
| M.null blocks = file : files
| L.null left = file : defragment (files, blocks')
| otherwise = file' : defragment (files, blocks''')
where
availableEmptyBlocks =
map (second fromJust)
. filter (isJust . snd)
. map (\l -> (l, S.findMin <$> M.lookup l blocks))
$ [getLength file .. 9]
(left, right) = partition ((< getPos file) . snd) availableEmptyBlocks
(el, pos) = minimumBy (comparing snd) left
file' = file {getPos = pos}
el' = el - getLength file
pos' = pos + getLength file
oldBlocks = S.delete pos . (!) blocks $ el
newBlocks
| el' `notMember` blocks' = singleton pos'
| otherwise = S.insert pos' . (!) blocks' $ el'
blocks' = foldr (M.delete . fst) blocks right
blocks''
| el' == 0 = blocks'
| otherwise = M.insert el' newBlocks blocks'
blocks'''
| S.null oldBlocks = M.delete el blocks''
| otherwise = M.insert el oldBlocks blocks''
-- The sum of n numbers from filePos to filePos + fileLength - 1 is fileLength *
-- filePos + ((fileLength * (fileLength - 1)) / 2)
checksum :: FileBlock -> Int
checksum (FileBlock index filePos fileLength) =
(fileLength * filePos + div (fileLength * (fileLength - 1)) 2) * index
part1 :: Bool -> ByteString -> String
part1 _ =
show
. foldr ((+) . checksum) 0
. sortDisk
. fromRight ([], [])
. flip evalState (False, 0, 0, [])
. runParserT parseInput ""
part2 :: Bool -> ByteString -> String
part2 _ =
show
. foldr ((+) . checksum) 0
. defragment
. second buildBlockMap
. fromRight ([], [])
. flip evalState (False, 0, 0, [])
. runParserT parseInput ""