From 4858769427f2ec50ba33e76b3df8e3c150cec922 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Thu, 6 Dec 2018 20:07:26 -0800 Subject: [PATCH] Day 06. --- README.md | 6 +++--- app/Main.hs | 2 +- input/06.txt | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Day06.hs | 40 ++++++++++++++++++++++++++++++++++++++-- 4 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 input/06.txt diff --git a/README.md b/README.md index 6d1d88b..e4d1706 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ To arrive at [⟨ortho|normal⟩](https://hilb.ert.space) as December comes. | 01 | < 0.1 | | 02 | < 0.1 | | 03 | ~ 2 | -| 04 | | -| 05 | | -| 06 | | +| 04 | ~ 1 | +| 05 | ~ 1 | +| 06 | ~ 3 | | 07 | | | 08 | | | 09 | | diff --git a/app/Main.hs b/app/Main.hs index 9c513f6..edb575b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -30,4 +30,4 @@ import qualified Day24 import qualified Day25 main :: IO () -main = Day05.main +main = Day06.main diff --git a/input/06.txt b/input/06.txt new file mode 100644 index 0000000..bb0948a --- /dev/null +++ b/input/06.txt @@ -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 diff --git a/src/Day06.hs b/src/Day06.hs index f145ccb..57b7583 100644 --- a/src/Day06.hs +++ b/src/Day06.hs @@ -1,6 +1,42 @@ 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 = do - input <- readFile "input/06.txt" - print input \ No newline at end of file + input <- map (read . ('(':) . (++ [')'])) . lines <$> readFile "input/06.txt" + print $ part1 input + print $ part2 input \ No newline at end of file