1
0
Fork 0

Day 03 - preliminary solution using lists; will implement using Map/Set/etc. later.

This commit is contained in:
Jonathan Chan 2018-12-03 00:24:21 -08:00
parent b07bc495bb
commit 242ec7fdc4
4 changed files with 1293 additions and 3 deletions

View File

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

1259
input/03.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -22,6 +22,7 @@ description: Please see the README on GitHub at <https://github.com/iona
dependencies: dependencies:
- base >= 4.7 && < 5 - base >= 4.7 && < 5
- containers - containers
- text
library: library:
source-dirs: src source-dirs: src

View File

@ -1,6 +1,36 @@
module Day03 (main) where module Day03 (main) where
import Data.Text (split, pack, unpack)
type Claim = (Int, Int, Int, Int, Int) -- id, left, top, width, height
(%) = mod
(//) = div
parse :: String -> Claim
parse str =
let i:l:t:w:h:[] = map (read . unpack) . tail . split (flip elem $ "#@,:x") . pack . filter (/= ' ') $ str
in (i, l, t, w, h)
doesClaim :: Int -> Claim -> Bool
doesClaim cell (_, left, top, width, height) =
let row = cell // 1000
col = cell % 1000
in (row >= top) && (row < top + height) && (col >= left) && (col < left + width)
claimCount :: [Claim] -> Int -> Int
claimCount claims cell = length . filter (doesClaim cell) $ claims
part1 :: [Claim] -> Int
part1 claims = length . filter id . map ((> 1) . claimCount claims) $ [0 .. 1000 * 1000 - 1]
part2 :: [Claim] -> Claim
part2 claims = filterOverlaps 0 claims
where filterOverlaps cell acc =
let newAcc = if claimCount claims cell > 1 then filter (not . doesClaim cell) acc else acc
in if length newAcc == 1 then head newAcc else filterOverlaps (cell + 1) newAcc
main :: IO () main :: IO ()
main = do main = do
input <- readFile "input/03.txt" input <- map parse . lines <$> readFile "input/03.txt"
print input print $ part1 input
print $ part2 input