1
0
Fork 0
adventofcode/src/Day11.hs

23 lines
740 B
Haskell
Raw Normal View History

2018-11-26 02:32:15 +00:00
module Day11 (main) where
2018-12-13 02:08:36 +00:00
import Data.Char (digitToInt)
import Data.List (maximumBy)
import Data.Ord (comparing)
indexToCoord :: Int -> (Int, Int)
indexToCoord i = (1 + i `div` 300, 1 + i `mod` 300)
powerLevel :: (Int, Int) -> Int
powerLevel (x, y) = (subtract 5) . digitToInt . (!! 2) . reverse . show $ ((x + 10) * y + 7672) * (x + 10)
powerSquare :: Int -> (Int, Int) -> Int
powerSquare size (x, y) =
if x > 300 - size + 1 || y > 300 - size + 1 then -5
else sum [powerLevel (x', y') | x' <- [x .. x + size - 1], y' <- [y .. y + size - 1]]
part1 :: (Int, Int)
part1 = indexToCoord . fst . maximumBy (comparing snd) . zip [0..] . map (powerSquare 3 . indexToCoord) $ [0 .. 300 * 300]
2018-11-26 02:32:15 +00:00
main :: IO ()
main = do
2018-12-13 02:08:36 +00:00
print part1