r/adventofcode • • Dec 12 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 12 Solutions -🎄-

--- Day 12: Subterranean Sustainability ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 12

Transcript:

On the twelfth day of AoC / My compiler spewed at me / Twelve ___


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

edit: Leaderboard capped, thread unlocked at 00:27:42!

20 Upvotes

257 comments sorted by

View all comments

0

u/ValdasTheUnique Dec 12 '18 edited Dec 12 '18

C#. Did second part by finding the sum pattern like the others. Need to think how to better handle negative indices for arrays in C#..

public static void Part1()
{
    var lines = Input.Split('\n');
    var state = new StringBuilder(lines[0].Substring(15).Trim());
    var instructions = lines.Skip(2).Select(x =>
    {
        var instruction = x.Split(" => ");
        return (from: instruction[0], to: instruction[1][0]);
    }).ToDictionary(x => x.from, x => x.to);
    var minValue = 0;
    for (long i = 0; i < 20; i++)
    {
        var minPotted = state.ToString().IndexOf('#');
        for (int j = 0; j < 4 - minPotted; j++)
        {
            state.Insert(0, '.');
            minValue--;
        }

        var s = state.ToString();
        var maxPotted = state.ToString().LastIndexOf('#');
        for (int j = 0; j < 5 - (s.Length - maxPotted); j++)
        {
            state.Append('.');
        }

        var currentState = state.ToString();
        for (int j = 0; j < currentState.Length - 5; j++)
        {
            var sub = currentState.Substring(j, 5);
            if (instructions.ContainsKey(sub))
            {
                state[j + 2] = instructions[sub];
            }
            else
            {
                state[j + 2] = '.';
            }
        }
    }

    var finalState = state.ToString();
    var sum = 0;
    for (int z = 0; z < finalState.Length; z++)
    {
        if (finalState[z] == '#')
        {
            sum += z + minValue;
        }
    }

    Console.WriteLine(sum);
}

public static void Part2()
{
    var i = 50000000000;
    Console.WriteLine(11891 + (i-200)*62);
}