-
Notifications
You must be signed in to change notification settings - Fork 256
/
bf.cs
185 lines (162 loc) · 4.74 KB
/
bf.cs
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
namespace Test
{
enum OpT { INC, MOVE, PRINT, LOOP };
struct Op
{
public OpT op;
public int v;
public Op[] loop;
public Op(OpT _op, int _v)
{
op = _op;
v = _v;
loop = null;
}
public Op(OpT _op, Op[] _l)
{
op = _op;
loop = _l;
v = 0;
}
}
public class Tape
{
int pos = 0;
int[] tape = new int[1];
public int CurrentCell
{
get => tape[pos];
set => tape[pos] = value;
}
public void Inc(int x) => CurrentCell += x;
public void Move(int x)
{
pos += x;
if (pos >= tape.Length)
Array.Resize(ref tape, tape.Length * 2);
}
}
class Printer
{
int sum1 = 0;
int sum2 = 0;
public int Checksum
{
get => (sum2 << 8) | sum1;
}
public bool Quiet { get; set; }
public void Print(int n)
{
if (Quiet)
{
sum1 = (sum1 + n) % 255;
sum2 = (sum2 + sum1) % 255;
}
else
{
Console.Write((char)n);
}
}
}
class Program
{
Op[] ops;
Printer p;
Program(string text, Printer p)
{
ops = parse(text.GetEnumerator());
this.p = p;
}
private Op[] parse(IEnumerator<char> it)
{
List<Op> res = new List<Op>();
while (it.MoveNext())
{
switch (it.Current)
{
case '+': res.Add(new Op(OpT.INC, 1)); break;
case '-': res.Add(new Op(OpT.INC, -1)); break;
case '>': res.Add(new Op(OpT.MOVE, 1)); break;
case '<': res.Add(new Op(OpT.MOVE, -1)); break;
case '.': res.Add(new Op(OpT.PRINT, 0)); break;
case '[': res.Add(new Op(OpT.LOOP, parse(it))); break;
case ']': return res.ToArray();
}
}
return res.ToArray();
}
public void run() => _run(ops, new Tape());
private void _run(Op[] program, Tape tape)
{
foreach (Op op in program)
{
switch (op.op)
{
case OpT.INC: tape.Inc(op.v); break;
case OpT.MOVE: tape.Move(op.v); break;
case OpT.LOOP: while (tape.CurrentCell > 0) _run(op.loop, tape); break;
case OpT.PRINT: p.Print(tape.CurrentCell); break;
}
}
}
private static void Notify(string msg)
{
try
{
using (var s = new System.Net.Sockets.TcpClient("localhost", 9001))
{
var data = System.Text.Encoding.UTF8.GetBytes(msg);
s.Client.Send(data);
}
}
catch
{
// standalone usage
}
}
private static void Verify()
{
var text = @"++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>
---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.";
var p_left = new Printer { Quiet = true };
new Program(text, p_left).run();
var left = p_left.Checksum;
var p_right = new Printer { Quiet = true };
foreach (var c in "Hello World!\n")
{
p_right.Print(c);
}
var right = p_right.Checksum;
if (left != right)
{
Console.Error.WriteLine($"{left} != {right}");
System.Environment.Exit(1);
}
}
static void Main(string[] args)
{
Verify();
var text = File.ReadAllText(args[0]);
var p = new Printer
{
Quiet = Environment.GetEnvironmentVariable("QUIET") != null
};
var runtime = Type.GetType("Mono.Runtime") != null ? "Mono" : ".NET Core";
Notify($"C#/{runtime}\t{Process.GetCurrentProcess().Id}");
var stopWatch = Stopwatch.StartNew();
new Program(text, p).run();
stopWatch.Stop();
var elapsed = stopWatch.ElapsedMilliseconds / 1e3;
Notify("stop");
Console.Error.WriteLine($"time: {elapsed}s");
if (p.Quiet)
{
Console.WriteLine($"Output checksum: {p.Checksum}");
}
}
}
}