|
| 1 | +module Main where |
| 2 | + |
| 3 | +import Data.Bits |
| 4 | +import Data.Char |
| 5 | +import Data.List |
| 6 | +import Data.List.Split |
| 7 | +import Data.List.Unique |
| 8 | +import Data.Matrix (Matrix, (!)) |
| 9 | +import Data.Matrix qualified as Mat |
| 10 | +import Data.Set (Set) |
| 11 | +import Data.Set qualified as S |
| 12 | +import Data.Tuple.Extra |
| 13 | +import Debug.Trace |
| 14 | +import GHC.Integer (shiftRInteger) |
| 15 | +import GHC.Num (integerFromInt, integerToInt) |
| 16 | +import System.Environment |
| 17 | +import Text.Regex.TDFA ((=~)) |
| 18 | + |
| 19 | +-- TODO: Cleanup imports after day done |
| 20 | + |
| 21 | +data Input = Computer {program :: [Integer], programCounter :: Int, registerA :: Integer, registerB :: Integer, registerC :: Integer, output :: [Integer]} deriving (Show, Eq, Ord) |
| 22 | + |
| 23 | +type Output = Integer |
| 24 | + |
| 25 | +parseInput :: String -> Input |
| 26 | +parseInput input = Computer pg 0 regA regB regC [] |
| 27 | + where |
| 28 | + [lRegA, lRegB, lRegC, _, lPg] = lines input |
| 29 | + readVal = read . dropWhile (not . isDigit) |
| 30 | + regA = readVal lRegA |
| 31 | + regB = readVal lRegB |
| 32 | + regC = readVal lRegC |
| 33 | + pg = read . (\l -> "[" ++ l ++ "]") . dropWhile (not . isDigit) $ lPg |
| 34 | + |
| 35 | +getValue :: Input -> Integer -> Integer |
| 36 | +getValue (Computer _ _ rA _ _ _) 4 = rA |
| 37 | +getValue (Computer _ _ _ rB _ _) 5 = rB |
| 38 | +getValue (Computer _ _ _ _ rC _) 6 = rC |
| 39 | +getValue (Computer _ _ _ _ _ _) n = n |
| 40 | + |
| 41 | +calculateOp :: Input -> Integer -> Integer -> Input |
| 42 | +calculateOp (c@(Computer _ pc rA rB rC out)) 0 n = c {programCounter = pc + 2, registerA = rA `div` (2 ^ (getValue c n))} |
| 43 | +calculateOp (c@(Computer _ pc rA rB rC out)) 1 n = c {programCounter = pc + 2, registerB = rB `xor` n} |
| 44 | +calculateOp (c@(Computer _ pc rA rB rC out)) 2 n = c {programCounter = pc + 2, registerB = (getValue c n) `mod` 8} |
| 45 | +calculateOp (c@(Computer _ pc 00 rB rC out)) 3 n = c {programCounter = pc + 2} |
| 46 | +calculateOp (c@(Computer _ pc rA rB rC out)) 3 n = c {programCounter = integerToInt $ getValue c n} |
| 47 | +calculateOp (c@(Computer _ pc rA rB rC out)) 4 n = c {programCounter = pc + 2, registerB = rB `xor` rC} |
| 48 | +calculateOp (c@(Computer _ pc rA rB rC out)) 5 n = c {programCounter = pc + 2, output = (getValue c n) `mod` 8 : out} |
| 49 | +calculateOp (c@(Computer _ pc rA rB rC out)) 6 n = c {programCounter = pc + 2, registerB = rA `div` (2 ^ (getValue c n))} |
| 50 | +calculateOp (c@(Computer _ pc rA rB rC out)) 7 n = c {programCounter = pc + 2, registerC = rA `div` (2 ^ (getValue c n))} |
| 51 | + |
| 52 | +performOps :: Input -> [Output] |
| 53 | +performOps (c@(Computer pg pc _ _ _ out)) |
| 54 | + | pc >= length pg = reverse out |
| 55 | + | otherwise = performOps $ calculateOp c op comboOp |
| 56 | + where |
| 57 | + op = pg !! pc |
| 58 | + comboOp = pg !! (pc + 1) |
| 59 | + |
| 60 | +part1 :: Input -> [Output] |
| 61 | +part1 = performOps |
| 62 | + |
| 63 | +part2 :: Input -> Output |
| 64 | +part2 input = head $ sub' 1 0 |
| 65 | + where |
| 66 | + sub' p v |
| 67 | + | (== p) . length $ program input = currIt |
| 68 | + | otherwise = concat $ map (sub' (p + 1)) currIt |
| 69 | + where |
| 70 | + currIt = filter (\i -> (`isSuffixOf` program input) . performOps $ input {registerA = i}) [8 * v .. 8 * v + 7] |
| 71 | + |
| 72 | +main :: IO () |
| 73 | +main = do |
| 74 | + args <- getArgs |
| 75 | + content <- readFile (last args) |
| 76 | + let input = parseInput content |
| 77 | + print input |
| 78 | + |
| 79 | + print $ part1 input |
| 80 | + print $ part2 input |
0 commit comments