|
| 1 | +import Text.Parsec |
| 2 | +import Parsing |
| 3 | +import Chart2d |
| 4 | +import qualified Data.HashMap.Strict as M |
| 5 | +import Searches |
| 6 | + |
| 7 | +main :: IO () |
| 8 | +main = optimisticInteract readD solve |
| 9 | + |
| 10 | +readD :: Parser [String] |
| 11 | +readD = many1 (oneOf ".|/\\-") `sepEndBy` newline |
| 12 | + |
| 13 | +solve input = unlines [ |
| 14 | + show $ input |
| 15 | + , show answer |
| 16 | + ] |
| 17 | + where |
| 18 | + inpchart = readM input |
| 19 | + |
| 20 | + starts = concat $ [[((0,y),'R'),((mx,y),'L')] | y <- [0..my]] ++ [[((x,0),'D'),((x,my),'U')] | x <- [0..mx]] |
| 21 | + where |
| 22 | + (mx,my) = maximum . M.keys $ inpchart |
| 23 | + |
| 24 | + answer = maximum . map (M.size . energized) $ starts |
| 25 | + |
| 26 | + energized start = M.fromList $ floodFill start contBeam |
| 27 | + where |
| 28 | + contBeam (c,d) = filter contained beams |
| 29 | + where |
| 30 | + here = inpchart M.! c |
| 31 | + beams = [(move c d', d') | d' <- splitBeam here d] |
| 32 | + contained (c,_) = M.member c inpchart |
| 33 | + |
| 34 | + splitBeam '.' d = [d] |
| 35 | + splitBeam '/' 'R' = "U" |
| 36 | + splitBeam '/' 'L' = "D" |
| 37 | + splitBeam '/' 'U' = "R" |
| 38 | + splitBeam '/' 'D' = "L" |
| 39 | + splitBeam '\\' 'R' = "D" |
| 40 | + splitBeam '\\' 'L' = "U" |
| 41 | + splitBeam '\\' 'U' = "L" |
| 42 | + splitBeam '\\' 'D' = "R" |
| 43 | + splitBeam '-' d |
| 44 | + | d `elem` "UD" = "RL" |
| 45 | + | otherwise = [d] |
| 46 | + splitBeam '|' d |
| 47 | + | d `elem` "RL" = "UD" |
| 48 | + | otherwise = [d] |
| 49 | + |
| 50 | + move (x,y) 'R' = (x+1,y) |
| 51 | + move (x,y) 'L' = (x-1,y) |
| 52 | + move (x,y) 'U' = (x,y-1) |
| 53 | + move (x,y) 'D' = (x,y+1) |
0 commit comments