Day 23 - part 1.
This commit is contained in:
parent
81ab71c823
commit
0380a9165e
|
@ -30,4 +30,4 @@ import qualified Day24
|
||||||
import qualified Day25
|
import qualified Day25
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = Day22.main
|
main = Day23.main
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
33
src/Day23.hs
33
src/Day23.hs
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue