1
0
Fork 0

Day 23 - part 1.

This commit is contained in:
Jonathan Chan 2018-12-23 16:41:57 -08:00
parent 81ab71c823
commit 0380a9165e
3 changed files with 1032 additions and 3 deletions

View File

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

1000
input/23.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,35 @@
module Day23 (main) where module Day23 (main) where
data Bot = Bot Position Radius deriving (Eq)
type Position = (Int, Int, Int)
type Radius = Int
instance Ord Bot where
Bot _ rad1 <= Bot _ rad2 = rad1 <= rad2
parse :: String -> Bot
parse str =
let pos = takeWhile (/= '>') . drop 5 $ str
rad = drop 2 . last . words $ str
in Bot (read $ "(" ++ pos ++ ")") (read rad)
manhattan :: Position -> Position -> Int
manhattan (x1, y1, z1) (x2, y2, z2) = abs (x1 - x2) + abs (y1 - y2) + abs (z1 - z2)
-- botInRange b1 b2 = is b2 within the range of b1?
botInRange :: Bot -> Bot -> Bool
botInRange (Bot pos1 radius) (Bot pos2 _) = manhattan pos1 pos2 <= radius
-- inRange pos bot = is pos within the range of bot?
inRange :: Bot -> Position -> Bool
inRange (Bot bPos radius) pos = manhattan bPos pos <= radius
part1 :: [Bot] -> Int
part1 bots =
let maxBot = maximum bots
in length $ filter (botInRange maxBot) bots
main :: IO () main :: IO ()
main = do main = do
input <- readFile "input/23.txt" bots <- map parse . lines <$> readFile "input/23.txt"
print input print $ part1 bots