|
1 | | -{-# OPTIONS_GHC -Wno-orphans #-} |
2 | | -{-# OPTIONS_GHC -Wno-unused-imports #-} |
3 | | -{-# OPTIONS_GHC -Wno-unused-top-binds #-} |
4 | | - |
5 | 1 | -- | |
6 | 2 | -- Module : AOC2024.Day17 |
7 | 3 | -- License : BSD3 |
@@ -114,65 +110,79 @@ instrParser i = |
114 | 110 | , fmap (BDV . comboParser) . strengthen |
115 | 111 | , fmap (CDV . comboParser) . strengthen |
116 | 112 | ) |
117 | | - `SV.index` fromIntegral i |
| 113 | + `SV.index` i |
118 | 114 |
|
119 | 115 | parseProgram :: [Int] -> Maybe (SV.Vector 8 Instr) |
120 | 116 | parseProgram xs = do |
121 | 117 | xsVec <- SV.fromList @16 =<< traverse (packFinite . fromIntegral) xs |
122 | 118 | SV.generateM \i -> |
123 | 119 | instrParser (xsVec `SV.index` combineProduct (0, i)) (xsVec `SV.index` combineProduct (1, i)) |
124 | 120 |
|
125 | | -readComboV3 :: Combo -> V3 Word -> Word |
126 | | -readComboV3 = \case |
| 121 | +readCombo :: Combo -> V3 Word -> Word |
| 122 | +readCombo = \case |
127 | 123 | CLiteral l -> \_ -> fromIntegral l |
128 | 124 | CReg r -> view (SV.fromTuple (_x, _y, _z) `SV.index` r) |
129 | 125 |
|
130 | 126 | stepProg :: SV.Vector 8 Instr -> V3 Word -> [Finite 8] |
131 | | -stepProg tp = go' 0 |
| 127 | +stepProg tp (V3 a0 b0 c0) = go' 0 a0 b0 c0 |
132 | 128 | where |
133 | | - go' :: Finite 8 -> V3 Word -> [Finite 8] |
134 | | - go' i v@(V3 a b c) = case tp `SV.index` fromIntegral i of |
135 | | - ADV r -> withStep $ V3 (a `div` (2 ^ combo r)) b c |
136 | | - BXL l -> withStep $ V3 a (b `xor` fromIntegral l) c |
137 | | - BST r -> withStep $ V3 a (combo r `mod` 8) c |
| 129 | + go' :: Finite 8 -> Word -> Word -> Word -> [Finite 8] |
| 130 | + go' i !a !b !c = case tp `SV.index` i of |
| 131 | + ADV r -> withStep (a `div` (2 ^ combo r)) b c |
| 132 | + BXL l -> withStep a (b `xor` fromIntegral l) c |
| 133 | + BST r -> withStep a (combo r `mod` 8) c |
138 | 134 | JNZ l |
139 | | - | a == 0 -> withStep v |
140 | | - | otherwise -> go' (weakenN l) v |
141 | | - BXC -> withStep $ V3 a (b `xor` c) c |
142 | | - OUT r -> modulo (fromIntegral (combo r)) : withStep v |
143 | | - BDV r -> withStep $ V3 a (a `div` (2 ^ combo r)) c |
144 | | - CDV r -> withStep $ V3 a b (a `div` (2 ^ combo r)) |
| 135 | + | a == 0 -> withStep 0 b c |
| 136 | + | otherwise -> go' (weakenN l) a b c |
| 137 | + BXC -> withStep a (b `xor` c) c |
| 138 | + OUT r -> modulo (fromIntegral (combo r)) : withStep a b c |
| 139 | + BDV r -> withStep a (a `div` (2 ^ combo r)) c |
| 140 | + CDV r -> withStep a b (a `div` (2 ^ combo r)) |
145 | 141 | where |
146 | | - combo = flip readComboV3 v |
| 142 | + combo = \case |
| 143 | + CLiteral l -> fromIntegral l |
| 144 | + CReg 0 -> a |
| 145 | + CReg 1 -> b |
| 146 | + CReg _ -> c |
147 | 147 | withStep |
148 | | - | i == maxBound = const [] |
149 | | - | otherwise = go' (succ i) |
| 148 | + | i == maxBound = \_ _ _ -> [] |
| 149 | + | otherwise = go' (i + 1) |
150 | 150 |
|
151 | | -go :: Int -> V3 Int -> Seq Int -> [Int] |
152 | | --- go i (V3 a b c) tp = case (,) <$> Seq.lookup i tp <*> Seq.lookup (i + 1) tp of |
153 | | -go i (V3 a b c) tp = case (,) <$> Seq.lookup i tp <*> Seq.lookup (i + 1) tp of |
154 | | - Nothing -> [] |
155 | | - Just (q, o) -> |
156 | | - let x = case o of |
157 | | - 0 -> 0 |
158 | | - 1 -> 1 |
159 | | - 2 -> 2 |
160 | | - 3 -> 3 |
161 | | - 4 -> a |
162 | | - 5 -> b |
163 | | - 6 -> c |
164 | | - in case q of |
165 | | - 0 -> go (i + 2) (V3 (a `div` (2 ^ x)) b c) tp |
166 | | - 1 -> go (i + 2) (V3 a (b `xor` o) c) tp |
167 | | - 2 -> go (i + 2) (V3 a (x `mod` 8) c) tp |
168 | | - 3 |
169 | | - | a == 0 -> go (i + 2) (V3 a b c) tp |
170 | | - | otherwise -> go o (V3 a b c) tp |
171 | | - 4 -> go (i + 2) (V3 a (b `xor` c) c) tp |
172 | | - 5 -> (x `mod` 8) : go (i + 2) (V3 a b c) tp |
173 | | - -- 5 -> trace (show (x `mod` 8, o, x)) (x `mod` 8) : go (i + 2) (V3 a b c) tp |
174 | | - 6 -> go (i + 2) (V3 a (a `div` (2 ^ x)) c) tp |
175 | | - 7 -> go (i + 2) (V3 a b (a `div` (2 ^ x))) tp |
| 151 | +-- | Assumes that: |
| 152 | +-- |
| 153 | +-- 1. Only A is persistent across each "loop" |
| 154 | +-- 2. The last instruction is a jump to 0 |
| 155 | +unstepProg :: SV.Vector 8 Instr -> [Finite 8] -> [Int] |
| 156 | +unstepProg prog = unLoop jnzIx 0 Nothing Nothing |
| 157 | + where |
| 158 | + jnzIx :: Finite 8 |
| 159 | + jnzIx = maxBound |
| 160 | + unLoop :: Finite 8 -> Word -> Maybe Word -> Maybe Word -> [Finite 8] -> [Int] |
| 161 | + unLoop i a b c = case prog `SV.index` i of |
| 162 | + ADV r -> _ (combo r) |
| 163 | + -- JNZ l |
| 164 | + -- | a == 0 -> unLoop |
| 165 | + -- | otherwise -> undefined |
| 166 | + where |
| 167 | + combo = \case |
| 168 | + CLiteral l -> [fromIntegral l] |
| 169 | + CReg 0 -> pure a |
| 170 | + CReg 1 -> maybeToList b -- hmm could really be anything |
| 171 | + CReg _ -> maybeToList c -- hmm could really be anything |
| 172 | + withStep |
| 173 | + | i == minBound = undefined |
| 174 | + | otherwise = unLoop (pred i) |
| 175 | + |
| 176 | + -- search 0 |
| 177 | + -- where |
| 178 | + -- search a = \case |
| 179 | + -- [] -> pure a |
| 180 | + -- o : os -> do |
| 181 | + -- a' <- ((a `shift` 3) +) <$> [0 .. 7] |
| 182 | + -- let b0 = (a' .&. 7) `xor` 6 |
| 183 | + -- let c = a' `shift` (-b0) |
| 184 | + -- guard $ modulo (fromIntegral $ (b0 `xor` c) `xor` 4) == o |
| 185 | + -- search a' os |
176 | 186 |
|
177 | 187 | -- 2,4, 1,6, 7,5, 4,6, 1,4, 5,5, 0,3, 3,0 |
178 | 188 | -- |
@@ -253,6 +263,33 @@ day17b = |
253 | 263 | pure option |
254 | 264 | } |
255 | 265 |
|
| 266 | +go :: Int -> V3 Int -> Seq Int -> [Int] |
| 267 | +-- go i (V3 a b c) tp = case (,) <$> Seq.lookup i tp <*> Seq.lookup (i + 1) tp of |
| 268 | +go i (V3 a b c) tp = case (,) <$> Seq.lookup i tp <*> Seq.lookup (i + 1) tp of |
| 269 | + Nothing -> [] |
| 270 | + Just (q, o) -> |
| 271 | + let x = case o of |
| 272 | + 0 -> 0 |
| 273 | + 1 -> 1 |
| 274 | + 2 -> 2 |
| 275 | + 3 -> 3 |
| 276 | + 4 -> a |
| 277 | + 5 -> b |
| 278 | + 6 -> c |
| 279 | + in case q of |
| 280 | + 0 -> go (i + 2) (V3 (a `div` (2 ^ x)) b c) tp |
| 281 | + 1 -> go (i + 2) (V3 a (b `xor` o) c) tp |
| 282 | + 2 -> go (i + 2) (V3 a (x `mod` 8) c) tp |
| 283 | + 3 |
| 284 | + | a == 0 -> go (i + 2) (V3 a b c) tp |
| 285 | + | otherwise -> go o (V3 a b c) tp |
| 286 | + 4 -> go (i + 2) (V3 a (b `xor` c) c) tp |
| 287 | + 5 -> (x `mod` 8) : go (i + 2) (V3 a b c) tp |
| 288 | + -- 5 -> trace (show (x `mod` 8, o, x)) (x `mod` 8) : go (i + 2) (V3 a b c) tp |
| 289 | + 6 -> go (i + 2) (V3 a (a `div` (2 ^ x)) c) tp |
| 290 | + 7 -> go (i + 2) (V3 a b (a `div` (2 ^ x))) tp |
| 291 | + |
| 292 | + |
256 | 293 | -- [ (go 0 (V3 i b c) (Seq.fromList p)) |
257 | 294 | -- -- | i <- [45184372088832] |
258 | 295 | -- \| i <- [1999] |
|
0 commit comments