-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from ephemient/hs/day17
Day 17: Chronospatial Computer
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 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 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,68 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
-- | | ||
-- Module: Day17 | ||
-- Description: <https://adventofcode.com/2024/day/17 Day 17: Chronospatial Computer> | ||
module Day17 (part1, part2, run, step) where | ||
|
||
import Data.Bits (shiftR, xor, (.&.)) | ||
import Data.List (isSuffixOf, unfoldr) | ||
import Data.Maybe (catMaybes) | ||
import Data.String (IsString) | ||
import Data.Text (Text) | ||
import Data.Void (Void) | ||
import Text.Megaparsec (MonadParsec, ParseErrorBundle, Stream (Token, Tokens), between, parse, sepBy) | ||
import Text.Megaparsec.Char (char, newline, string) | ||
import Text.Megaparsec.Char.Lexer qualified as L (decimal) | ||
|
||
parser :: (MonadParsec e s m, IsString (Tokens s), Token s ~ Char, Num a) => m ((a, a, a), [a]) | ||
parser = do | ||
a <- between (string "Register A: ") newline L.decimal | ||
b <- between (string "Register B: ") newline L.decimal | ||
c <- between (string "Register C: ") newline L.decimal | ||
newline | ||
program <- between (string "Program: ") newline $ L.decimal `sepBy` char ',' | ||
pure ((a, b, c), program) | ||
|
||
step :: [Int] -> (Int, (Int, Int, Int)) -> Maybe (Maybe Int, (Int, (Int, Int, Int))) | ||
step program (ip, registers@(a, b, c)) | ||
| ip < 0 || ip >= length program = Nothing | ||
| 0 <- instruction = Just (Nothing, (ip + 2, (a `shiftR` combo, b, c))) | ||
| 1 <- instruction = Just (Nothing, (ip + 2, (a, b `xor` operand, c))) | ||
| 2 <- instruction = Just (Nothing, (ip + 2, (a, combo .&. 7, c))) | ||
| 3 <- instruction = Just (Nothing, (if a == 0 then ip + 2 else operand, registers)) | ||
| 4 <- instruction = Just (Nothing, (ip + 2, (a, b `xor` c, c))) | ||
| 5 <- instruction = Just (Just $ combo .&. 7, (ip + 2, registers)) | ||
| 6 <- instruction = Just (Nothing, (ip + 2, (a, a `shiftR` combo, c))) | ||
| 7 <- instruction = Just (Nothing, (ip + 2, (a, b, a `shiftR` combo))) | ||
where | ||
instruction = program !! ip | ||
operand = program !! (ip + 1) | ||
combo | ||
| 0 <= operand && operand <= 3 = operand | ||
| 4 <- operand = a | ||
| 5 <- operand = b | ||
| 6 <- operand = c | ||
|
||
run :: [Int] -> (Int, Int, Int) -> [Int] | ||
run program = catMaybes . unfoldr (step program) . (0,) | ||
|
||
part1 :: Text -> Either (ParseErrorBundle Text Void) [Int] | ||
part1 input = do | ||
(registers, program) <- parse parser "" input | ||
pure $ run program registers | ||
|
||
part2 :: Text -> Either (ParseErrorBundle Text Void) Int | ||
part2 input = do | ||
((_, b, c), program) <- parse parser "" input | ||
let go nums | ||
| (a, _) : _ <- filter ((== program) . snd) next = a | ||
| otherwise = go $ fst <$> next | ||
where | ||
next = | ||
[ (a, output) | ||
| a <- (+) . (8 *) <$> nums <*> [0 .. 7], | ||
let output = run program (a, b, c), | ||
output `isSuffixOf` program | ||
] | ||
pure $ go [0] |
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,40 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Day17Spec (spec) where | ||
|
||
import Data.Text (Text) | ||
import Data.Text qualified as T (unlines) | ||
import Day17 (part1, part2, run, step) | ||
import Test.Hspec (Spec, describe, it, shouldBe) | ||
|
||
example1, example2 :: Text | ||
example1 = | ||
T.unlines | ||
[ "Register A: 729", | ||
"Register B: 0", | ||
"Register C: 0", | ||
"", | ||
"Program: 0,1,5,4,3,0" | ||
] | ||
example2 = | ||
T.unlines | ||
[ "Register A: 2024", | ||
"Register B: 0", | ||
"Register C: 0", | ||
"", | ||
"Program: 0,3,5,4,3,0" | ||
] | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "part 1" $ do | ||
it "examples" $ do | ||
step [2, 6] (0, (-1, -1, 9)) `shouldBe` Just (Nothing, (2, (-1, 1, 9))) | ||
run [5, 0, 5, 1, 5, 4] (10, -1, -1) `shouldBe` [0, 1, 2] | ||
run [0, 1, 5, 4, 3, 0] (2024, -1, -1) `shouldBe` [4, 2, 5, 6, 7, 7, 7, 7, 3, 1, 0] | ||
step [1, 7] (0, (-1, 29, -1)) `shouldBe` Just (Nothing, (2, (-1, 26, -1))) | ||
step [4, 0] (0, (-1, 2024, 43690)) `shouldBe` Just (Nothing, (2, (-1, 44354, 43690))) | ||
part1 example1 `shouldBe` Right [4, 6, 3, 5, 6, 3, 5, 2, 1, 0] | ||
describe "part 2" $ do | ||
it "examples" $ do | ||
part2 example2 `shouldBe` Right 117440 |