Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- namespace AdventOfCode_Solutions
- {
- class DayTwelve_2
- {
- public static List<Register> registers;
- internal static void Run()
- {
- string[] fileLines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "DayTwelve_Instructions.txt"));
- List<List<string>> instructions = new List<List<string>>();
- foreach (string line in fileLines)
- {
- instructions.Add(new List<string>());
- foreach (string part in line.Split(' '))
- {
- instructions[instructions.Count - 1].Add(part);
- }
- }
- registers = new List<Register>();
- registers.Add(new Register('a'));
- registers.Add(new Register('b'));
- registers.Add(new Register('c'));
- registers.Add(new Register('d'));
- int index = 0;
- while (index < instructions.Count)
- {
- switch (instructions[index][0])
- {
- case "cpy":
- int holder;
- if (int.TryParse(instructions[index][1], out holder))
- {
- Register.FindByID(instructions[index][2][0]).Set(holder);
- }
- else
- {
- Register.FindByID(instructions[index][2][0]).Set(Register.FindByID(instructions[index][1][0]).value);
- }
- index++;
- break;
- case "jnz":
- int holder2;
- if (int.TryParse(instructions[index][1], out holder2))
- {
- if (holder2 != 0)
- {
- index += int.Parse(instructions[index][2]);
- }
- else
- {
- index++;
- }
- }
- else
- {
- if (Register.FindByID(instructions[index][1][0]).value != 0)
- {
- index += int.Parse(instructions[index][2]);
- }
- else
- {
- index++;
- }
- }
- break;
- case "inc":
- Register.FindByID(instructions[index][1][0]).value++;
- index++;
- break;
- case "dec":
- Register.FindByID(instructions[index][1][0]).value--;
- index++;
- break;
- }
- Console.WriteLine("a:" + Register.FindByID('a').value + " b:" + Register.FindByID('b').value + " c:" + Register.FindByID('c').value + " d:" + Register.FindByID('d').value);
- }
- Console.WriteLine(Register.FindByID('a').value);
- }
- public class Register
- {
- public char id;
- public int value;
- public Register(char id)
- {
- this.id = id;
- if (id == 'c')
- {
- value = 1;
- }
- else
- {
- value = 0;
- }
- }
- public void Increment()
- {
- value++;
- }
- public void Set(int value)
- {
- this.value = value;
- }
- public static Register FindByID(char id)
- {
- foreach (Register register in registers)
- {
- if (register.id == id)
- {
- return register;
- }
- }
- return null;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment