|
1 |
| -|| solved by hand by examining the supplied program, and determining |
2 |
| -|| the constraints on the digits |
3 |
| -|| |
4 |
| -|| code below was used to run various inputs on the program |
| 1 | +|| day24 -- Arithmetic Logic Unit |
5 | 2 |
|
6 |
| -%export day24 |
7 | 3 |
|
8 |
| -%import <io> |
| 4 | +%import <io> (>>=)/io_bind |
| 5 | +%import <map> |
| 6 | +%import <maybe> |
| 7 | +%import <mirandaExtensions> |
| 8 | +%import <vector> |
9 | 9 |
|
10 | 10 |
|
11 |
| -|| solved by hand examination of binary |
| 11 | +|| instruction operand is reg w, x, y, z or literal int |
| 12 | +op ::= W | X | Y | Z | I int |
12 | 13 |
|
13 |
| -day24 :: io () |
14 |
| -day24 |
15 |
| - = io_mapM_ putStrLn [part1, part2] |
| 14 | +parseOp :: string -> op |
| 15 | +parseOp "w" = W |
| 16 | +parseOp "x" = X |
| 17 | +parseOp "y" = Y |
| 18 | +parseOp "z" = Z |
| 19 | +parseOp s = I (intval s) |
| 20 | + |
| 21 | +insn ::= |
| 22 | + Inp op | |
| 23 | + Add op op | |
| 24 | + Mul op op | |
| 25 | + Div op op | |
| 26 | + Mod op op | |
| 27 | + Eql op op |
| 28 | + |
| 29 | +isInp :: insn -> bool |
| 30 | +isInp (Inp _) = True |
| 31 | +isInp _ = False |
| 32 | + |
| 33 | +|| extract integer literal operand 2 from insn |
| 34 | +viewOp2Lit :: insn -> maybe int |
| 35 | +viewOp2Lit (Add _ (I n)) = Just n |
| 36 | +viewOp2Lit (Mul _ (I n)) = Just n |
| 37 | +viewOp2Lit (Div _ (I n)) = Just n |
| 38 | +viewOp2Lit (Mod _ (I n)) = Just n |
| 39 | +viewOp2Lit (Eql _ (I n)) = Just n |
| 40 | +viewOp2Lit _ = Nothing |
| 41 | + |
| 42 | +parseInsn :: string -> insn |
| 43 | +parseInsn = words .> go |
| 44 | + where |
| 45 | + go ["inp", s] = Inp (parseOp s) |
| 46 | + go ["add", sa, sb] = Add (parseOp sa) (parseOp sb) |
| 47 | + go ["mul", sa, sb] = Mul (parseOp sa) (parseOp sb) |
| 48 | + go ["div", sa, sb] = Div (parseOp sa) (parseOp sb) |
| 49 | + go ["mod", sa, sb] = Mod (parseOp sa) (parseOp sb) |
| 50 | + go ["eql", sa, sb] = Eql (parseOp sa) (parseOp sb) |
| 51 | + go _ = undef |
| 52 | + |
| 53 | +blockInfo == m_map int [insn] |
| 54 | + |
| 55 | +|| each block starts with an Inp insn; collect a list of insns at each |
| 56 | +|| insn index of a block |
| 57 | +genBlocks :: [insn] -> blockInfo |
| 58 | +genBlocks |
| 59 | + = foldl go (0, m_empty) .> snd |
| 60 | + where |
| 61 | + go (i, m) ins |
| 62 | + = (i' + 1, m_insertWith cmpint (converse (++)) i' [ins] m) |
| 63 | + where |
| 64 | + i' = if' (isInp ins) 0 i |
| 65 | + |
| 66 | +|| extract the varying op2 int components from the insns at index i in the blocks |
| 67 | +extractVaryings :: blockInfo -> int -> [int] |
| 68 | +extractVaryings m i |
| 69 | + = m_findWithDefault cmpint [] i m |> map viewOp2Lit |> catMaybes |
| 70 | + |
| 71 | +|| The value of z through the 14 blocks forms a LIFO stack of base-26 numbers, |
| 72 | +|| which is pushed to when the DIV z I immediate is 1, and popped from when it |
| 73 | +|| is 26 |
| 74 | +solve :: bool -> [int] -> [int] -> [int] -> int |
| 75 | +solve part1 xs ys zs |
| 76 | + = go [] v vs |
16 | 77 | where
|
17 |
| - part1 = "part 1: " ++ "99893999291967" |
18 |
| - part2 = "part 2: " ++ "34171911181211" |
| 78 | + v = v_rep (#xs) undef |
| 79 | + vs = enumerate $ zip3 xs ys zs |
| 80 | + |
| 81 | + addDigit n d = n * 10 + d |
| 82 | + |
| 83 | + go stk vr ((i, (x, y, z)) : vs) |
| 84 | + = go ((i, y) : stk) vr vs, if z == 1 |
| 85 | + = go stk' vr1 vs, if part1 |
| 86 | + = go stk' vr2 vs, otherwise |
| 87 | + where |
| 88 | + (j, o) : stk' = stk |
| 89 | + |
| 90 | + delta = x + o |
| 91 | + |
| 92 | + vr1 = vr // [(j, 9), (i, 9 + delta)], if delta < 0 |
| 93 | + = vr // [(j, 9 - delta), (i, 9)], otherwise |
| 94 | + |
| 95 | + vr2 = vr // [(j, 1), (i, 1 + delta)], if delta > 0 |
| 96 | + = vr // [(j, 1 - delta), (i, 1)], otherwise |
| 97 | + |
| 98 | + go _ vr _ = v_toList vr |> foldl addDigit 0 |
| 99 | + |
| 100 | +readInsns :: string -> io [insn] |
| 101 | +readInsns fn = readFile fn >>= lines .> map parseInsn .> io_pure |
| 102 | + |
| 103 | +main :: io () |
| 104 | +main = readInsns "../inputs/day24.txt" >>= genBlocks .> go |
| 105 | + where |
| 106 | + go blocks |
| 107 | + = io_mapM_ putStrLn [part1, part2] |
| 108 | + where |
| 109 | + xs = extractVaryings blocks 5 |
| 110 | + ys = extractVaryings blocks 15 |
| 111 | + zs = extractVaryings blocks 4 |
| 112 | + part1 = (++) "part 1: " . showint $ solve True xs ys zs |
| 113 | + part2 = (++) "part 2: " . showint $ solve False xs ys zs |
0 commit comments