A space to discuss and keep up software development and manage your software career
An inclusive community for gaming enthusiasts
News and discussion of science and technology such as AI, VR, cryptocurrency, quantum computing, and more.
From composing and gigging to gear, hot music takes, and everything in between.
Discussing AI software development, and showing off what we're building.
Movie and TV enthusiasm, criticism and everything in-between.
A general discussion space for the Forem community. If it doesn't have a home elsewhere, it belongs here
Memes and software development shitposting
Web design, graphic design and everything in-between
A community of golfers and golfing enthusiasts
Your central hub for all things security. From ethical hacking and CTFs to GRC and career development, for beginners and pros alike
For engineers building software at scale. We discuss architecture, cloud-native, and SRE—the hard-won lessons you can't just Google
Discussing the core forem open source software project — features, bugs, performance, self-hosting.
A collaborative community for all things Crypto—from Bitcoin to protocol development and DeFi to NFTs and market analysis.
A place for parents to the share the joys, challenges, and wisdom that come from raising kids. We're here for them and for each other.
A community for makers, hobbyists, and professionals to discuss Arduino, Raspberry Pi, 3D printing, and much more.
I used Haskell. Definitely not the most efficient or optimized solution:
module Main where import qualified Data.Map.Strict as M import Data.Maybe (mapMaybe) type Coordinate = (Int, Int) toCoordinateMap :: [[a]] -> M.Map (Int, Int) a toCoordinateMap a = M.fromList $ do (y, row) <- zip [0 ..] a (x, v) <- zip [0 ..] row pure ((x, y), v) findAdjSeats :: Coordinate -> M.Map (Int, Int) Char -> [Coordinate] findAdjSeats (x, y) seats = filter (`M.member` seats) [ (x-1, y-1), (x, y-1), (x+1, y-1), (x-1, y), (x+1, y), (x-1, y+1), (x, y+1), (x+1, y+1) ] findFirstSeatInSight :: Coordinate -> M.Map Coordinate Char -> [Coordinate] findFirstSeatInSight coord seatMap = mapMaybe (firstInSight coord) [ (-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1) ] where firstInSight (x, y) (dx, dy) = (x-dx, y-dy) `M.lookup` seatMap >>= aux where aux seat = if seat == 'L' || seat == '#' then Just (x-dx, y-dy) else firstInSight (x-dx, y-dy) (dx, dy) runSimulation :: M.Map Coordinate Char -> (Coordinate -> M.Map Coordinate Char -> [Coordinate]) -> Int -> Int runSimulation seatMap findAdjSeatsF occupiedSeats = if seatMap == nextCycle then length $ M.filter (== '#') nextCycle else runSimulation nextCycle findAdjSeatsF occupiedSeats where noOccupiedAdjSeats = not . any ((== Just '#') . (`M.lookup` seatMap)) moreOrEqThanNOccupiedSeats n = (>= n) . length . filter ((== Just '#') . (`M.lookup` seatMap)) nextCycle = M.mapWithKey (\coordinate c -> case c of '.' -> '.' 'L' -> if noOccupiedAdjSeats $ findAdjSeatsF coordinate seatMap then '#' else 'L' '#' -> if moreOrEqThanNOccupiedSeats occupiedSeats $ findAdjSeatsF coordinate seatMap then 'L' else '#' _ -> error $ "cannot do" ++ show c ) seatMap main :: IO () main = do input <- lines <$> readFile "input.txt" let seatMap = toCoordinateMap input print $ runSimulation seatMap findAdjSeats 4 print $ runSimulation seatMap findFirstSeatInSight 5
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I used Haskell. Definitely not the most efficient or optimized solution: