-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
122 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{-# Language LambdaCase #-} | ||
{-| | ||
Module : Advent.AsmProg | ||
Description : Support module for 2016 | ||
Copyright : (c) Eric Mertens, 2024 | ||
License : ISC | ||
Maintainer : [email protected] | ||
This module supports a set of 3 tasks from the 2016 problem set that | ||
used a 4 register machine state. | ||
-} | ||
module Advent.AsmProg where | ||
|
||
import Advent.ReadS (P(..)) | ||
import Control.Applicative ( Alternative(empty, (<|>)) ) | ||
import Control.Lens (use) | ||
import Control.Monad.State ( MonadState ) | ||
|
||
data Registers = Registers { _regA, _regB, _regC, _regD :: !Int } | ||
deriving (Read, Show, Eq, Ord) | ||
|
||
regA :: Functor f => (Int -> f Int) -> Registers -> f Registers | ||
regA f r = (\x -> r { _regA = x}) <$> f (_regA r) | ||
|
||
regB :: Functor f => (Int -> f Int) -> Registers -> f Registers | ||
regB f r = (\x -> r { _regB = x}) <$> f (_regB r) | ||
|
||
regC :: Functor f => (Int -> f Int) -> Registers -> f Registers | ||
regC f r = (\x -> r { _regC = x}) <$> f (_regC r) | ||
|
||
regD :: Functor f => (Int -> f Int) -> Registers -> f Registers | ||
regD f r = (\x -> r { _regD = x}) <$> f (_regD r) | ||
|
||
zeroRegisters :: Registers | ||
zeroRegisters = Registers 0 0 0 0 | ||
|
||
class HasRegisters a where | ||
reg :: Functor f => Register -> (Int -> f Int) -> a -> f a | ||
|
||
data Register = A | B | C | D | ||
deriving (Show, Eq, Ord) | ||
|
||
instance HasRegisters Registers where | ||
reg A = regA | ||
reg B = regB | ||
reg C = regC | ||
reg D = regD | ||
{-# INLINE reg #-} | ||
|
||
data Value = Int !Int | Reg !Register | ||
deriving Show | ||
|
||
rval :: (MonadState r m, HasRegisters r) => Value -> m Int | ||
rval = \case | ||
Int i -> pure i | ||
Reg r -> use (reg r) | ||
{-# INLINE rval #-} | ||
|
||
pValue :: P Value | ||
pValue = Int <$> P reads <|> Reg <$> pReg | ||
|
||
pReg :: P Register | ||
pReg = P lex >>= \case | ||
"a" -> pure A | ||
"b" -> pure B | ||
"c" -> pure C | ||
"d" -> pure D | ||
_ -> empty |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{-# Language QuasiQuotes, TemplateHaskell, BlockArguments, LambdaCase, ImportQualifiedPost #-} | ||
{-# OPTIONS_GHC -w #-} | ||
{-| | ||
Module : Main | ||
Description : Day 13 solution | ||
Copyright : (c) Eric Mertens, 2024 | ||
License : ISC | ||
Maintainer : [email protected] | ||
<https://adventofcode.com/2024/day/13> | ||
-} | ||
module Main where | ||
|
||
import Data.Map (Map) | ||
import Data.Map qualified as Map | ||
import Data.Set (Set) | ||
import Data.Set qualified as Set | ||
import Data.List | ||
import Data.Maybe | ||
import Data.Array.Unboxed | ||
|
||
import Advent | ||
import Advent.Coord | ||
import Advent.Search | ||
import System.Environment | ||
|
||
main :: IO () | ||
main = | ||
-- withArgs ["/Users/emertens/Source/advent/example.txt"] | ||
do | ||
input <- [format|2024 13 (%s%n)*|] | ||
print input |