Draco18s

Goto makes things easier what

Dec 15th, 2020
755
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Advent_of_Code_2020 {
  8.     internal class DayFifteen {
  9.         internal static long Part1(string input) {
  10.             string[] lines = input.Split(',');
  11.             List<int> turns = new List<int>();
  12.             foreach(string l in lines) {
  13.                 int v = int.Parse(l);
  14.                 turns.Add(v);
  15.             }
  16.             while(true) {
  17.                 int last = turns[turns.Count - 1]; // shove into list
  18.                 int next = GetDelay(turns, last); // get how long it has been
  19.                 turns.Add(next);
  20.                 if(turns.Count == 2020) {
  21.                     return next;
  22.                 }
  23.             }
  24.         }
  25.  
  26.         private static int GetDelay(List<int> turns, int last) {
  27.             // loop from end towards beginning (ignoring last index or we will always get 1)
  28.             for(int i = 1; i < turns.Count; i++) {
  29.                 int pp = turns[turns.Count - 1 - i];
  30.                 if(pp == last) {
  31.                     return i; // return how many values we had to look at
  32.                 }
  33.             }
  34.             return 0; // value is unique!
  35.         }
  36.  
  37.         internal static long Part2(string input) {
  38.             // we don't need the whole fekking list. We just need to know what turn
  39.             // the value was last seen
  40.             Dictionary<int, int> dict = new Dictionary<int, int>();
  41.             string[] lines = input.Split(',');
  42.             int turn = 0;
  43.             foreach(string l in lines) {
  44.                 int v = int.Parse(l);
  45.                 dict.Add(v, turn);
  46.                 turn++;
  47.             }
  48.             // input values are guaranteed unique, so the most recent number is always
  49.             // the first time it was said, so the first turn after input is always 0.
  50.             int next = 0;
  51.             int last = 0;
  52.             while(true) {
  53.                 // read comments, ignore the goto statements
  54.                 goto label4;
  55.             label3:
  56.                 // increment turn counter
  57.                 turn++;
  58.                 goto label5;
  59.             label2:
  60.                 // compute the next number based on the last
  61.                 if(dict.ContainsKey(last)) {
  62.                     next = turn - dict[last];
  63.                 }
  64.                 else {
  65.                     next = 0;
  66.                 }
  67.                 goto label1;
  68.             label4:
  69.                 // update variables
  70.                 last = next;
  71.                 goto label2;
  72.             label1:
  73.                 // store the number
  74.                 if(dict.ContainsKey(last)) {
  75.                     dict[last] = turn;
  76.                 }
  77.                 else {
  78.                     dict.Add(last, turn);
  79.                 }
  80.                 goto label3;
  81.             label5:
  82.                 // check for the end of the game
  83.                 if(turn == 30000000) { // 30 million
  84.                     return last; // we need the "last" number spoken
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment