Advent of Code 2017

Dit zijn mijn oplossingen voor Advent of Code 2017.
Alle oplossingen zijn geschreven in C# 7.2
Aantal lijnen code tot nu toe: 1461

Open volledig
private static void Day_1()
{
    string input = "77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277";

    int sum = 0;
    byte previous = 0;
    byte current = 0;
    for (int i = 0; i < input.Length; i++)
    {
        current = (byte)char.GetNumericValue(input[i]);
        if (i == 0)
        {
            previous = (byte)char.GetNumericValue(input.Last());
        }
        if (current == previous)
        {
            sum += current;
        }
        previous = current;
    }
    Debug.WriteLine(sum);
}

static void Day_1_2()
{
    string input = "77736991856689225253142335214746294932318813454849177823468674346512426482777696993348135287531487622845155339235443718798255411492778415157351753377959586612882455464736285648473397681163729345143319577258292849619491486748832944425643737899293811819448271546283914592546989275992844383947572926628695617661344293284789225493932487897149244685921644561896799491668147588536732985476538413354195246785378443492137893161362862587297219368699689318441563683292683855151652394244688119527728613756153348584975372656877565662527436152551476175644428333449297581939357656843784849965764796365272113837436618857363585783813291999774718355479485961244782148994281845717611589612672436243788252212252489833952785291284935439662751339273847424621193587955284885915987692812313251556836958571335334281322495251889724281863765636441971178795365413267178792118544937392522893132283573129821178591214594778712292228515169348771198167462495988252456944269678515277886142827218825358561772588377998394984947946121983115158951297156321289231481348126998584455974277123213413359859659339792627742476688827577318285573236187838749444212666293172899385531383551142896847178342163129883523694183388123567744916752899386265368245342587281521723872555392212596227684414269667696229995976182762587281829533181925696289733325513618571116199419759821597197636415243789757789129824537812428338192536462468554399548893532588928486825398895911533744671691387494516395641555683144968644717265849634943691721391779987198764147667349266877149238695714118982841721323853294642175381514347345237721288281254828745122878268792661867994785585131534136646954347165597315643658739688567246339618795777125767432162928257331951255792438831957359141651634491912746875748363394329848227391812251812842263277229514125426682179711184717737714178235995431465217547759282779499842892993556918977773236196185348965713241211365895519697294982523166196268941976859987925578945185217127344619169353395993198368185217391883839449331638641744279836858188235296951745922667612379649453277174224722894599153367373494255388826855322712652812127873536473277";
    int half = input.Length / 2;
    int sum = 0;
    byte current = 0;
    for (int i = 0; i < input.Length; i++)
    {
        current = (byte)char.GetNumericValue(input[i]);
        int nextLoc = i + half;
        if (nextLoc >= input.Length)
        {
            nextLoc = nextLoc - input.Length;
        }
        if (current == (byte)char.GetNumericValue(input[nextLoc]))
        {
            sum += current;
        }
    }
    Debug.WriteLine(sum);
}
Open volledig
static void Day_2()
{
    string line1 = "86      440     233     83      393     420     228     491     159     13      110     135     97      238     92      396";
    string line2 = "3646    3952    3430    145     1574    2722    3565    125     3303    843     152     1095    3805    134     3873    3024";
    string line3 = "2150    257     237     2155    1115    150     502     255     1531    894     2309    1982    2418    206     307     2370";
    string line4 = "1224    343     1039    126     1221    937     136     1185    1194    1312    1217    929     124     1394    1337    168";
    string line5 = "1695    2288    224     2667    2483    3528    809     263     2364    514     3457    3180    2916    239     212     3017";
    string line6 = "827     3521    127     92      2328    3315    1179    3240    695     3144    3139    533     132     82      108     854";
    string line7 = "1522    2136    1252    1049    207     2821    2484    413     2166    1779    162     2154    158     2811    164     2632";
    string line8 = "95      579     1586    1700    79      1745    1105    89      1896    798     1511    1308    1674    701     60      2066";
    string line9 = "1210    325     98      56      1486    1668    64      1601    1934    1384    69      1725    992     619     84      167";
    string line10 = "4620    2358    2195    4312    168     1606    4050    102     2502    138     135     4175    1477    2277    2226    1286";
    string line11 = "5912    6261    3393    431     6285    3636    4836    180     6158    6270    209     3662    5545    204     6131    230";
    string line12 = "170     2056    2123    2220    2275    139     461     810     1429    124     1470    2085    141     1533    1831    518";
    string line13 = "193     281     2976    3009    626     152     1750    1185    3332    715     1861    186     1768    3396    201     3225";
    string line14 = "492     1179    154     1497    819     2809    2200    2324    157     2688    1518    168     2767    2369    2583    173";
    string line15 = "286     2076    243     939     399     451     231     2187    2295    453     1206    2468    2183    230     714     681";
    string line16 = "3111    2857    2312    3230    149     3082    408     1148    2428    134     147     620     128     157     492     2879";
    int[][] arr = new int[16][];

    for (int i = 0; i < 16; i++)
    {
        arr[0] = line1.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[1] = line2.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[2] = line3.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[3] = line4.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[4] = line5.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[5] = line6.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[6] = line7.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[7] = line8.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[8] = line9.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[9] = line10.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[10] = line11.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[11] = line12.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[12] = line13.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[13] = line14.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[14] = line15.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    for (int i = 0; i < 16; i++)
    {
        arr[15] = line16.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    int sum = 0;
    foreach (var row in arr)
    {
        int max = row.Max();
        int min = row.Min();
        sum += max - min;
    }
    Debug.WriteLine(sum);

}

static void Day_2_2()
{
    int[][] numbers = new int[16][];
    string path = @"D:\day2.txt";
    int counter = 0;
    foreach (string line in File.ReadLines(path))
    {
        numbers[counter++] = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray();
    }
    int sum = 0;
    foreach (var row in numbers)
    {
        for (int i = 0; i < row.Length; i++)
        {
            for (int x = 0; x < row.Length; x++)
            {
                if (i != x)
                {

                    if ((row[i] % row[x]) == 0)
                    {
                        sum += row[i] / row[x];
                        break;
                    }
                }
            }
        }
    }
    Debug.WriteLine(sum);
}
Open volledig
static void Day_3()
{
    int center = 269;
    int currentX = 269;
    int currentY = 269;
    int x = 1;
    int y = 0;
    int numberx = 0;
    int numbery = 0;
    int[,] mem = new int[539, 539];
    mem[center, center] = 1;
    for (int i = 2; i < 290521; i++)
    {
        if (i == 289326)
        {
            numberx = currentX + x;
            numbery = currentY + y;
        }
        if (y == 0 && x == 1)
        {
            mem[currentY, ++currentX] = i;
            if (mem[currentY + 1, currentX] != 0)
            {
                y = 0;
                x = 1;
            }
            else
            {
                y = 1;
                x = 0;
            }
        }
        else if (y == 1 && x == 0)
        {
            mem[++currentY, currentX] = i;

            if (mem[currentY, currentX - 1] != 0)
            {
                y = 1;
                x = 0;
            }
            else
            {
                y = 0;
                x = -1;
            }
        }
        else if (y == 0 && x == -1)
        {
            mem[currentY, --currentX] = i;
            if (mem[currentY - 1, currentX] != 0)
            {
                y = 0;
                x = -1;
            }
            else
            {
                y = -1;
                x = 0;
            }
        }
        else if (y == -1 && x == 0)
        {
            mem[--currentY, currentX] = i;
            if (mem[currentY, currentX + 1] != 0)
            {
                y = -1;
                x = 0;
            }
            else
            {
                y = 0;
                x = 1;
            }
        }
    }

    int steps = center - numberx + numbery - center;
    Debug.WriteLine(steps);
}

static void Day_3_2()
{
    int center = 269;
    int currentX = 269;
    int currentY = 269;
    int x = 1;
    int y = 0;
    int numberx = 0;
    int numbery = 0;
    int[,] mem = new int[539, 539];
    mem[center, center] = 1;

    int i = 1;
    while (i <= 300000)
    {
        if (i > 289326)
        {
            numberx = currentX + x;
            numbery = currentY + y;
            Debug.WriteLine(i);
            return;
        }
        if (y == 0 && x == 1)
        {
            currentX++;
            i = mem[currentY, currentX]
            + mem[currentY, currentX + 1]
            + mem[currentY, currentX - 1]
            + mem[currentY + 1, currentX]
            + mem[currentY + 1, currentX + 1]
            + mem[currentY + 1, currentX - 1]
            + mem[currentY - 1, currentX]
            + mem[currentY - 1, currentX + 1]
            + mem[currentY - 1, currentX - 1];
            mem[currentY, currentX] = i;
            if (mem[currentY + 1, currentX] != 0)
            {
                y = 0;
                x = 1;
            }
            else
            {
                y = 1;
                x = 0;
            }
        }
        else if (y == 1 && x == 0)
        {
            currentY++;
            i = mem[currentY, currentX]
            + mem[currentY, currentX + 1]
            + mem[currentY, currentX - 1]
            + mem[currentY + 1, currentX]
            + mem[currentY + 1, currentX + 1]
            + mem[currentY + 1, currentX - 1]
            + mem[currentY - 1, currentX]
            + mem[currentY - 1, currentX + 1]
            + mem[currentY - 1, currentX - 1];
            mem[currentY, currentX] = i;

            if (mem[currentY, currentX - 1] != 0)
            {
                y = 1;
                x = 0;
            }
            else
            {
                y = 0;
                x = -1;
            }
        }
        else if (y == 0 && x == -1)
        {
            currentX--;
            i = mem[currentY, currentX]
            + mem[currentY, currentX + 1]
            + mem[currentY, currentX - 1]
            + mem[currentY + 1, currentX]
            + mem[currentY + 1, currentX + 1]
            + mem[currentY + 1, currentX - 1]
            + mem[currentY - 1, currentX]
            + mem[currentY - 1, currentX + 1]
            + mem[currentY - 1, currentX - 1];
            mem[currentY, currentX] = i;
            if (mem[currentY - 1, currentX] != 0)
            {
                y = 0;
                x = -1;
            }
            else
            {
                y = -1;
                x = 0;
            }
        }
        else if (y == -1 && x == 0)
        {
            currentY--;
            i = mem[currentY, currentX]
            + mem[currentY, currentX + 1]
            + mem[currentY, currentX - 1]
            + mem[currentY + 1, currentX]
            + mem[currentY + 1, currentX + 1]
            + mem[currentY + 1, currentX - 1]
            + mem[currentY - 1, currentX]
            + mem[currentY - 1, currentX + 1]
            + mem[currentY - 1, currentX - 1];
            mem[currentY, currentX] = i;
            if (mem[currentY, currentX + 1] != 0)
            {
                y = -1;
                x = 0;
            }
            else
            {
                y = 0;
                x = 1;
            }
        }
    }

    int steps = center - numberx + numbery - center;
    Debug.WriteLine(steps);
}
Open volledig
static void Day_4()
{
    string path = @"D:\day4.txt";

    int sum = 0;
    foreach (string line in File.ReadLines(path))
    {
        if (line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Distinct().Count() == line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Count())
        {
            sum++;
        }
    }
    Debug.WriteLine(sum);
}

static void Day_4_2()
{
    string path = @"D:\day4.txt";

    int sum = 0;
    foreach (string line in File.ReadLines(path))
    {
        Debug.WriteLine(line);
        string[] words = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);

        for (int i = 0; i < words.Length; i++)
        {
            words[i] = String.Concat(words[i].OrderBy(x => x));
        }

        if (words.Distinct().Count() == words.Count())
        {
            sum++;
        }
    }
    Debug.WriteLine(sum);
}
Open volledig
static void Day_5()
{
    List jumps = new List();
    string path = @"D:\day5.txt";
    foreach (string line in File.ReadLines(path))
    {
        jumps.Add(int.Parse(line.Trim()));
    }
    int steps = 0;
    int index = 0;
    while (index < jumps.Count() || index < 0)
    {
        index += jumps[index]++;

        steps++;
    }
    Debug.WriteLine(steps);
}

static void Day_5_2()
{
    List jumps = new List();
    string path = @"D:\day5.txt";
    foreach (string line in File.ReadLines(path))
    {
        jumps.Add(int.Parse(line.Trim()));
    }
    int steps = 0;
    int index = 0;
    int prevIndex = 0;
    while (index < jumps.Count() || index < 0)
    {
        prevIndex = index;
        index += jumps[index];
        if (jumps[prevIndex] >= 3)
        {
            jumps[prevIndex]--;
        }
        else
        {
            jumps[prevIndex]++;
        }

        steps++;
    }
    Debug.WriteLine(steps);
}
Open volledig
static void Day_6()
{
    int[] bank = { 10, 3, 15, 10, 5, 15, 5, 15, 9, 2, 5, 8, 5, 2, 3, 6 };
    int sum = 0;
    List order = new List();

    while (true)
    {
        int value = bank.Max();
        int index = Array.IndexOf(bank, value);
        bank[index] = 0;

        index++;

        while (value != 0)
        {
            if (index == bank.Length)
            {
                index = 0;
            }

            bank[index]++;
            value--;


            index++;
        }
        sum++;
        string sort = "";
        for (int i = 0; i < bank.Length; i++)
        {
            sort += (bank[i] + " ").ToString();
        }
        sort.TrimEnd();
        if (order.Contains(sort))
        {
            int index1 = order.IndexOf(sort);
            Debug.WriteLine(sum - index1 - 1);
            break;
        }
        order.Add(sort);
    }
    Debug.WriteLine(sum);
}
Open volledig
static void Day_7()
{
    List lijst = new List();
    string path = @"D:\day7.txt";

    foreach (string line in File.ReadLines(path))
    {

        string[] s = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
        lijst.Add(new Day7List
        {
            Name = s[0],
            Weight = int.Parse(s[1].Substring(1, s[1].Length - 2)),
            Childs = new List()
        });



        if (line.Contains("->"))
        {
            int length = s.Count();
            for (int i = 3; i < length; i++)
            {
                int index = lijst.FindIndex(x => x.Name == s[0]);
                string child = s[i].TrimEnd();
                lijst[index].Childs.Add(child);
            }
        }
    }

    int highest = 0;
    string highName = "";

    foreach (var element in lijst)
    {
        foreach (var child in element.Childs)
        {
            sum = 0;
            CountChilds(child, lijst);
            if (sum > highest)
            {
                highest = sum;
                highName = element.Name;
            }
        }
    }
    Debug.WriteLine("Bottom program: " + highName);

    int index2 = lijst.FindIndex(x => x.Name == highName);
    Debug.WriteLine("Wrong weight: " + GetWrongWeight(index2, lijst));
    int index3 = lijst.FindIndex(x => x.Name == PreviousWeightName);
    int correctedWeight = CorrectWeight - WrongWeight + lijst[index3].Weight;
    Debug.WriteLine("Corrected weight: " + correctedWeight);


}

static int sum = 0;
static int CountChilds(string child, List lijst)
{
    int index = lijst.FindIndex(x => x.Name == child);
    sum += lijst[index].Childs.Count();
    foreach (string element in lijst[index].Childs)
    {
        CountChilds(element, lijst);
    }
    return sum;
}

static int weightSum = 0;
static int CountWeight(string child, List lijst)
{
    int index = lijst.FindIndex(x => x.Name == child);
    weightSum += lijst[index].Weight;
    foreach (string element in lijst[index].Childs)
    {
        CountWeight(element, lijst);
    }

    return weightSum;
}

static string PreviousWeightName;
static int CorrectWeight;
static int WrongWeight;
static string GetWrongWeight(int index, List lijst)
{
    if (lijst[index].Childs.Count() > 2)
    {
        List weights = new List();
        foreach (string child in lijst[index].Childs)
        {
            weightSum = 0;
            weights.Add(new ChildWeight
            {
                Name = child,
                TotalWeight = CountWeight(child, lijst)
            });
        }

        List test2 = new List();
        foreach (var element in weights)
        {
            test2.Add(element.TotalWeight);
        }
        int[] test3 = test2.Distinct().ToArray();

        if (test3.Length == 1)
        {
            return PreviousWeightName;
        }

        if (test2.FindAll(x => x.Equals(test3[0])).Count() == 1)
        {
            int index2 = weights.FindIndex(x => x.TotalWeight == test3[0]);
            string name = weights[index2].Name;
            int index3 = lijst.FindIndex(x => x.Name == name);
            PreviousWeightName = name;

            WrongWeight = test3[0];
            CorrectWeight = test3[1];
            GetWrongWeight(index3, lijst);

        }
        else if (test2.FindAll(x => x.Equals(test3[1])).Count() == 1)
        {
            int index2 = weights.FindIndex(x => x.TotalWeight == test3[1]);
            string name = weights[index2].Name;
            int index3 = lijst.FindIndex(x => x.Name == name);
            PreviousWeightName = name;

            WrongWeight = test3[1];
            CorrectWeight = test3[0];

            GetWrongWeight(index3, lijst);
        }
        else
        {
            return "error";
        }
    }
    else
    {
        return "error";
    }
    return PreviousWeightName;
}

public class Day7List
{
    public List Childs { get; set; }
    public string Name { get; set; }
    public int Weight { get; set; }
}
public class ChildWeight
{
    public string Name { get; set; }
    public int TotalWeight { get; set; }
}
Open volledig
static void Day_8()
{
    List instructList = new List();
    List values = new List();

    string path = @"D:\day8.txt";
    foreach (string line in File.ReadLines(path))
    {
        string[] words = line.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();


        instructList.Add(new Instruction
        {
            Register = words[0],
            Value = 0,
            ProcessVal = int.Parse(words[2]),
            ProcessDo = words[1],
            Condition = words[4] + "," + words[5] + words[6]
        });
        if (values.FindIndex(x => x.Register == words[0]) == -1)
        {
            values.Add(new InstructionVal
            {
                Register = words[0],
                Value = 0
            });
        }
    }

    int highest = 0;
    foreach (var row in instructList)
    {
        string conditionRegister = row.Condition.Substring(0, row.Condition.IndexOf());
        string conditionVal = row.Condition.Substring(row.Condition.IndexOf() + 1);
        //bool condition = false;
        if (Eval(values.Find(x => x.Register == conditionRegister).Value + conditionVal) == 1)
        {
            //condition = true;
            if (row.ProcessDo == "inc")
            {
                values.Find(x => x.Register == row.Register).Value += row.ProcessVal;
            }
            else if (row.ProcessDo == "dec")
            {
                values.Find(x => x.Register == row.Register).Value -= row.ProcessVal;
            }
            if (values.Find(x => x.Register == row.Register).Value > highest)
            {
                highest = values.Find(x => x.Register == row.Register).Value;
            }
        }
    }
    Debug.WriteLine("Highest value: " + values.MaxBy(x => x.Value).Value);
    Debug.WriteLine("Ever highest " + highest);
}

public class Instruction
{
    public string Condition { get; set; }
    public string Register { get; set; }
    public int Value { get; set; }
    public int ProcessVal { get; set; }
    public string ProcessDo { get; set; }
}
public class InstructionVal
{
    public string Register { get; set; }
    public int Value { get; set; }
}

static int Eval(String expression)
{
    System.Data.DataTable table = new System.Data.DataTable();
    if (expression.Contains("=="))
    {
        int index = expression.IndexOf("==");
        //return Convert.ToInt32(table.Compute(expression.Remove(expression.IndexOf("=="), 1), String.Empty));
        if (int.Parse(expression.Substring(0, index)) == int.Parse(expression.Substring(index + 2)))
        {
            return 1;
        }
        return 0;
    }
    if (expression.Contains("!="))
    {
        int index = expression.IndexOf("!=");

        if (int.Parse(expression.Substring(0, index)) != int.Parse(expression.Substring(index + 2)))
        {
            return 1;
        }
        return 0;
    }
    return Convert.ToInt32(table.Compute(expression, String.Empty));
}
Open volledig
static void Day_9()
{
    string path = @"D:\day9.txt";

    string input = File.ReadAllText(path);

    int score = 0;
    int garbageCount = 0;
    int indent = 1;
    bool inGarbage = false;
    for (int i = 0; i < input.Length; i++)
    {

        switch (input[i])
        {
            case "!":
                i++;
                break;

            case "{":
                if (!inGarbage)
                {
                    score += indent++;
                }
                else
                {
                    garbageCount++;
                }
                break;

            case "}":
                if (!inGarbage)
                {
                    indent--;
                }
                else
                {
                    garbageCount++;
                }
                break;

            case "<":
                if (inGarbage)
                {
                    garbageCount++;
                }
                inGarbage = true;
                break;

            case ">":
                inGarbage = false;
                break;
            default:
                if (inGarbage)
                {
                    garbageCount++;
                }
                break;
        }
    }
    Debug.WriteLine("Total score: " + score);
    Debug.WriteLine("Garbage count: " + garbageCount);
}
Open volledig
static void Day_10()
{
    int currentPos = 0;
    int skip = 0;
    string path = @"D:\10.txt";

    List knot = new List();

    knot.AddRange(Enumerable.Range(0, 256));

    string line = File.ReadAllText(path);
    List lengths = new List();
    lengths.AddRange(line.Split().Select(int.Parse).ToList());



    List reverseList = new List();
    for (int i = 0; i < lengths.Count(); i++)
    {
        int reverseLength = lengths[i];
        ReverseList(knot, currentPos, reverseLength);
        currentPos = IncreasePos(currentPos, knot.Count(), reverseLength, skip);
        skip++;
    }
    Debug.WriteLine("First answer: {0}", knot[0] * knot[1]);

    List asciiKnot = new List();
    asciiKnot.AddRange(Enumerable.Range(0, 256));
    List asciiLengths = new List();
    asciiLengths.AddRange(Encoding.ASCII.GetBytes(line));
    asciiLengths.AddRange(new byte[] { 17, 31, 73, 47, 23 });

    currentPos = 0;
    skip = 0;

    for (int j = 0; j < 64; j++)
    {
        for (int i = 0; i < asciiLengths.Count(); i++)
        {
            int reverseLength = asciiLengths[i];
            ReverseList(asciiKnot, currentPos, reverseLength);
            currentPos = IncreasePos(currentPos, asciiKnot.Count(), reverseLength, skip);
            skip++;
        }
    }

    List processed = new List();

    for (int i = 0; i < 16; i++)
    {
        byte xor = Convert.ToByte(asciiKnot[i * 16]);
        for (int j = 16 * i + 1; j < 16 * (i + 1); j++)
        {
            xor ^= Convert.ToByte(asciiKnot[j]);
        }
        processed.Add(xor);
    }

    Debug.WriteLine("Second answer: " + BitConverter.ToString(processed.ToArray()).Replace("-", string.Empty));
}

static int IncreasePos(int currentPos, int knotLength, int value, int skip)
{
    currentPos += value + skip;

    while (currentPos > knotLength)
    {
        currentPos -= knotLength;
    }

    return currentPos;
}

static void ReverseList(List knot, int currentPos, int reverseLength)
{
    List reverseList = new List();
    if (currentPos + reverseLength > knot.Count())
    {
        reverseList.AddRange(knot.GetRange(currentPos, knot.Count() - currentPos));
        int splitIndex = reverseList.Count();

        reverseList.AddRange(knot.GetRange(0, reverseLength - knot.Count() + currentPos));

        int knotLength = knot.Count();
        knot.RemoveRange(currentPos, knotLength - currentPos);
        knot.RemoveRange(0, reverseLength - knotLength + currentPos);
        reverseList.Reverse();

        knot.AddRange(reverseList.GetRange(0, splitIndex));
        knot.InsertRange(0, reverseList.GetRange(splitIndex, reverseList.Count() - splitIndex));
    }
    else
    {
        reverseList.AddRange(knot.GetRange(currentPos, reverseLength));
        reverseList.Reverse();
        knot.RemoveRange(currentPos, reverseLength);
        knot.InsertRange(currentPos, reverseList);
    }
}
Open volledig
static void Day_11()
{
    string path = @"D:\day11.txt";
    string input = File.ReadAllText(path);


    var loc = (y: 0, x: 0, z: 0);
    int maxDistance = 0;
    foreach (string word in input.Split())
    {
        switch (word)
        {
            case "nw":
                loc.y += 1;
                loc.x -= 1;
                break;
            case "n":
                loc.y += 1;
                loc.z -= 1;
                break;
            case "ne":
                loc.x += 1;
                loc.z -= 1;
                break;
            case "se":
                loc.x += 1;
                loc.y -= 1;
                break;
            case "s":
                loc.y -= 1;
                loc.z += 1;
                break;
            case "sw":
                loc.x -= 1;
                loc.z += 1;
                break;
        }
        int tempDistance = (Math.Abs(loc.x) + Math.Abs(loc.y) + Math.Abs(loc.z)) / 2;
        if (maxDistance < tempDistance)
        {
            maxDistance = tempDistance;
        }
    }
    int distance = (Math.Abs(loc.x) + Math.Abs(loc.y) + Math.Abs(loc.z)) / 2;
    Debug.WriteLine("Distance: " + distance);
    Debug.WriteLine("Max distance: " + maxDistance);

}
Open volledig
static void Day_12()
{
    string path = @"D:\day12.txt";
    Dictionary> dictionary = new Dictionary>();

    foreach (string line in File.ReadLines(path))
    {
        List connect = new List();
        string trimmedLine = line.Replace(",", " ");
        string[] words = trimmedLine.Split(new char[0], StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray();
        for (int i = 2; i < words.Count(); i++)
        {
            connect.Add(int.Parse(words[i]));
        }
        dictionary.Add(int.Parse(words[0]), connect);
    }

    GetConnections(dictionary, 0);
    int amount = group.Count();
    Debug.WriteLine("Connected to 0: " + amount);
    group.Clear();

    int groupCount = 1;
    for (int i = 1; i < dictionary.Count(); i++)
    {
        if (!hasGroup.Contains(i))
        {
            GetConnections(dictionary, i);
            groupCount++;
            group.Clear();
        }
    }
    Debug.WriteLine("Amount of groups: " + groupCount);
}

static List group = new List();
static List hasGroup = new List();
static void GetConnections(Dictionary> dictionary, int index)
{
    if (!group.Contains(index))
    {
        group.Add(index);
        hasGroup.Add(index);
        foreach (int number in dictionary[index])
        {
            if (!group.Contains(number))
            {
                GetConnections(dictionary, number);
            }
        }
    }
}
Open volledig
static void Day_13()
{
    string path = @"D:\day13.txt";
    List firewall = new List();
    int counter = 0;
    foreach (string line in File.ReadLines(path))
    {
        int[] words = line.Split(new[] { ":" }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x.Trim())).ToArray();
        int depth = words[0];
        int range = words[1];

        if (counter < depth)
        {
            for (int i = counter; counter < depth; counter++)
            {
                firewall.Add(new Firewall
                {
                    Depth = counter,
                    Range = 0,
                    Scanner = 0,
                    ScannerDirection = 0
                });
            }
        }
        firewall.Add(new Firewall
        {
            Depth = depth,
            Range = range,
            Scanner = 1,
            ScannerDirection = 1
        });
        counter++;
    }
    int packet = -1;
    int caught = 0;
    foreach (var layer in firewall)
    {
        packet++;
        if (layer.Scanner == 1)
        {
            caught += layer.Depth * layer.Range;
        }

        foreach (var element in firewall)
        {
            if ((element.Scanner == element.Range && element.ScannerDirection != -1) || (element.Scanner == 1 && element.ScannerDirection != 1))
            {
                element.ScannerDirection *= -1;
                element.Scanner += element.ScannerDirection;
            }
            else
            {
                element.Scanner += element.ScannerDirection;
            }
        }
    }
    Debug.WriteLine("Severity: " + caught);


    //Part 2
    int wait = 0;

    bool notCaught = false;
    while (!notCaught)
    {
        notCaught = true;
        foreach (var element in firewall)
        {
            if (element.Scanner != 0)
            {
                if ((element.Depth + wait) % (2 * element.Range - 2) == 0)
                {
                    notCaught = false;
                    wait++;
                    break;
                }
            }
        }
    }
    Debug.WriteLine("Wait: " + wait);
}

public class Firewall
{
    public int Depth;
    public int Range;
    public int Scanner;
    public int ScannerDirection;
}
Open volledig
static void Day_14()
{
    string input = "hfdlxzhv";
    List disk = new List();
    for (int i = 0; i < 128; i++)
    {
        string hexString = KnotHash(input + "-" + i);
        disk.Add(String.Join("", hexString.Select(x => Convert.ToString(Convert.ToInt32(x.ToString(), 16), 2).PadLeft(4, '0'))));
    }

    int sum = 0;
    foreach(string row in disk)
    {
        sum += row.Count(x => x.Equals('1'));
    }

    Debug.WriteLine("Number of squares: " + sum);

    List> groups = new List>();
    sum = 0;
    for(int i = 0; i < 128; i++)
    {
        for(int j = 0; j < 128; j++)
        {
            if (disk[i][j].Equals('1') && !groups.Contains(Tuple.Create(i, j)))
            {
                sum++;
                groups.Add(Tuple.Create(i, j));
                CheckGroups(disk, groups, i, j);
            }
        }
    }
    Debug.WriteLine("Number of groups: " + sum);

}

static string KnotHash(string input)
{
    List asciiKnot = new List();
    asciiKnot.AddRange(Enumerable.Range(0, 256));
    List asciiLengths = new List();
    asciiLengths.AddRange(Encoding.ASCII.GetBytes(input));
    asciiLengths.AddRange(new byte[] { 17, 31, 73, 47, 23 });

    int currentPos = 0;
    int skip = 0;

    for (int j = 0; j < 64; j++)
    {
        for (int i = 0; i < asciiLengths.Count(); i++)
        {
            int reverseLength = asciiLengths[i];
            ReverseList(asciiKnot, currentPos, reverseLength);
            currentPos = IncreasePos(currentPos, asciiKnot.Count(), reverseLength, skip);
            skip++;
        }
    }

    List processed = new List();

    for (int i = 0; i < 16; i++)
    {
        byte xor = Convert.ToByte(asciiKnot[i * 16]);
        for (int j = 16 * i + 1; j < 16 * (i + 1); j++)
        {
            xor ^= Convert.ToByte(asciiKnot[j]);
        }
        processed.Add(xor);
    }

    return BitConverter.ToString(processed.ToArray()).Replace("-", string.Empty);
}

static void CheckGroups(List disk, List> groups, int x, int y)
{
    if (x + 1 < 128 && disk[x + 1][y].Equals('1'))
    {
        if (!groups.Contains(Tuple.Create(x + 1, y)))
        {
            groups.Add(Tuple.Create(x + 1, y));
            CheckGroups(disk, groups, x + 1, y);
        }
    }
    if (x - 1 >= 0 && disk[x - 1][y].Equals('1'))
    {
        if (!groups.Contains(Tuple.Create(x - 1, y)))
        {
            groups.Add(Tuple.Create(x - 1, y));
            CheckGroups(disk, groups, x - 1, y);
        }
    }
    if (y + 1 < 128 && disk[x][y + 1].Equals('1'))
    {
        if (!groups.Contains(Tuple.Create(x, y + 1)))
        {
            groups.Add(Tuple.Create(x, y + 1));
            CheckGroups(disk, groups, x, y + 1);
        }
    }
    if (y - 1 >= 0 && disk[x][y - 1].Equals('1'))
    {
        if (!groups.Contains(Tuple.Create(x, y - 1)))
        {
            groups.Add(Tuple.Create(x, y - 1));
            CheckGroups(disk, groups, x, y - 1);
        }
    }
}
Open volledig
static void Day_15()
{
    long generatorA = 289;
    long generatorB = 629;
    int factorA = 16807;
    int factorB = 48271;
    int sum = 0;
    for (int i = 0; i < 40000000; i++)
    {
        generatorA = (generatorA * factorA) % 2147483647;
        generatorB = (generatorB * factorB) % 2147483647;
        string binaryA = Convert.ToString(generatorA, 2).PadLeft(32, '0');
        string binaryB = Convert.ToString(generatorB, 2).PadLeft(32, '0');
        if (binaryA.Substring(16).Equals(binaryB.Substring(16)))
            sum++;
    }
    Debug.WriteLine("Count: " + sum);
}


//Part 2
static void Day_15_2()
{
    long generatorA = 289;
    long generatorB = 629;
    int factorA = 16807;
    int factorB = 48271;
    int sum = 0;
    for (int i = 0; i < 5000000; i++)
    {
        do
        {
            generatorA = (generatorA * factorA) % 2147483647;
        } while (generatorA % 4 != 0);
        do
        {
            generatorB = (generatorB * factorB) % 2147483647;
        } while (generatorB % 8 != 0);
        string binaryA = Convert.ToString(generatorA, 2).PadLeft(32, '0');
        string binaryB = Convert.ToString(generatorB, 2).PadLeft(32, '0');
        if (binaryA.Substring(16).Equals(binaryB.Substring(16)))
            sum++;
    }
    Debug.WriteLine("Count: " + sum);

}
Open volledig
static void Day_16()
{
    string programs = "abcdefghijklmnop";
    string path = @"D:\day16.txt";
    string input = File.ReadAllText(path);
    int first;
    int second;
    StringBuilder sb;
    char save;
    foreach (string move in input.Split(','))
    {
        switch (move[0])
        {
            case 's':
                for (int i = 0; i < int.Parse(move.Substring(1)); i++)
                {
                    programs = String.Join("", programs.Prepend(programs[15])).Substring(0, 16);
                }
                break;

            case 'x':
                int seperatorIndex = move.IndexOf('/');
                first = int.Parse(move.Substring(1, seperatorIndex - 1));
                second = int.Parse(move.Substring(seperatorIndex + 1));
                save = programs[first];
                sb = new StringBuilder(programs);
                sb[first] = programs[second];
                sb[second] = save;
                programs = sb.ToString();
                sb = null;
                break;

            case 'p':
                first = programs.IndexOf(move[1]);
                second = programs.IndexOf(move[3]);
                sb = new StringBuilder(programs);
                save = programs[first];
                sb[first] = programs[second];
                sb[second] = save;
                programs = sb.ToString();
                break;

            default:
                Debug.WriteLine("Error");
                return;
        }
    }
    Debug.WriteLine(programs);
}

static void Day_16_2()
{
    string programs = "abcdefghijklmnop";
    string path = @"D:\day16.txt";
    string input = File.ReadAllText(path);
    string firstDance = "ionlbkfeajgdmphc";
    int first;
    int second;
    StringBuilder sb;
    char save;
    //Every 30 runs, programs stand in the same order
    for (int x = 0; x < 1000000000 % 30; x++)
    {
        foreach (string move in input.Split(','))
        {
            switch (move[0])
            {
                case 's':
                    for (int i = 0; i < int.Parse(move.Substring(1)); i++)
                    {
                        programs = String.Join("", programs.Prepend(programs[15])).Substring(0, 16);
                    }
                    break;

                case 'x':
                    int seperatorIndex = move.IndexOf('/');
                    first = int.Parse(move.Substring(1, seperatorIndex - 1));
                    second = int.Parse(move.Substring(seperatorIndex + 1));
                    save = programs[first];
                    sb = new StringBuilder(programs);
                    sb[first] = programs[second];
                    sb[second] = save;
                    programs = sb.ToString();
                    sb = null;
                    break;

                case 'p':
                    first = programs.IndexOf(move[1]);
                    second = programs.IndexOf(move[3]);
                    sb = new StringBuilder(programs);
                    save = programs[first];
                    sb[first] = programs[second];
                    sb[second] = save;
                    programs = sb.ToString();
                    sb = null;
                    break;

                default:
                    Debug.WriteLine("Error");
                    return;
            }
        }
        if (firstDance.Equals(programs))
        {
            Debug.WriteLine(x);
        }
    }
    Debug.WriteLine(programs);
}
Open volledig
static void Day_17()
{
    List spinlock = new List();
    int steps = 343;
    int current = 0;
    for (int i = 0; i < 2018; i++)
    {
        if (++current >= spinlock.Count())
        {
            spinlock.Add(i);
        }
        else
        {
            spinlock.Insert(current, i);
        }
        current = DoSteps(spinlock, steps, current);
        
    }

    int index = spinlock.IndexOf(2017);
    Debug.WriteLine("Value after 2017: " + spinlock[index + 1]);
    current = 0;
    int value = 0;
    for (int i = 0; i < 50000000; i++)
    {
        index = (current + steps) % (i + 1) + 1;
        if (index == 1)
        {
            value = i + 1;
        }
        current = index;
    }
    Debug.WriteLine("Value after 0: " + value);

}

static int DoSteps(List spinlock, int steps, int current)
{

    if (steps > spinlock.Count())
    {
        steps = steps % spinlock.Count();
    }
    if (current + steps >= spinlock.Count())
    {
        steps -= spinlock.Count() - current;
        current = steps;
    }
    else
    {
        current += steps;
    }
    

    return current;
}
Open volledig
static void Day_18()
{
    string path = @"C:\Users\arthu\Documents\day18.txt";
    List duet = new List();
    Dictionary duetVals = new Dictionary();
    duet.AddRange(File.ReadAllLines(path));

    double lastPlayed = 0;
    bool rcv = false;
    for (int i = 0; i < duet.Count(); i++)
    {
        if (rcv)
        {
            break;
        }
        string[] splitted = duet[i].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);

        if (!int.TryParse(splitted[1], out int k))
        {
            try
            {
                duetVals.Add(splitted[1], 0);
            }
            catch (ArgumentException)
            {

            }
        }
        double jgzVal = 0;
        double x = 0;
        if (splitted[0].Equals("jgz"))
        {
            if (!double.TryParse(splitted[1], out x))
            {
                jgzVal = duetVals[splitted[1]];
            }
            else
            {
                jgzVal = x;
            }
        }

        double value = 0;
        double z = 0;
        if (splitted.Length > 2)
        {
            if (!double.TryParse(splitted[2], out z))
            {
                value = duetVals[splitted[2]];
            }
            else
            {
                value = z;
            }
        }
        else
        {
            if (!double.TryParse(splitted[1], out z))
            {
                value = duetVals[splitted[1]];
            }
            else
            {
                value = z;
            }
        }



        switch (splitted[0])
        {
            case "snd":
                lastPlayed = value;
                break;

            case "set":
                duetVals[splitted[1]] = value;
                break;
            case "add":
                duetVals[splitted[1]] += value;
                break;
            case "mul":
                duetVals[splitted[1]] *= value;
                break;
            case "mod":
                if (value != 0)
                {
                    duetVals[splitted[1]] %= value;
                }
                break;
            case "rcv":
                if (value != 0)
                {
                    rcv = true;
                }
                break;
            case "jgz":
                if (jgzVal > 0)
                {
                    i += Convert.ToInt32(value);
                    i--;
                }
                break;
        }

    }
    Debug.WriteLine("Last played: " + lastPlayed);
}


public static async Task Day_18_2Async()
{
    var completedTask = await Task.WhenAny(
        Program1Async(),
        Program2Async());
    await completedTask;
}

public static List program1Receive = new List();
public static List program2Receive = new List();
public static int counter = 0;

private async static Task Program1Async()
{

    string path = @"C:\Users\arthu\Documents\day18.txt";
    List duet = new List();
    Dictionary duetVals = new Dictionary
            {
                { "i", 0 },
                { "a", 0 },
                { "b", 0 },
                { "f", 0 }
            };
    duet.AddRange(File.ReadAllLines(path));
    duetVals.Add("p", 0);
    double lastPlayed = 0;
    for (int i = 0; i < duet.Count(); i++)
    {
        string[] splitted = duet[i].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);

        double jgzVal = 0;
        double x = 0;
        if (splitted[0].Equals("jgz"))
        {
            if (!double.TryParse(splitted[1], out x))
            {
                jgzVal = duetVals[splitted[1]];
            }
            else
            {
                jgzVal = x;
            }
        }

        double value = 0;
        double z = 0;
        if (splitted.Length > 2)
        {
            if (!double.TryParse(splitted[2], out z))
            {
                value = duetVals[splitted[2]];
            }
            else
            {
                value = z;
            }
        }
        else
        {
            if (!double.TryParse(splitted[1], out z))
            {
                value = duetVals[splitted[1]];
            }
            else
            {
                value = z;
            }
        }



        switch (splitted[0])
        {
            case "snd":
                program2Receive.Add(value);
                break;

            case "set":
                duetVals[splitted[1]] = value;
                break;
            case "add":
                duetVals[splitted[1]] += value;
                break;
            case "mul":
                duetVals[splitted[1]] *= value;
                break;
            case "mod":
                if (value != 0)
                {
                    duetVals[splitted[1]] %= value;
                }
                break;
            case "rcv":
                int waiting = 0;
                while (program1Receive.Count() == 0)
                {
                    await Task.Delay(100);
                    waiting++;
                }
                duetVals[splitted[1]] = program1Receive[0];
                program1Receive.RemoveAt(0);
                break;
            case "jgz":
                if (jgzVal > 0)
                {
                    i += Convert.ToInt32(value);
                    i--;
                }
                break;
        }

    }
    Debug.WriteLine("Last played: " + lastPlayed);
}
private async static Task Program2Async()
{
    bool deadlock = false;
    string path = @"C:\Users\arthu\Documents\day18.txt";
    List duet = new List();
    Dictionary duetVals = new Dictionary
            {
                { "i", 0 },
                { "a", 0 },
                { "b", 0 },
                { "f", 0 }
            };
    duet.AddRange(File.ReadAllLines(path));
    duetVals.Add("p", 1);
    double lastPlayed = 0;
    for (int i = 0; i < duet.Count(); i++)
    {
        if (deadlock)
        {
            Debug.WriteLine("stopped");
            return;
        }
        string[] splitted = duet[i].Split(new char[0], StringSplitOptions.RemoveEmptyEntries);


        double jgzVal = 0;
        double x = 0;
        if (splitted[0].Equals("jgz"))
        {
            if (!double.TryParse(splitted[1], out x))
            {
                jgzVal = duetVals[splitted[1]];
            }
            else
            {
                jgzVal = x;
            }
        }

        double value = 0;
        double z = 0;
        if (splitted.Length > 2)
        {
            if (!double.TryParse(splitted[2], out z))
            {
                value = duetVals[splitted[2]];
            }
            else
            {
                value = z;
            }
        }
        else
        {
            if (!double.TryParse(splitted[1], out z))
            {
                value = duetVals[splitted[1]];
            }
            else
            {
                value = z;
            }
        }



        switch (splitted[0])
        {
            case "snd":
                program1Receive.Add(value);
                counter++;
                break;

            case "set":
                duetVals[splitted[1]] = value;
                break;
            case "add":
                duetVals[splitted[1]] += value;
                break;
            case "mul":
                duetVals[splitted[1]] *= value;
                break;
            case "mod":
                if (value != 0)
                {
                    duetVals[splitted[1]] %= value;
                }
                break;
            case "rcv":
                int waiting = 0;
                while (program2Receive.Count() == 0)
                {
                    if (waiting == 20)
                    {
                        deadlock = true;
                        Debug.WriteLine("Times send: " + counter);
                        Debug.WriteLine("program2 finished");
                        return;
                    }
                    await Task.Delay(100);
                    waiting++;
                }
                duetVals[splitted[1]] = program2Receive[0];
                program2Receive.RemoveAt(0);
                break;
            case "jgz":
                if (jgzVal > 0)
                {
                    i += Convert.ToInt32(value);
                    i--;
                }
                break;
        }

    }
    Debug.WriteLine("Last played: " + lastPlayed);
}
Open volledig
const byte up = 1;
const byte right = 2;
const byte down = 3;
const byte left = 4;
const char vert = '|';
const char horz = '-';
const char corner = '+';
const char space = ' ';
static int vertIndex;
static int horzIndex;
static void Day_19()
{
    string path = @"D:\day19.txt";
    List> diagram = new List>();
    HashSet letters = new HashSet();
    

    foreach(string line in File.ReadLines(path))
    {
        diagram.Add(line.ToList());
    }
    horzIndex = diagram[0].IndexOf(vert);
    vertIndex = 0;
    char current = vert;
    char next = space;
    int direction = down;
    int steps = -1;
    while (true)
    {
        steps++;
        if ((direction == down || direction == up) && current == horz)
        {
            if (direction == down)
                current = diagram[++vertIndex][horzIndex];
            else
                current = diagram[--vertIndex][horzIndex];
            steps++;
        }
        else if ((direction == left || direction == right) && current == vert)
        {
            if (direction == right)
                current = diagram[vertIndex][++horzIndex];
            else
                current = diagram[vertIndex][--horzIndex];
            steps++;
        }
        switch (current)
        {
            case vert:
                current = MoveVert(direction, current, diagram);
                break;
            case horz:
                current = MoveHorz(direction, current, diagram);
                break;
            case corner:
                direction = MoveCorner(direction, current, diagram);
                current = diagram[vertIndex][horzIndex];
                break;
            case space:
                Debug.WriteLine("Letters seen: " + string.Join("", letters));
                Debug.WriteLine("Number of steps: " + steps);
                return;
            default:
                if (char.IsLetter(current))
                {
                    letters.Add(current);
                    if (direction == up || direction == down)
                        current = MoveVert(direction, current, diagram);
                    else
                        current = MoveHorz(direction, current, diagram);
                }
                break;
        }
    }
}
static char MoveVert(int direction, char current, List> diagram)
{
    if (direction == up)
        current = diagram[--vertIndex][horzIndex];
    else
        current = diagram[++vertIndex][horzIndex];

    return current;
}
static char MoveHorz(int direction, char current, List> diagram)
{
    if (direction == left)
        current = diagram[vertIndex][--horzIndex];
    else
        current = diagram[vertIndex][++horzIndex];

    return current;
}
static int MoveCorner(int direction, char current, List> diagram)
{

    if (direction == up || direction == down)
    {
        direction = left;
        horzIndex--;
        if (diagram[vertIndex][horzIndex + 2] == horz)
        {
            direction = right;
            horzIndex += 2;
        }
    }
    else
    {
        direction = up;
        vertIndex--;
        if(diagram[vertIndex + 2][horzIndex] == vert)
        {
            direction = down;
            vertIndex += 2;
        }
    }
    return direction;
}
Open volledig
public static void Day_20()
{
    string path = @"C:\Users\arthu\Documents\day20.txt";
    List particles = new List();
    foreach(string line in File.ReadLines(path))
    {
        char[] seperators = new char[] { '<', '>', 'p', 'a', 'v', '=' };
        string subLine = String.Join("", line.Split(seperators, StringSplitOptions.RemoveEmptyEntries));
        double[] parts = subLine.Split(',').Select(x => double.Parse(x)).ToArray();

        particles.Add(new Day20List
        {
            Position = Tuple.Create(parts[0], parts[1], parts[2]),
            Velocity = Tuple.Create(parts[3], parts[4], parts[5]),
            Acceleration = Tuple.Create(parts[6], parts[7], parts[8])
        });
    }
    
    int counter = 0;
    HashSet> checkCollide = new HashSet>();
    List> remove = new List>();
    while (true)
    {
        checkCollide.Clear();
        remove.Clear();
        foreach(var particle in particles)
        {
            particle.Velocity = Tuple.Create(
                particle.Velocity.Item1 + particle.Acceleration.Item1,
                particle.Velocity.Item2 + particle.Acceleration.Item2,
                particle.Velocity.Item3 + particle.Acceleration.Item3);
            particle.Position = Tuple.Create(
                particle.Position.Item1 + particle.Velocity.Item1, 
                particle.Position.Item2 + particle.Velocity.Item2, 
                particle.Position.Item3 + particle.Velocity.Item3);

            particle.distances.Add(Math.Abs(particle.Position.Item1) + Math.Abs(particle.Position.Item2) + Math.Abs(particle.Position.Item3));

            if (particle.distances.Count() >= 100)
            {
                particle.distances.RemoveAt(0);
            }
            
        }
        foreach(var particle in particles)
        {
            if (!checkCollide.Add(particle.Position))
            {
                remove.Add(particle.Position);
            }
        }
        foreach(var element in remove)
        {
            particles.RemoveAll(x => x.Position.Equals(element));
        }
        if (counter == 10000)
        {
            counter = 0;
            Debug.WriteLine("Part1: Closest to 0 over long time: " + particles.IndexOf(particles.MinBy(x => x.distances.Sum())));
            Debug.WriteLine("Part2: Particles left: " + particles.Count());
        }
        counter++;
        
    }
}

public class Day20List
{
    public Tuple Position;
    public Tuple Velocity;
    public Tuple Acceleration;
    public List distances = new List();
}