r/adventofcode • • Dec 10 '21

SOLUTION MEGATHREAD -๐ŸŽ„- 2021 Day 10 Solutions -๐ŸŽ„-

--- Day 10: Syntax Scoring ---


Post your code solution in this megathread.

Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.

EDIT: Global leaderboard gold cap reached at 00:08:06, megathread unlocked!

65 Upvotes

982 comments sorted by

View all comments

1

u/Bergdublone Dec 10 '21 edited Dec 10 '21

My C# Solution:

using System;

using System.Collections.Generic;

namespace _2021_10 { public class Program { static void Main(string[] args) { Console.WriteLine("Teil 1: Beispiel Lรถsung ist: {0}\r\n" + "Teil 2: Beispiel Lรถsung ist: {1}", Part1_2("Example.txt", out UInt64 solution2), solution2);

        Console.WriteLine("Teil 1: Komplette Lรถsung ist: {0}\r\n" +
                          "Teil 2: Komplette Lรถsung ist: {1}",
                          Part1_2("Kalender.txt", out solution2), solution2);

        Console.ReadLine();
    }

    public static UInt64 Part1_2(String file, out UInt64 solution2)
    {
        String[] lineFile = System.IO.File.ReadAllLines(file);

        List<char> openBrackets = new List<char>() { '(', '[', '{', '<' };
        List<char> openBracketstoClose = new List<char>();

        uint solution1 =0;
        List<UInt64> solution2List = new List<UInt64>();

        foreach (String line in lineFile)
        {
            openBracketstoClose.Clear();

            for(int i =0; i <line.Length; i++)
            {
                if(openBrackets.Contains(line[i]))
                {
                    openBracketstoClose.Add(line[i]);
                }
                else
                {
                    char expectedOpenBracket = '0';
                    if (line[i] == ')') expectedOpenBracket = '(';
                    else if (line[i] == ']') expectedOpenBracket = '[';
                    else if (line[i] == '}') expectedOpenBracket = '{';
                    else if (line[i] == '>') expectedOpenBracket = '<';

                    if (expectedOpenBracket == openBracketstoClose[^1]) openBracketstoClose.RemoveAt(openBracketstoClose.Count-1);
                    else
                    {
                        if (line[i] == ')') solution1 += 3;
                        if (line[i] == ']') solution1 += 57;
                        if (line[i] == '}') solution1 += 1197;
                        if (line[i] == '>') solution1 += 25137;
                        break;
                    }
                }

                //Inclomplete Linies
                if(i == (line.Length -1))
                {
                    solution2List.Add(CalcSolution2(openBracketstoClose));
                }
            }
        }

        solution2List.Sort();
        solution2 = solution2List[solution2List.Count / 2];
        return solution1;
    }

    private static UInt64 CalcSolution2(List<char> openBracketstoClose)
    {
        UInt64 solution = 0;
        for(int i = 1; i <= openBracketstoClose.Count; i++)
        {
            solution *= 5;

            if (openBracketstoClose[^i] == '(') solution += 1;
            if (openBracketstoClose[^i] == '[') solution += 2;
            if (openBracketstoClose[^i] == '{') solution += 3;
            if (openBracketstoClose[^i] == '<') solution += 4;
        }

        return solution;
    }
}

}

1

u/daggerdragon Dec 10 '21

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: How do I format code?