<commit_before>{-# LANGUAGE LambdaCase #-}

module Main where

import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Maybe (mapMaybe)
import Linear.V2 (V2 (..))

data Square = Bug | Empty deriving (Eq, Show)

type Grid = Map (V2 Word) Square

toC :: Square -> Char
toC = \case Bug -> '#'; Empty -> '.'

fromC :: Char -> Maybe Square
fromC = \case '#' -> Just Bug; '.' -> Just Empty; _ -> Nothing

parse :: String -> Grid
parse s = M.fromList $ zip coords squares
  where
    ls = lines s
    n = fromIntegral $ length ls - 1
    coords = [V2 x y | x <- [0 .. n], y <- [0 .. n]]
    squares = concatMap (mapMaybe fromC) ls

readInput :: FilePath -> IO Grid
readInput = (parse <$>) . readFile

main :: IO ()
main = putStrLn "TODO"<commit_msg>pretty printing, next grid step<commit_after>{-# LANGUAGE LambdaCase #-}

module Main where

import Data.Map.Strict (Map)
import qualified Data.Map.Strict as M
import Data.Maybe (mapMaybe)
import Linear.V2 (V2 (..))

data Square = Bug | Empty deriving (Eq, Show)

type Grid = Map (V2 Int) Square

toC :: Square -> Char
toC = \case Bug -> '#'; Empty -> '.'

fromC :: Char -> Maybe Square
fromC = \case '#' -> Just Bug; '.' -> Just Empty; _ -> Nothing

parse :: String -> Grid
parse s = M.fromList $ zip coords squares
  where
    ls = lines s
    n = fromIntegral $ length ls - 1 -- assumes square grid…
    coords = [V2 x y | x <- [0 .. n], y <- [0 .. n]]
    squares = concatMap (mapMaybe fromC) ls

readInput :: FilePath -> IO Grid
readInput = (parse <$>) . readFile

pretty :: Grid -> String
pretty g = unlines ss
  where
    V2 x y = fst $ M.findMax g
    coords = [[V2 x' y' | y' <- [0 .. y]] | x' <- [0 .. x]]
    ss = fmap (fmap (toC . (g M.!))) coords

next :: Grid -> Grid
next g = M.mapWithKey update g
  where
    update v s
      | s == Bug && n == 1 = Bug
      | s == Empty && (n == 1 || n == 2) = Bug
      | otherwise = Empty
      where
        adj = [v + w | w <- [V2 (-1) 0, V2 1 0, V2 0 (-1), V2 0 1]]
        n = length . filter (== Bug) $ mapMaybe (g M.!?) adj

main :: IO ()
main = putStrLn . pretty =<< readInput "input_1"
{'fraction_non_alphanumeric': 0.1643770724774988, 'fraction_numerical': 0.016106110847939364, 'mean_word_length': 2.450980392156863, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5509431', 'n_tokens_mistral': 797, 'n_tokens_neox': 755, 'n_words': 282}