<commit_before>module Day06 where

import           Data.List                     (foldl')
import qualified Data.Map.Strict               as M
import           Data.Maybe                    (fromMaybe)

import           Text.ParserCombinators.Parsec (ParseError, char, digit, endBy,
                                                many, parse, space, string, try,
                                                (<|>))

type Matrix  = M.Map XY Int

type Command = (Op, XY, XY)
data Op      = On | Off | Toggle deriving Show
type XY      = (Int, Int)

parseCommands :: String -> Either ParseError [Command]
parseCommands = parse commands ""
  where
    commands = command `endBy` eol
    command = do
      o <- op
      space
      p0 <- xy
      string " through "
      p1 <- xy
      return (o, p0, p1)

    op =  try (string "turn on")  *> return On
      <|> try (string "turn off") *> return Off
      <|>      string "toggle"    *> return Toggle

    xy = do
      x <- read <$> many digit
      char ','
      y <- read <$> many digit
      return (x,y)

    eol = try (string "\n\r")
      <|> try (string "\r\n")<commit_msg>Annotate parser fns<commit_after>module Day06 where

import           Data.List                     (foldl')
import qualified Data.Map.Strict               as M
import           Data.Maybe                    (fromMaybe)

import           Text.ParserCombinators.Parsec (GenParser, ParseError, char, digit, endBy,
                                                many, parse, space, string, try,
                                                (<|>))

type Matrix  = M.Map XY Int

type Command = (Op, XY, XY)
data Op      = On | Off | Toggle deriving Show
type XY      = (Int, Int)

parseCommands :: String -> Either ParseError [Command]
parseCommands = parse commands ""
  where
    commands = command `endBy` eol
    command :: GenParser Char st Command
    command = do
      o <- op
      space
      p0 <- xy
      string " through "
      p1 <- xy
      return (o, p0, p1)

    op :: GenParser Char st Op
    op =  try (string "turn on")  *> return On
      <|> try (string "turn off") *> return Off
      <|>      string "toggle"    *> return Toggle

    xy :: GenParser Char st XY
    xy = do
      x <- read <$> many digit
      char ','
      y <- read <$> many digit
      return (x,y)

    eol :: GenParser Char st String
    eol = try (string "\n\r")
      <|> try (string "\r\n")
{'fraction_non_alphanumeric': 0.10684818481848185, 'fraction_numerical': 0.0049504950495049506, 'mean_word_length': 1.4795501022494888, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2700860', 'n_tokens_mistral': 734, 'n_tokens_neox': 687, 'n_words': 244}