-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday17_part01.fs
117 lines (105 loc) · 3.19 KB
/
day17_part01.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
module day17_part01
open AdventOfCode_2024.Modules
open System.Collections.Generic
type OpCode =
| Adv
| Bxl
| Bst
| Jnz
| Bxc
| Out
| Bdv
| Cdv
type Register = {
Name: string
Value: int
}
let opType(v: int) =
match v with
| 0 -> Adv
| 1 -> Bxl
| 2 -> Bst
| 3 -> Jnz
| 4 -> Bxc
| 5 -> Out
| 6 -> Bdv
| 7 -> Cdv
| _ -> failwith "error"
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,int>()
let r' =
partregisters.Split("\r\n")
|> Array.iter(fun r ->
let (n, v) = ((r.Split(" ")[1]).Replace(":",""),
(int)(r.Split(" ")[2]))
registers.Add(n, v)
)
let ops =
(partprogram.Split(" ")[1]).Split(",")
|> Array.map int |> Array.map opType
(registers, ops)
let comboOperand(op: OpCode) (registers: Dictionary<string,int>) =
match op with
| Adv -> 0
| Bxl -> 1
| Bst -> 2
| Jnz -> 3
| 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,int>) (pIdx: int)=
let mutable pointerIdx = pIdx + 2
let mutable output = -1
if op.IsAdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (comboOperand opOp registers))
registers["A"] <- (int)(numerator / denominator)
elif op.IsBxl then
registers["B"] <- registers["B"] ^^^ (literalOperand opOp)
elif op.IsBst then
let numerator = comboOperand opOp registers
registers["B"] <- numerator % 8
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) % 8
elif op.IsBdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (comboOperand opOp registers))
registers["B"] <- (int)(numerator / denominator)
elif op.IsCdv then
let numerator = (float)(registers["A"])
let denominator = System.Math.Pow(2, (comboOperand opOp registers))
registers["C"] <- (int)(numerator / denominator)
(pointerIdx, output)
let runProgram(ops: OpCode array)(registers: Dictionary<string,int>) =
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 |> List.map string
let execute() =
let path = "day17/day17_input.txt"
let content = LocalHelper.GetContentFromFile path
let (registers, ops) = parseContent content
let result = runProgram ops registers
String.concat "," result