Guest User

AoC_2016_DayTwelve

a guest
Dec 11th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. namespace AdventOfCode_Solutions
  6. {
  7.     class DayTwelve_2
  8.     {
  9.         public static List<Register> registers;
  10.  
  11.         internal static void Run()
  12.         {
  13.             string[] fileLines = File.ReadAllLines(Path.Combine(Directory.GetCurrentDirectory(), "DayTwelve_Instructions.txt"));
  14.             List<List<string>> instructions = new List<List<string>>();
  15.             foreach (string line in fileLines)
  16.             {
  17.                 instructions.Add(new List<string>());
  18.                 foreach (string part in line.Split(' '))
  19.                 {
  20.                     instructions[instructions.Count - 1].Add(part);
  21.                 }
  22.             }
  23.  
  24.             registers = new List<Register>();
  25.  
  26.             registers.Add(new Register('a'));
  27.             registers.Add(new Register('b'));
  28.             registers.Add(new Register('c'));
  29.             registers.Add(new Register('d'));
  30.  
  31.             int index = 0;
  32.             while (index < instructions.Count)
  33.             {
  34.                 switch (instructions[index][0])
  35.                 {
  36.                     case "cpy":
  37.                         int holder;
  38.                         if (int.TryParse(instructions[index][1], out holder))
  39.                         {
  40.                             Register.FindByID(instructions[index][2][0]).Set(holder);
  41.                         }
  42.                         else
  43.                         {
  44.                             Register.FindByID(instructions[index][2][0]).Set(Register.FindByID(instructions[index][1][0]).value);
  45.                         }
  46.                         index++;
  47.                         break;
  48.  
  49.                     case "jnz":
  50.                         int holder2;
  51.                         if (int.TryParse(instructions[index][1], out holder2))
  52.                         {
  53.                             if (holder2 != 0)
  54.                             {
  55.                                 index += int.Parse(instructions[index][2]);
  56.                             }
  57.                             else
  58.                             {
  59.                                 index++;
  60.                             }
  61.                         }
  62.                         else
  63.                         {
  64.                             if (Register.FindByID(instructions[index][1][0]).value != 0)
  65.                             {
  66.                                 index += int.Parse(instructions[index][2]);
  67.                             }
  68.                             else
  69.                             {
  70.                                 index++;
  71.                             }
  72.                         }
  73.  
  74.                         break;
  75.  
  76.                     case "inc":
  77.                         Register.FindByID(instructions[index][1][0]).value++;
  78.                         index++;
  79.                         break;
  80.  
  81.                     case "dec":
  82.                         Register.FindByID(instructions[index][1][0]).value--;
  83.                         index++;
  84.                         break;
  85.                 }
  86.                 Console.WriteLine("a:" + Register.FindByID('a').value + " b:" + Register.FindByID('b').value + " c:" + Register.FindByID('c').value + " d:" + Register.FindByID('d').value);
  87.             }
  88.             Console.WriteLine(Register.FindByID('a').value);
  89.         }
  90.  
  91.         public class Register
  92.         {
  93.             public char id;
  94.             public int value;
  95.  
  96.             public Register(char id)
  97.             {
  98.                 this.id = id;
  99.                 if (id == 'c')
  100.                 {
  101.                     value = 1;
  102.                 }
  103.                 else
  104.                 {
  105.                     value = 0;
  106.                 }
  107.             }
  108.  
  109.             public void Increment()
  110.             {
  111.                 value++;
  112.             }
  113.  
  114.             public void Set(int value)
  115.             {
  116.                 this.value = value;
  117.             }
  118.  
  119.             public static Register FindByID(char id)
  120.             {
  121.                 foreach (Register register in registers)
  122.                 {
  123.                     if (register.id == id)
  124.                     {
  125.                         return register;
  126.                     }
  127.                 }
  128.                 return null;
  129.             }
  130.         }
  131.     }
  132. }
Add Comment
Please, Sign In to add comment