Skip to content

Commit

Permalink
examples
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy committed Dec 17, 2024
1 parent 31610cb commit 1e9e4ac
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions solutions/src/2024/17.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@ Maintainer : [email protected]
<https://adventofcode.com/2024/day/17>
>>> :{
:main + "Register A: 729
Register B: 0
Register C: 0
\&
Program: 0,1,5,4,3,0
"
:}
4,6,3,5,6,3,5,2,1,0
no part 2
>>> :{
:main + "Register A: 2024
Register B: 0
Register C: 0
\&
Program: 0,3,5,4,3,0
"
:}
5,7,3,0
117440
-}
module Main (main) where

import Advent (format)
import Data.List (intercalate)
import Data.List (intercalate, isSuffixOf)
import Data.SBV
(Word64, SWord64, SBool,
(.==), (.&.), (.&&), sShiftRight, shiftR, xor,
Expand All @@ -30,14 +52,19 @@ main =
%n
Program: %u&,%n|]

-- Run with given input register A
putStrLn (intercalate "," (map show (run (Machine a1 b c) program)))

-- Search specific to the kinds of problems we get
--print (search program b c)

-- Search using Z3
res <- optLexicographic \a2 ->
do minimize "smallest a" a2
constrain (sRun (SMachine program a2 (fromIntegral b) (fromIntegral c)) program)
case getModelValue "smallest a" res of
Just x -> print (x :: Word64)
Nothing -> fail "no solution"
Nothing -> putStrLn "no part 2"

data Machine = Machine { rA, rB, rC :: !Int }

Expand Down Expand Up @@ -83,3 +110,12 @@ sRun m0 pgm = go m0 pgm
0 -> 0; 1 -> 1; 2 -> 2; 3 -> 3
4 -> sA m; 5 -> sB m; 6 -> sC m
_ -> error "invalid combo operand"

search :: [Int] -> Int -> Int -> Int
search pgm b c = head (go =<< [0 .. 7])
where
go a =
case run (Machine a 0 0) pgm of
out | out == pgm -> [a]
| out `isSuffixOf` pgm -> go =<< take 8 [8 * a ..]
| otherwise -> []

0 comments on commit 1e9e4ac

Please sign in to comment.