default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, text
- , megaparsec
\ No newline at end of file
+ , megaparsec
+
+-- executable advent08foldable
+-- hs-source-dirs: src/advent08
+-- main-is: advent08-foldable.hs
+-- default-language: Haskell2010
+-- build-depends: base >= 4.7 && < 5
+-- , text
+-- , megaparsec
+
+executable advent08tree
+ hs-source-dirs: src/advent08
+ main-is: advent08-treelibrary.hs
+ default-language: Haskell2010
+ build-depends: base >= 4.7 && < 5
+ , text
+ , megaparsec
+ , containers
+
+executable advent09
+ hs-source-dirs: src/advent09
+ main-is: advent09.hs
+ default-language: Haskell2010
+ build-depends: base >= 4.7 && < 5
+ , text
+ , megaparsec
+ , containers
--- /dev/null
+458 players; last marble is worth 71307 points
--- /dev/null
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 9 - Advent of Code 2018</title>
+<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
+<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
+<link rel="stylesheet" type="text/css" href="/static/style.css?18"/>
+<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
+<link rel="shortcut icon" href="/favicon.png"/>
+</head><!--
+
+
+
+
+Oh, hello! Funny seeing you here.
+
+I appreciate your enthusiasm, but you aren't going to find much down here.
+There certainly aren't clues to any of the puzzles. The best surprises don't
+even appear in the source until you unlock them for real.
+
+Please be careful with automated requests; I'm not Google, and I can only take
+so much traffic. Please be considerate so that everyone gets to play.
+
+If you're curious about how Advent of Code works, it's running on some custom
+Perl code. Other than a few integrations (auth, analytics, ads, social media),
+I built the whole thing myself, including the design, animations, prose, and
+all of the puzzles.
+
+The puzzles are most of the work; the easiest ones take 3-4 hours each, but the
+harder ones take 6-8 hours, and a few even longer than that. A lot of effort
+went into building this thing - I hope you're enjoying playing it as much as I
+enjoyed making it for you!
+
+If you'd like to hang out, I'm @ericwastl on Twitter.
+
+- Eric Wastl
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+-->
+<body>
+<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2018/about">[About]</a></li><li><a href="/2018/events">[Events]</a></li><li><a href="https://teespring.com/adventofcode" target="_blank">[Shop]</a></li><li><a href="/2018/settings">[Settings]</a></li><li><a href="/2018/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2018/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">18*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap"><y></span><a href="/2018">2018</a><span class="title-event-wrap"></y></span></h1><nav><ul><li><a href="/2018">[Calendar]</a></li><li><a href="/2018/support">[AoC++]</a></li><li><a href="/2018/sponsors">[Sponsors]</a></li><li><a href="/2018/leaderboard">[Leaderboard]</a></li><li><a href="/2018/stats">[Stats]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2018/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://aoc.prodo.ai/" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);" rel="noopener">Alfie by Prodo</a> - a more immediate, feedback-driven coding experience. Try our online JavaScript playground with Advent of Code!</div></div>
+</div><!--/sidebar-->
+
+<main>
+<article class="day-desc"><h2>--- Day 9: Marble Mania ---</h2><p>You talk to the Elves while you wait for your navigation system to <span title="Do you have any idea how long it takes to load navigation data for all of time and space?!">initialize</span>. To pass the time, they introduce you to their favorite <a href="https://en.wikipedia.org/wiki/Marble_(toy)">marble</a> game.</p>
+<p>The Elves play this game by taking turns arranging the marbles in a <em>circle</em> according to very particular rules. The marbles are numbered starting with <code>0</code> and increasing by <code>1</code> until every marble has a number.</p>
+<p>First, the marble numbered <code>0</code> is placed in the circle. At this point, while it contains only a single marble, it is still a circle: the marble is both clockwise from itself and counter-clockwise from itself. This marble is designated the <em>current marble</em>.</p>
+<p>Then, each Elf takes a turn placing the <em>lowest-numbered remaining marble</em> into the circle between the marbles that are <code>1</code> and <code>2</code> marbles <em>clockwise</em> of the current marble. (When the circle is large enough, this means that there is one marble between the marble that was just placed and the current marble.) The marble that was just placed then becomes the <em>current marble</em>.</p>
+<p>However, if the marble that is about to be placed has a number which is a multiple of <code>23</code>, <em>something entirely different happens</em>. First, the current player keeps the marble they would have placed, adding it to their <em>score</em>. In addition, the marble <code>7</code> marbles <em>counter-clockwise</em> from the current marble is <em>removed</em> from the circle and <em>also</em> added to the current player's score. The marble located immediately <em>clockwise</em> of the marble that was removed becomes the new <em>current marble</em>.</p>
+<p>For example, suppose there are 9 players. After the marble with value <code>0</code> is placed in the middle, each player (shown in square brackets) takes a turn. The result of each of those turns would produce circles of marbles like this, where clockwise is to the right and the resulting current marble is in parentheses:</p>
+<pre><code>[-] <em>(0)</em>
+[1] 0<em> (1)</em>
+[2] 0<em> (2)</em> 1
+[3] 0 2 1<em> (3)</em>
+[4] 0<em> (4)</em> 2 1 3
+[5] 0 4 2<em> (5)</em> 1 3
+[6] 0 4 2 5 1<em> (6)</em> 3
+[7] 0 4 2 5 1 6 3<em> (7)</em>
+[8] 0<em> (8)</em> 4 2 5 1 6 3 7
+[9] 0 8 4<em> (9)</em> 2 5 1 6 3 7
+[1] 0 8 4 9 2<em>(10)</em> 5 1 6 3 7
+[2] 0 8 4 9 2 10 5<em>(11)</em> 1 6 3 7
+[3] 0 8 4 9 2 10 5 11 1<em>(12)</em> 6 3 7
+[4] 0 8 4 9 2 10 5 11 1 12 6<em>(13)</em> 3 7
+[5] 0 8 4 9 2 10 5 11 1 12 6 13 3<em>(14)</em> 7
+[6] 0 8 4 9 2 10 5 11 1 12 6 13 3 14 7<em>(15)</em>
+[7] 0<em>(16)</em> 8 4 9 2 10 5 11 1 12 6 13 3 14 7 15
+[8] 0 16 8<em>(17)</em> 4 9 2 10 5 11 1 12 6 13 3 14 7 15
+[9] 0 16 8 17 4<em>(18)</em> 9 2 10 5 11 1 12 6 13 3 14 7 15
+[1] 0 16 8 17 4 18 9<em>(19)</em> 2 10 5 11 1 12 6 13 3 14 7 15
+[2] 0 16 8 17 4 18 9 19 2<em>(20)</em>10 5 11 1 12 6 13 3 14 7 15
+[3] 0 16 8 17 4 18 9 19 2 20 10<em>(21)</em> 5 11 1 12 6 13 3 14 7 15
+[4] 0 16 8 17 4 18 9 19 2 20 10 21 5<em>(22)</em>11 1 12 6 13 3 14 7 15
+[5] 0 16 8 17 4 18<em>(19)</em> 2 20 10 21 5 22 11 1 12 6 13 3 14 7 15
+[6] 0 16 8 17 4 18 19 2<em>(24)</em>20 10 21 5 22 11 1 12 6 13 3 14 7 15
+[7] 0 16 8 17 4 18 19 2 24 20<em>(25)</em>10 21 5 22 11 1 12 6 13 3 14 7 15
+</code></pre>
+<p>The goal is to be the <em>player with the highest score</em> after the last marble is used up. Assuming the example above ends after the marble numbered <code>25</code>, the winning score is <code>23+9=<em>32</em></code> (because player 5 kept marble <code>23</code> and removed marble <code>9</code>, while no other player got any points in this very short example game).</p>
+<p>Here are a few more examples:</p>
+<ul>
+<li><code>10</code> players; last marble is worth <code>1618</code> points: high score is <em><code>8317</code></em></li>
+<li><code>13</code> players; last marble is worth <code>7999</code> points: high score is <em><code>146373</code></em></li>
+<li><code>17</code> players; last marble is worth <code>1104</code> points: high score is <em><code>2764</code></em></li>
+<li><code>21</code> players; last marble is worth <code>6111</code> points: high score is <em><code>54718</code></em></li>
+<li><code>30</code> players; last marble is worth <code>5807</code> points: high score is <em><code>37305</code></em></li>
+</ul>
+<p><em>What is the winning Elf's score?</em></p>
+</article>
+<p>Your puzzle answer was <code>398048</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Amused by the speed of your answer, the Elves are curious:</p>
+<p><em>What would the new winning Elf's score be if the number of the last marble were 100 times larger?</em></p>
+</article>
+<p>Your puzzle answer was <code>3180373421</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
+<p>At this point, you should <a href="/2018">return to your advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="9/input" target="_blank">get your puzzle input</a>.</p>
+<p>You can also <span class="share">[Share<span class="share-content">on
+ <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Marble+Mania%22+%2D+Day+9+%2D+Advent+of+Code+2018&url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F9&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
+ <a href="http://www.reddit.com/submit?url=https%3A%2F%2Fadventofcode%2Ecom%2F2018%2Fday%2F9&title=I%27ve+completed+%22Marble+Mania%22+%2D+Day+9+%2D+Advent+of+Code+2018" target="_blank">Reddit</a
+></span>]</span> this puzzle.</p>
+</main>
+
+<!-- ga -->
+<script>
+(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ga('create', 'UA-69522494-1', 'auto');
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file
--- /dev/null
+{-# LANGUAGE OverloadedStrings #-}
+
+-- import Data.List
+
+import Data.Monoid
+import Data.Text (Text)
+import qualified Data.Text.IO as TIO
+
+import Data.Void (Void)
+
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import qualified Text.Megaparsec.Char.Lexer as L
+import qualified Control.Applicative as CA
+
+
+data Tree a = Node [Tree a] a deriving (Eq, Show)
+
+instance Foldable Tree where
+ foldMap f (Node (t: ts) m) = f m <> foldMap (foldMap f) ts
+
+
+main :: IO ()
+main = do
+ text <- TIO.readFile "data/advent08-small.txt"
+ let treeSpec = successfulParse text
+ let (tree, _) = parseTree treeSpec
+ print $ foldMap id tree
+ print $ part1 tree
+ print $ part2 tree
+
+-- part1 = foldMap sum
+part1 = metadataOfTree
+
+part2 = valueOfTree
+
+
+parseTree (c:m:spec) = (Node children metadata, remainder')
+ where (children, remainder) = parseManyTree c spec
+ metadata = take m remainder
+ remainder' = drop m remainder
+
+parseManyTree n spec
+ | n == 0 = ([], spec)
+ | otherwise = ((tree:otherTrees), remainder')
+ where (tree, remainder) = parseTree spec
+ (otherTrees, remainder') = parseManyTree (n-1) remainder
+
+metadataOfTree (Node trees metadata) = metadata ++ (concatMap metadataOfTree trees)
+
+valueOfTree (Node trees metadata)
+ | null trees = sum metadata
+ | otherwise = sum selectedValues
+ where childValues = map valueOfTree trees
+ selectedValues = map (\v -> childValues!!(v-1)) $ filter (<= (length trees)) metadata
+
+
+-- Parse the input file
+
+type Parser = Parsec Void Text
+
+sc :: Parser ()
+sc = L.space (skipSome spaceChar) CA.empty CA.empty
+-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty
+
+lexeme = L.lexeme sc
+integer = lexeme L.decimal
+
+treeFileP = many integer
+
+successfulParse :: Text -> [Int]
+successfulParse input =
+ case parse treeFileP "input" input of
+ Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right treeSpec -> treeSpec
\ No newline at end of file
--- /dev/null
+{-# LANGUAGE OverloadedStrings #-}
+
+-- import Data.List
+
+import Data.Tree (Tree(Node), rootLabel, subForest)
+
+import Data.Text (Text)
+import qualified Data.Text.IO as TIO
+
+import Data.Void (Void)
+
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import qualified Text.Megaparsec.Char.Lexer as L
+import qualified Control.Applicative as CA
+
+main :: IO ()
+main = do
+ text <- TIO.readFile "data/advent08-small.txt"
+ let treeSpec = successfulParse text
+ let (tree, _) = parseTree treeSpec
+ -- print $ foldMap sum tree
+ print $ part1 tree
+ print $ part2 tree
+
+part1 = sum . fmap sum
+
+part2 = valueOfTree
+
+
+parseTree (c:m:spec) = (Node metadata children, remainder')
+ where (children, remainder) = parseManyTree c spec
+ metadata = take m remainder
+ remainder' = drop m remainder
+
+parseManyTree n spec
+ | n == 0 = ([], spec)
+ | otherwise = ((tree:otherTrees), remainder')
+ where (tree, remainder) = parseTree spec
+ (otherTrees, remainder') = parseManyTree (n-1) remainder
+
+valueOfTree (Node metadata trees)
+ | null trees = sum metadata
+ | otherwise = sum selectedValues
+ where childValues = map valueOfTree trees
+ selectedValues = map (\v -> childValues!!(v-1)) $ filter (<= (length trees)) metadata
+
+
+-- Parse the input file
+
+type Parser = Parsec Void Text
+
+sc :: Parser ()
+sc = L.space (skipSome spaceChar) CA.empty CA.empty
+-- sc = L.space (skipSome (char ' ')) CA.empty CA.empty
+
+lexeme = L.lexeme sc
+integer = lexeme L.decimal
+
+treeFileP = many integer
+
+successfulParse :: Text -> [Int]
+successfulParse input =
+ case parse treeFileP "input" input of
+ Left _error -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right treeSpec -> treeSpec
\ No newline at end of file
--- /dev/null
+{-# LANGUAGE OverloadedStrings, ViewPatterns, PatternSynonyms #-}
+
+import Data.List
+
+import Data.Foldable (toList)
+
+import Data.Text (Text)
+import qualified Data.Text.IO as TIO
+
+import Data.Void (Void)
+
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import qualified Text.Megaparsec.Char.Lexer as L
+import qualified Control.Applicative as CA
+
+-- import Data.Map.Strict ((!))
+import qualified Data.Map.Strict as M
+
+import qualified Data.Sequence as Q
+import Data.Sequence ((<|), (|>), ViewL((:<)), ViewR((:>)) )
+
+-- zipper of left, current, right
+data Circle = Circle (Q.Seq Integer) Integer (Q.Seq Integer) deriving (Eq)
+type Score = M.Map Integer Integer -- player -> score
+data Game = Game Circle Score deriving (Show, Eq)
+
+instance Show Circle where
+ show (Circle left current right) = (showSide left) ++ " (" ++ (show current) ++ ") " ++ (showSide right)
+ where showSide s = intercalate " " $ map show $ toList s
+
+main :: IO ()
+main = do
+ text <- TIO.readFile "data/advent09.txt"
+ let (numberOfPlayers, numberOfMarbles) = successfulParse text
+ -- let numberOfPlayers = 10
+ -- let numberOfMarbles = 1618
+ -- print $ take 5 $ scanl (\c n -> insertAfter n $ stepClockwise c) (createCircle 0) [1..]
+ -- print $ playGame numberOfPlayers numberOfMarbles
+ -- print (let p = 10 ; m = 1618 in part1 p m)
+ -- print (let p = 13 ; m = 7999 in part1 p m)
+ -- print (let p = 17 ; m = 1104 in part1 p m)
+ -- print (let p = 21 ; m = 6111 in part1 p m)
+ -- print (let p = 30 ; m = 5807 in part1 p m)
+ print $ part1 numberOfPlayers numberOfMarbles
+ print $ part1 numberOfPlayers (numberOfMarbles * 100)
+
+
+ -- putStrLn $ part1 schedule
+ -- print $ part2 schedule
+
+part1 players marbles = highScore $ playGame players marbles
+
+playGame :: Integer -> Integer -> Game
+-- playGame players marbles = scanl makeMove createGame $ zip (cycle [1..players]) [1..marbles]
+playGame players marbles = foldl' makeMove createGame $ zip (cycle [1..players]) [1..marbles]
+
+highScore :: Game -> Integer
+highScore (Game _ score) = maximum $ M.elems score
+
+createGame :: Game
+createGame = Game (createCircle 0) M.empty
+
+createCircle :: Integer -> Circle
+createCircle current = Circle Q.empty current Q.empty
+
+currentMarble :: Circle -> Integer
+currentMarble (Circle _ m _) = m
+
+stepClockwise :: Circle -> Circle
+stepClockwise (Circle left current right)
+ | (Q.null left) && (Q.null right) = Circle left current right
+ | (Q.null right) = stepClockwise (Circle Q.empty current left)
+ | otherwise = Circle (left |> current) r rs
+ where (r :< rs) = Q.viewl right
+
+stepAntiClockwise :: Circle -> Circle
+stepAntiClockwise (Circle left current right)
+ | (Q.null left) && (Q.null right) = Circle left current right
+ | (Q.null left) = stepAntiClockwise (Circle right current Q.empty)
+ | otherwise = Circle ls l (current <| right)
+ where (ls :> l) = Q.viewr left
+
+insertAfter :: Integer -> Circle -> Circle
+insertAfter new (Circle left current right) = Circle (left |> current) new right
+
+removeCurrent :: Circle -> Circle
+removeCurrent (Circle left _ right)
+ | Q.null right = Circle ls l Q.empty
+ | otherwise = Circle left r rs
+ where (l :< ls) = Q.viewl left
+ (r :< rs) = Q.viewl right
+
+makeMove :: Game -> (Integer, Integer) -> Game
+makeMove (Game circle score) (player, marble) =
+ if marble `mod` 23 == 0
+ then let circle' = (iterate stepAntiClockwise circle) !! 7
+ score' = updateScore score player (marble + (currentMarble circle'))
+ circle'' = removeCurrent circle'
+ in Game circle'' score'
+ else let circle' = insertAfter marble (stepClockwise circle)
+ in Game circle' score
+
+updateScore :: Score -> Integer -> Integer -> Score
+updateScore score player change = M.insert player (current + change) score
+ where current = M.findWithDefault 0 player score
+
+
+-- Parse the input file
+
+type Parser = Parsec Void Text
+
+sc :: Parser ()
+sc = L.space (skipSome spaceChar) CA.empty CA.empty
+
+lexeme = L.lexeme sc
+integer = lexeme L.decimal
+symb = L.symbol sc
+
+infixP = symb "players; last marble is worth"
+suffixP = symb "points"
+
+
+-- linkP = pairify <$> prefixP <*> upperChar <* infixP <*> upperChar <* suffixP
+-- where pairify _ a b = (a, b)
+gameFileP = (,) <$> integer <* infixP <*> integer <* suffixP
+
+successfulParse :: Text -> (Integer, Integer)
+successfulParse input =
+ case parse gameFileP "input" input of
+ Left _error -> (0, 0) -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right game -> game
\ No newline at end of file
--- /dev/null
+{-# LANGUAGE OverloadedStrings, ViewPatterns, PatternSynonyms #-}
+
+import Data.List
+
+import Data.Foldable (toList)
+
+import Data.Text (Text)
+import qualified Data.Text.IO as TIO
+
+import Data.Void (Void)
+
+import Text.Megaparsec
+import Text.Megaparsec.Char
+import qualified Text.Megaparsec.Char.Lexer as L
+import qualified Control.Applicative as CA
+
+-- import Data.Map.Strict ((!))
+import qualified Data.Map.Strict as M
+
+import qualified Data.Sequence as Q
+import Data.Sequence ((<|), (|>), ViewL((:<)), ViewR((:>)) )
+
+-- zipper of left, current, right
+data Circle = Circle (Q.Seq Integer) Integer (Q.Seq Integer) deriving (Eq)
+type Score = M.Map Integer Integer -- player -> score
+data Game = Game Circle Score deriving (Show, Eq)
+
+instance Show Circle where
+ show (Circle left current right) = (showSide left) ++ " (" ++ (show current) ++ ") " ++ (showSide right)
+ where showSide s = intercalate " " $ map show $ toList s
+
+main :: IO ()
+main = do
+ text <- TIO.readFile "data/advent09.txt"
+ let (numberOfPlayers, numberOfMarbles) = successfulParse text
+ -- let numberOfPlayers = 10
+ -- let numberOfMarbles = 1618
+ -- print $ take 5 $ scanl (\c n -> insertAfter n $ stepClockwise c) (createCircle 0) [1..]
+ -- print $ playGame numberOfPlayers numberOfMarbles
+ -- print (let p = 10 ; m = 1618 in part1 p m)
+ -- print (let p = 13 ; m = 7999 in part1 p m)
+ -- print (let p = 17 ; m = 1104 in part1 p m)
+ -- print (let p = 21 ; m = 6111 in part1 p m)
+ -- print (let p = 30 ; m = 5807 in part1 p m)
+ print $ part1 numberOfPlayers numberOfMarbles
+ print $ part1 numberOfPlayers (numberOfMarbles * 100)
+
+
+ -- putStrLn $ part1 schedule
+ -- print $ part2 schedule
+
+part1 players marbles = highScore $ playGame players marbles
+
+playGame :: Integer -> Integer -> Game
+-- playGame players marbles = scanl makeMove createGame $ zip (cycle [1..players]) [1..marbles]
+playGame players marbles = foldl' makeMove createGame $ zip (cycle [1..players]) [1..marbles]
+
+highScore :: Game -> Integer
+highScore (Game _ score) = maximum $ M.elems score
+
+createGame :: Game
+createGame = Game (createCircle 0) M.empty
+
+createCircle :: Integer -> Circle
+createCircle current = Circle Q.empty current Q.empty
+
+currentMarble :: Circle -> Integer
+currentMarble (Circle _ m _) = m
+
+stepClockwise :: Circle -> Circle
+stepClockwise (Circle left current right)
+ | (Q.null left) && (Q.null right) = Circle left current right
+ | (Q.null right) = stepClockwise (Circle Q.empty current left)
+ | otherwise = Circle (left |> current) r rs
+ where (r :< rs) = Q.viewl right
+
+stepAntiClockwise :: Circle -> Circle
+stepAntiClockwise (Circle left current right)
+ | (Q.null left) && (Q.null right) = Circle left current right
+ | (Q.null left) = stepAntiClockwise (Circle right current Q.empty)
+ | otherwise = Circle ls l (current <| right)
+ where (ls :> l) = Q.viewr left
+
+insertAfter :: Integer -> Circle -> Circle
+insertAfter new (Circle left current right) = Circle (left |> current) new right
+
+removeCurrent :: Circle -> Circle
+removeCurrent (Circle left _ right)
+ | Q.null right = Circle ls l Q.empty
+ | otherwise = Circle left r rs
+ where (l :< ls) = Q.viewl left
+ (r :< rs) = Q.viewl right
+
+makeMove :: Game -> (Integer, Integer) -> Game
+makeMove (Game circle score) (player, marble) =
+ if marble `mod` 23 == 0
+ then let circle' = (iterate stepAntiClockwise circle) !! 7
+ score' = updateScore score player (marble + (currentMarble circle'))
+ circle'' = removeCurrent circle'
+ in Game circle'' score'
+ else let circle' = insertAfter marble (stepClockwise circle)
+ in Game circle' score
+
+updateScore :: Score -> Integer -> Integer -> Score
+updateScore score player change = M.insert player (current + change) score
+ where current = M.findWithDefault 0 player score
+
+
+-- Parse the input file
+
+type Parser = Parsec Void Text
+
+sc :: Parser ()
+sc = L.space (skipSome spaceChar) CA.empty CA.empty
+
+lexeme = L.lexeme sc
+integer = lexeme L.decimal
+symb = L.symbol sc
+
+infixP = symb "players; last marble is worth"
+suffixP = symb "points"
+
+
+-- linkP = pairify <$> prefixP <*> upperChar <* infixP <*> upperChar <* suffixP
+-- where pairify _ a b = (a, b)
+gameFileP = (,) <$> integer <* infixP <*> integer <* suffixP
+
+successfulParse :: Text -> (Integer, Integer)
+successfulParse input =
+ case parse gameFileP "input" input of
+ Left _error -> (0, 0) -- TIO.putStr $ T.pack $ parseErrorPretty err
+ Right game -> game
\ No newline at end of file