|
|
@@ -0,0 +1,131 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace D16._1
|
|
|
+{
|
|
|
+ public class Computer
|
|
|
+ {
|
|
|
+ public ProcessUnit CPU { get; private set; }
|
|
|
+
|
|
|
+ public Computer() => CPU = new ProcessUnit();
|
|
|
+
|
|
|
+ public class ProcessUnit
|
|
|
+ {
|
|
|
+ public delegate void OpCode(ushort a, ushort b, ushort c);
|
|
|
+
|
|
|
+ public Register[] Registers = new Register[4] { 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ public OpCode[] OpCodes;
|
|
|
+
|
|
|
+ public void SetRegisters(ushort[] registers)
|
|
|
+ {
|
|
|
+ for (var i = 0; i < Registers.Length; ++i)
|
|
|
+ Registers[i] = registers[i];
|
|
|
+ }
|
|
|
+
|
|
|
+ public ProcessUnit()
|
|
|
+ {
|
|
|
+ OpCodes = new OpCode[]
|
|
|
+ {
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] + Registers[b]; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] + b; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] * Registers[b]; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] * b; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] & Registers[b]; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] & b; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] | Registers[b]; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] | b; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a]; },
|
|
|
+ (a, b, c) => { Registers[c] = a; },
|
|
|
+ (a, b, c) => { Registers[c] = a > Registers[b] ? 1 : 0; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] > b ? 1 : 0; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] > Registers[b] ? 1 : 0; },
|
|
|
+ (a, b, c) => { Registers[c] = a == Registers[b] ? 1 : 0; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] == b ? 1 : 0; },
|
|
|
+ (a, b, c) => { Registers[c] = Registers[a] == Registers[b] ? 1 : 0; },
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ public class Register
|
|
|
+ {
|
|
|
+ ushort Value { get; set; }
|
|
|
+
|
|
|
+ public static implicit operator Register(ushort a) => new Register { Value = a };
|
|
|
+ public static implicit operator ushort(Register a) => a.Value;
|
|
|
+ public static implicit operator Register(int a) => new Register { Value = (ushort)a };
|
|
|
+ public static bool operator >(Register a, Register b) => a.Value > b.Value;
|
|
|
+ public static bool operator <(Register a, Register b) => a.Value < b.Value;
|
|
|
+ public static bool operator ==(Register a, Register b) => a.Value == b.Value;
|
|
|
+ public static bool operator !=(Register a, Register b) => a.Value != b.Value;
|
|
|
+
|
|
|
+ public override bool Equals(object obj)
|
|
|
+ {
|
|
|
+ var register = obj as Register;
|
|
|
+ return register != null &&
|
|
|
+ Value == register.Value;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override int GetHashCode() => HashCode.Combine(Value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class Program
|
|
|
+ {
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ if (args.Length < 1) return;
|
|
|
+ if (File.Exists(args[0]) == false) return;
|
|
|
+ var file = File.OpenText(args[0]);
|
|
|
+
|
|
|
+ var pc = new Computer();
|
|
|
+
|
|
|
+ var result = new Dictionary<int, int>();
|
|
|
+
|
|
|
+ bool prevIsBlank = false;
|
|
|
+ int sample = 0;
|
|
|
+ do
|
|
|
+ {
|
|
|
+ var line = file.ReadLine();
|
|
|
+ if (line == null) break;
|
|
|
+
|
|
|
+ if (line == string.Empty || line.StartsWith("//"))
|
|
|
+ {
|
|
|
+ if (prevIsBlank) break;
|
|
|
+ prevIsBlank = true;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ else prevIsBlank = false;
|
|
|
+
|
|
|
+ result.Add(sample, 0);
|
|
|
+
|
|
|
+ string before = line;
|
|
|
+ string op = file.ReadLine();
|
|
|
+ string after = file.ReadLine();
|
|
|
+
|
|
|
+ var regBefore = before.Substring(9, before.Length - 10).Split(", ").Select(s => ushort.Parse(s)).ToArray();
|
|
|
+ var regAfter = after.Substring(9, after.Length - 10).Split(", ").Select(s => ushort.Parse(s)).ToArray();
|
|
|
+ var opParams = op.Split(" ").Select(s => ushort.Parse(s)).ToArray();
|
|
|
+
|
|
|
+ foreach (var opc in pc.CPU.OpCodes)
|
|
|
+ {
|
|
|
+ pc.CPU.SetRegisters(regBefore);
|
|
|
+
|
|
|
+ opc(opParams[1], opParams[2], opParams[3]);
|
|
|
+
|
|
|
+ var regresult = pc.CPU.Registers;
|
|
|
+
|
|
|
+ bool isSame = regresult[opParams[3]] == regAfter[opParams[3]];
|
|
|
+ if (isSame) result[sample]++;
|
|
|
+ }
|
|
|
+
|
|
|
+ sample++;
|
|
|
+ } while (true);
|
|
|
+
|
|
|
+ var resultCount = result.Count(r => r.Value >= 3);
|
|
|
+ Console.WriteLine($"The result is : {resultCount}");
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|