This commit is contained in:
Jonathan Chan 2018-12-06 20:07:26 -08:00
parent ac3bd77f23
commit 4858769427
4 changed files with 92 additions and 6 deletions

View File

@ -11,9 +11,9 @@ To arrive at [⟨ortho|normal⟩](https://hilb.ert.space) as December comes.
| 01 | < 0.1 | | 01 | < 0.1 |
| 02 | < 0.1 | | 02 | < 0.1 |
| 03 | ~ 2 | | 03 | ~ 2 |
| 04 | | | 04 | ~ 1 |
| 05 | | | 05 | ~ 1 |
| 06 | | | 06 | ~ 3 |
| 07 | | | 07 | |
| 08 | | | 08 | |
| 09 | | | 09 | |

View File

@ -30,4 +30,4 @@ import qualified Day24
import qualified Day25 import qualified Day25
main :: IO () main :: IO ()
main = Day05.main main = Day06.main

50
input/06.txt Normal file
View File

@ -0,0 +1,50 @@
77, 279
216, 187
72, 301
183, 82
57, 170
46, 335
55, 89
71, 114
313, 358
82, 88
78, 136
339, 314
156, 281
260, 288
125, 249
150, 130
210, 271
190, 258
73, 287
187, 332
283, 353
66, 158
108, 97
237, 278
243, 160
61, 52
353, 107
260, 184
234, 321
181, 270
104, 84
290, 109
193, 342
43, 294
134, 211
50, 129
92, 112
309, 130
291, 170
89, 204
186, 177
286, 302
188, 145
40, 52
254, 292
270, 287
238, 216
299, 184
141, 264
117, 129

View File

@ -1,6 +1,42 @@
module Day06 (main) where module Day06 (main) where
import Data.List (maximumBy, sortBy)
import Data.Ord (comparing)
import Data.Function (on)
import Data.Map (fromListWith, toAscList)
import Data.Set (fromList, toList)
-- given a list of elements, return pairs of the element with its frequency
-- if two elements have the same frequency, the elements are sorted in ascending order
getElemFreqs :: Ord a => [a] -> [(a, Int)]
getElemFreqs = sortBy (flip $ comparing snd) . toAscList . fromListWith (+) . (flip zip) (repeat 1)
part1 :: [(Int, Int)] -> Int
part1 coords =
let maxrow = fst $ maximumBy (comparing fst) coords
maxcol = snd $ maximumBy (comparing snd) coords
grid = map closest [(r, c) | r <- [1..maxrow], c <- [1..maxcol]]
edges = toList . fromList . map closest . concat $ [[(r, 1), (1, c), (r, maxcol), (maxrow, c)] | r <- [1..maxrow], c <- [1..maxcol]]
in snd . head . filter ((`notElem` edges) . fst) . getElemFreqs $ grid
where
closest coord =
let (car:cadr:_) = sortBy (comparing (manhattan coord)) coords
in if ((==) `on` manhattan coord) car cadr
then ((-1), (-1))
else car
manhattan (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2)
part2 :: [(Int, Int)] -> Int
part2 coords =
let maxrow = fst $ maximumBy (comparing fst) coords
maxcol = snd $ maximumBy (comparing snd) coords
in length . filter closeBy $ [(r, c) | r <- [1..maxrow], c <- [1..maxcol]]
where
closeBy coord = (sum $ map (manhattan coord) coords) < 10000
manhattan (r1, c1) (r2, c2) = abs (r1 - r2) + abs (c1 - c2)
main :: IO () main :: IO ()
main = do main = do
input <- readFile "input/06.txt" input <- map (read . ('(':) . (++ [')'])) . lines <$> readFile "input/06.txt"
print input print $ part1 input
print $ part2 input