-
Notifications
You must be signed in to change notification settings - Fork 0
/
day17_part02.fs
168 lines (154 loc) · 5.14 KB
/
day17_part02.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
module day17_part02
open AdventOfCode_2024.Modules
open System.Collections.Generic
type OpCode =
| Adv
| Bxl
| Bst
| Jnz
| Bxc
| Out
| Bdv
| Cdv
type Register = {
Name: string
Value: int64
}
let opType(v: int64) =
match v with
| 0L -> Adv
| 1L -> Bxl
| 2L -> Bst
| 3L -> Jnz
| 4L -> Bxc
| 5L -> Out
| 6L -> Bdv
| 7L -> Cdv
| _ -> failwith "error"
let revOpType(v: OpCode) =
match v with
| Adv -> 0L
| Bxl -> 1L
| Bst -> 2L
| Jnz -> 3L
| Bxc -> 4L
| Out -> 5L
| Bdv -> 6L
| Cdv -> 7L
let parseContent(lines: string) =
let partregisters = lines.Split("\r\n\r\n")[0]
let partprogram = lines.Split("\r\n\r\n")[1]
let registers = Dictionary<string,int64>()
let r' =
partregisters.Split("\r\n")
|> Array.iter(fun r ->
let (n, v) = ((r.Split(" ")[1]).Replace(":",""),
(int64)(r.Split(" ")[2]))
registers.Add(n, v)
)
let ops =
(partprogram.Split(" ")[1]).Split(",")
|> Array.map int64 |> Array.map opType
(registers, ops)
let comboOperand(op: OpCode) (registers: Dictionary<string,int64>) =
match op with
| Adv -> 0L
| Bxl -> 1L
| Bst -> 2L
| Jnz -> 3L
| Bxc -> registers["A"]
| Out -> registers["B"]
| Bdv -> registers["C"]
| Cdv -> failwith "error"
let literalOperand(op: OpCode) =
match op with
| Adv -> 0
| Bxl -> 1
| Bst -> 2
| Jnz -> 3
| Bxc -> 4
| Out -> 5
| Bdv -> 6
| Cdv -> 7
let performOp(op: OpCode) (opOp: OpCode) (registers: Dictionary<string,int64>) (pIdx: int)=
let mutable pointerIdx = pIdx + 2
let mutable output = -1L
if op.IsAdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (float)(comboOperand opOp registers))
registers["A"] <- (int64)(numerator / denominator)
elif op.IsBxl then
registers["B"] <- registers["B"] ^^^ (literalOperand opOp)
elif op.IsBst then
let numerator = comboOperand opOp registers
registers["B"] <- numerator % 8L
elif op.IsJnz then
if registers["A"] <> 0 then
pointerIdx <- literalOperand opOp
elif op.IsBxc then
registers["B"] <- registers["B"] ^^^ registers["C"]
elif op.IsOut then
output <- (comboOperand opOp registers) % 8L
elif op.IsBdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (float)(comboOperand opOp registers))
registers["B"] <- (int64)(numerator / denominator)
elif op.IsCdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (float)(comboOperand opOp registers))
registers["C"] <- (int64)(numerator / denominator)
(pointerIdx, output)
let runProgram(ops: OpCode array)(registers: Dictionary<string,int64>) =
let mutable pIdx = 0
let mutable outputValues = []
while pIdx < ops.Length do
let (newPidx, output) = performOp ops[pIdx] ops[pIdx+1] registers pIdx
pIdx <- newPidx
if output <> -1 then
outputValues <- outputValues @ [output]
outputValues
let findNewRegister(ops: OpCode array) (registers: Dictionary<string,int64>) =
let reversedOps = ops |> Array.rev
let reversedOpsIds = reversedOps |> Array.map revOpType
let checkStack = new Stack<int64*int>()
checkStack.Push((0L,0))
let mutable currentsolution = System.Int64.MaxValue
while checkStack.Count > 0 do
let (pIdx, mindex) = checkStack.Pop()
for bitIdx in 0L..8L do
let newRegisters = Dictionary(registers)
newRegisters["A"] <- bitIdx + pIdx
let output = runProgram ops newRegisters
if output[0] = reversedOpsIds[mindex] then
if mindex+1 >= ops.Length then
if bitIdx + pIdx < currentsolution then
currentsolution <- bitIdx + pIdx
else
checkStack.Push((8L*(pIdx+bitIdx), mindex+1))
currentsolution
// alternative solution with decomposition of the problem
let findA (output: int64 list) =
let rec generateA (output: int64 list) =
let mutable registerB = 0L
seq {
if output.Length = 0 then
yield 0L
else
for currentOp in generateA output.Tail do // iterate in reverse order
for bitPos in 0L..7L do
let registerA = currentOp * 8L + bitPos
registerB <- registerA % 8L
registerB <- registerB ^^^ 3L
let registerC = registerA >>> int registerB
registerB <- registerB ^^^ registerC
registerB <- registerB ^^^ 5L
if output[0] = registerB % 8L then
yield registerA
}
generateA output |> Seq.min
let execute() =
let path = "day17/day17_input.txt"
let content = LocalHelper.GetContentFromFile path
let (registers, ops) = parseContent content
let registerA = findNewRegister ops registers
registerA