this post was submitted on 02 Dec 2023
23 points (96.0% liked)

Advent Of Code

985 readers
75 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 2: Cube Conundrum


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


πŸ”’This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

πŸ”“ Edit: Post has been unlocked after 6 minutes

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 3 points 1 year ago* (last edited 1 year ago)

Done in C# Input parsing done with a mixture of splits and Regex (no idea why everyone hates it?) capture groups.

I have overbuilt for both days, but not tripped on any of the 'traps' in the input data - generally expecting the input to be worse than it is... too used to actual data from users

Input Parsing (common)

public class Day2RoundInput { private Regex gameNumRegex = new Regex("[a-z]* ([0-9]*)", RegexOptions.IgnoreCase);

    public Day2RoundInput(string gameString)
    {
        var colonSplit = gameString.Trim().Split(':', StringSplitOptions.RemoveEmptyEntries);
        var match = gameNumRegex.Match(colonSplit[0].Trim());
        var gameNumberString = match.Groups[1].Value;
        GameNumber = int.Parse(gameNumberString.Trim());

        HandfulsOfCubes = new List();
        var roundsSplit = colonSplit[1].Trim().Split(';', StringSplitOptions.RemoveEmptyEntries);
        foreach (var round in roundsSplit)
        {
            HandfulsOfCubes.Add(new HandfulCubes(round));
        }
    }
    public int GameNumber { get; set; }

    public List HandfulsOfCubes { get; set; }

    public class HandfulCubes
    {
        private Regex colourRegex = new Regex("([0-9]*) (red|green|blue)");

        public HandfulCubes(string roundString)
        {
            var colourCounts = roundString.Split(',', StringSplitOptions.RemoveEmptyEntries);
            foreach (var colour in colourCounts)
            {
                var matches = colourRegex.Matches(colour.Trim());

                foreach (Match match in matches)
                {
                    var captureOne = match.Groups[1];

                    var count = int.Parse(captureOne.Value.Trim());

                    var captureTwo = match.Groups[2];

                    switch (captureTwo.Value.Trim().ToLower())
                    {
                        case "red":
                            RedCount = count;
                            break;
                        case "green":
                            GreenCount = count;
                            break;
                        case "blue":
                            BlueCount = count;
                            break;
                        default: throw new Exception("uh oh");
                    }
                }
            }
        }

        public int RedCount { get; set; }
        public int GreenCount { get; set; }
        public int BlueCount { get; set; }
    }

}

Task1internal class Day2Task1:IRunnable { public void Run() { var inputs = GetInputs();

        var maxAllowedRed = 12;
        var maxAllowedGreen = 13;
        var maxAllowedBlue = 14;

        var allowedGameIdSum = 0;

        foreach ( var game in inputs ) { 
            var maxRed = game.HandfulsOfCubes.Select(h => h.RedCount).Max();
            var maxGreen = game.HandfulsOfCubes.Select(h => h.GreenCount).Max();
            var maxBlue = game.HandfulsOfCubes.Select(h => h.BlueCount).Max();

            if ( maxRed <= maxAllowedRed && maxGreen <= maxAllowedGreen && maxBlue <= maxAllowedBlue) 
            {
                allowedGameIdSum += game.GameNumber;
                Console.WriteLine("Game:" + game.GameNumber + " allowed");
            }
            else
            {
                Console.WriteLine("Game:" + game.GameNumber + "not allowed");
            }
        }

        Console.WriteLine("Sum:" + allowedGameIdSum.ToString());

    }

    private List GetInputs()
    {
        List inputs = new List();

        var textLines = File.ReadAllLines("Days/Two/Day2Input.txt");

        foreach (var line in textLines)
        {
            inputs.Add(new Day2RoundInput(line));
        }

        return inputs;
    }

    
}

Task2internal class Day2Task2:IRunnable { public void Run() { var inputs = GetInputs();

        var result = 0;

        foreach ( var game in inputs ) {
            var maxRed = game.HandfulsOfCubes.Select(h => h.RedCount).Max();
            var maxGreen = game.HandfulsOfCubes.Select(h => h.GreenCount).Max();
            var maxBlue = game.HandfulsOfCubes.Select(h => h.BlueCount).Max();

            var power = maxRed*maxGreen*maxBlue;
            Console.WriteLine("Game:" + game.GameNumber + " Result:" + power.ToString());

            result += power;
        }

        Console.WriteLine("Day2 Task2 Result:" + result.ToString());

    }

    private List GetInputs()
    {
        List inputs = new List();

        var textLines = File.ReadAllLines("Days/Two/Day2Input.txt");
        //var textLines = File.ReadAllLines("Days/Two/Day2ExampleInput.txt");

        foreach (var line in textLines)
        {
            inputs.Add(new Day2RoundInput(line));
        }

        return inputs;
    }

    
}