Tweaked some parsing code
[advent-of-code-21.git] / advent21 / Main.hs
index f187b98718cb4b595cea00b59d7c337b27755577..3048dafe31fddacea739847b68029a8c7e7a4192 100644 (file)
@@ -1,19 +1,12 @@
--- Writeup at https://work.njae.me.uk/2021/12/23/advent-of-code-2021-day-20/
-
-import Debug.Trace
+-- Writeup at https://work.njae.me.uk/2021/12/26/advent-of-code-2021-day-21/
 
 
+-- import Debug.Trace
 
 import Data.Text ()
 import qualified Data.Text.IO as TIO
 import Data.Attoparsec.Text hiding (take, takeWhile)
 import Control.Applicative
 
 
 import Data.Text ()
 import qualified Data.Text.IO as TIO
 import Data.Attoparsec.Text hiding (take, takeWhile)
 import Control.Applicative
 
--- import Control.Monad.State.Strict
--- import Control.Monad.Reader
--- import Control.Monad.Writer
--- import Control.Monad.RWS.Strict
--- import Control.Monad.Loops
-
 import qualified Data.Map.Strict as M
 import Data.Map.Strict ((!))
 import Data.List
 import qualified Data.Map.Strict as M
 import Data.Map.Strict ((!))
 import Data.List
@@ -36,19 +29,12 @@ type Games = MS.MultiSet Game
 type Dice = MS.MultiSet Int
 type Winners = MS.MultiSet Player
 
 type Dice = MS.MultiSet Int
 type Winners = MS.MultiSet Player
 
-
 instance Show Game where
 instance Show Game where
-
   show game = "{" ++ (showPlayer Player1) ++ (showActive) ++ (showPlayer Player2) ++ "}"
     where showPlayer p = (show $ position $ (players game) ! p) ++ "," ++ (show $ score $ (players game) ! p) 
           showActive = if (current game) == Player1 then "<" else ">"
 
 
   show game = "{" ++ (showPlayer Player1) ++ (showActive) ++ (showPlayer Player2) ++ "}"
     where showPlayer p = (show $ position $ (players game) ! p) ++ "," ++ (show $ score $ (players game) ! p) 
           showActive = if (current game) == Player1 then "<" else ">"
 
 
-
-
--- type GameState = State Game
-
-
 main :: IO ()
 main = 
   do  text <- TIO.readFile "data/advent21.txt"
 main :: IO ()
 main = 
   do  text <- TIO.readFile "data/advent21.txt"
@@ -103,22 +89,16 @@ nonDetGameSimulation winThreshold games0 dice winners0
         winners = MS.insertMany Player2 p2Wins $ MS.insertMany Player1 p1Wins winners0
 
 nonDetGameStep :: Games -> Dice -> Games
         winners = MS.insertMany Player2 p2Wins $ MS.insertMany Player1 p1Wins winners0
 
 nonDetGameStep :: Games -> Dice -> Games
+-- nonDetGameStep games dice | trace ("G0 >" ++ (show games) ++ "-" ++ (show dice)) False = undefined
 nonDetGameStep games dice = MS.foldOccur (nonDetGameStep1 dice) MS.empty games
 nonDetGameStep games dice = MS.foldOccur (nonDetGameStep1 dice) MS.empty games
--- nonDetGameStep games dice 
-  -- | trace ("G0 >" ++ (show games) ++ "-" ++ (show dice)) False = undefined
-  -- | otherwise = MS.foldOccur (nonDetGameStep1 games) MS.empty dice
 
 nonDetGameStep1 :: Dice -> Game -> MS.Occur -> Games -> Games
 
 nonDetGameStep1 :: Dice -> Game -> MS.Occur -> Games -> Games
-nonDetGameStep1 dice game gnum acc = MS.foldOccur (nonDetGameStep2 game gnum) MS.empty dice
--- nonDetGameStep1 game dice dnum acc 
-  -- | trace ("G1 >" ++ (show game) ++ "-" ++ (show dice) ++ ": " ++ (show gnum)) False = undefined
-  -- | otherwise = MS.foldOccur (nonDetGameStep2 dice dnum) acc game
+-- nonDetGameStep1 dice game gnum acc | trace ("G1 >" ++ (show game) ++ "-" ++ (show dice) ++ ": " ++ (show gnum)) False = undefined
+nonDetGameStep1 dice game gnum acc = MS.foldOccur (nonDetGameStep2 game gnum) acc dice
 
 nonDetGameStep2 :: Game -> MS.Occur -> Int -> MS.Occur -> Games -> Games
 
 nonDetGameStep2 :: Game -> MS.Occur -> Int -> MS.Occur -> Games -> Games
+-- nonDetGameStep2 dice dnum game gnum acc | trace ("G2 >" ++ (show game) ++ "-" ++ (show dice) ++ ": " ++ (show gnum) ++ "," ++ (show dnum)) False = undefined
 nonDetGameStep2 game gnum roll dnum acc = MS.insertMany game' (gnum * dnum) acc
 nonDetGameStep2 game gnum roll dnum acc = MS.insertMany game' (gnum * dnum) acc
--- nonDetGameStep2 dice dnum game gnum acc
-  -- | trace ("G2 >" ++ (show game) ++ "-" ++ (show dice) ++ ": " ++ (show gnum) ++ "," ++ (show dnum)) False = undefined
-  -- | otherwise = MS.insertMany game' (gnum * dnum) acc
   where game' = gameStep game roll
 
 
   where game' = gameStep game roll
 
 
@@ -133,7 +113,7 @@ mod1 a b = ((a - 1) `mod` b) + 1
 
 -- Parsing
 
 
 -- Parsing
 
-playerP = ("1" *> pure Player1) <|> ("2" *> pure Player2)
+playerP = (Player1 <$ "1") <|> (Player2 <$ "2")
 
 playerStateP = playerify <$> ("Player " *> playerP) <*> (" starting position: " *> decimal)
   where playerify name pos = (name, PlayerState {position = pos, score = 0})
 
 playerStateP = playerify <$> ("Player " *> playerP) <*> (" starting position: " *> decimal)
   where playerify name pos = (name, PlayerState {position = pos, score = 0})