r/adventofcode • • Dec 08 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 8 Solutions -🎄-

--- Day 8: Memory Maneuver ---


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 8

Sigh, imgur broke again. Will upload when it unborks.

Transcript:

The hottest programming book this year is "___ For Dummies".


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:12:10!

32 Upvotes

301 comments sorted by

View all comments

1

u/keithnicholasnz Dec 08 '18

C#

only evil I did was the Total :) which I could get rid of now.... but I committed the shameful act so I will own it :)

namespace AdventOfCode
{
    public class DNode
    {
        public DNode Parent { get; set; }
        public List<DNode> Children { get; set; } = new List<DNode>();
        public List<int> MetaData { get; set; } = new List<int>();
        public int QuantityChild { get; set; }
        public int QuantityMeta { get; set; }

        public DNode Add(DNode child)
        {
            Children.Add(child);
            child.Parent = this;
            return this;
        }

        public int Value()
        {
            if (!Children.Any())
            {
                return MetaData.Sum();
            }
            else
            {
                int value = 0;
                foreach (var index in MetaData)
                {
                    if (index - 1 < Children.Count)
                    {
                        value += Children[index-1].Value();
                    }
                }
                return value;
            }
        }
    }

    public class Day8
    {
        private static int Total =0;
        public void Go()
        {
            var data = File.ReadAllText("Day8.txt").Split(" ").Select(v => int.Parse(v.Trim())).ToList();

            var head = Translate(data);

            Console.WriteLine(Total);
            Console.WriteLine(head.Value());
        }

        private DNode Translate(List<int> data)
        {
            DNode node = new DNode()
            {
                QuantityChild = data[0],
                QuantityMeta = data[1]
            };
            data.RemoveRange(0,2);
            for (int i = 0; i < node.QuantityChild; i++)
            {
                node.Add(Translate(data));
            }

            for (int m = 0; m < node.QuantityMeta; m++)
            {
                node.MetaData.Add(data[0]);
                data.RemoveAt(0);
            }

            Total += node.MetaData.Sum();

            return node;
        }
    }
}