this post was submitted on 07 Dec 2023
18 points (100.0% liked)

Advent Of Code

772 readers
8 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 2023

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
18
submitted 11 months ago* (last edited 11 months ago) by Ategon to c/advent_of_code
 

Day 7: Camel Cards

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/ , pastebin, or github (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


๐Ÿ”’ Thread is locked until there's at least 100 2 star entries on the global leaderboard

๐Ÿ”“ Thread has been unlocked after around 20 mins

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 2 points 11 months ago

C#

Not too bad - I just scored every hand for the first part so I could easily sort it.

For the second part I just brute forced the replacements for the hand type matchinge (first digit of score)

Task1public class Day7Task1:IRunnable {

public static Dictionary CardValues = new Dictionary()
 {
     { '2', "01" },
     { '3', "02" },
     { '4', "03" },
     { '5', "04" },
     { '6', "05" },
     { '7', "06" },
     { '8', "07" },
     { '9', "08" },
     { 'T', "09" },
     { 'J', "10" },
     { 'Q', "11" },
     { 'K', "12" },
     { 'A', "13" }
 };
 
 public void Run()
 {
     //var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt");
     var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt");



     var hands = inputLines.Select(line =>
     {
         var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
         return new Hand(split[0],  split[1] );
     }).ToList();

     var sortedHands = hands.OrderBy(hand => hand.Score).ToList();

     long resultValue = 0;

     for (int i = 1; i < hands.Count()+1; i++)
     {
         resultValue += i * sortedHands[i-1].Bid;
     }

     Console.WriteLine("Result:" + resultValue);

 }

 public class Hand
 {
     public Hand(string cards, string bid)
     {
         Cards = cards;
         Bid = int.Parse(bid);
         Score = GenerateScore();
     }

     public string Cards { get; set; }
     public int Bid { get; set; }
     
     public long Score { get; }

     private long GenerateScore()
     {
         var resultString = new StringBuilder();
         var cardGroups = Cards.GroupBy(c => c).ToList();
         var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList();
         if (cardGroups.Count() == 1)
         {
             resultString.Append("7");
         }
         else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1))
         {
             resultString.Append("6");
         }
         else if(cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2))
         {
             resultString.Append("5");
         }
         else if(cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3))
         {
             resultString.Append("4");
         }
         else if(cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1)
         {
             resultString.Append("3");
         }
         else if(cardGroups.Count() == 4 )
         {
             resultString.Append("2");
         }
         else
         {
             resultString.Append("1");
         }

         foreach (var card in Cards)
         {
             resultString.Append(Day7Task1.CardValues[card]);
         }

         Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString);
         return long.Parse(resultString.ToString());
     }
 }
}

Task2

public class Day7Task2:IRunnable
{
    public static Dictionary CardValues = new Dictionary()
    {
        { '2', "01" },
        { '3', "02" },
        { '4', "03" },
        { '5', "04" },
        { '6', "05" },
        { '7', "06" },
        { '8', "07" },
        { '9', "08" },
        { 'T', "09" },
        { 'J', "00" },
        { 'Q', "11" },
        { 'K', "12" },
        { 'A', "13" }
    };
    
    public void Run()
    {
        //var inputLines = File.ReadAllLines("Days/Seven/Day7ExampleInput.txt");
        var inputLines = File.ReadAllLines("Days/Seven/Day7Input.txt");



        var hands = inputLines.Select(line =>
        {
            var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
            return new Hand(split[0],  split[1] );
        }).ToList();

        var sortedHands = hands.OrderBy(hand => hand.Score).ToList();

        long resultValue = 0;

        for (int i = 1; i < hands.Count()+1; i++)
        {
            resultValue += i * sortedHands[i-1].Bid;
        }

        Console.WriteLine("Result:" + resultValue);

    }

    public class Hand
    {
        public Hand(string cards, string bid)
        {
            Cards = cards;
            Bid = int.Parse(bid);
            Score = GenerateScore();
        }

        public string Cards { get; set; }
        public int Bid { get; set; }
        
        public long Score { get; }

        private long GenerateScore()
        {
            var generateFirstDigit = new Func(cards =>
            {
                var cardGroups = cards.GroupBy(c => c).ToList();
                var groupCounts = cardGroups.OrderByDescending(g => g.Count()).Select(g => g.Count()).ToList();
                if (cardGroups.Count() == 1)
                {
                    return 7;
                }
                else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 4 || cardGroups[0].Count() == 1))
                {
                    return 6;
                }
                else if (cardGroups.Count() == 2 && (cardGroups[0].Count() == 3 || cardGroups[0].Count() == 2))
                {
                    return 5;
                }
                else if (cardGroups.Count() == 3 && (cardGroups[0].Count() == 3 || cardGroups[1].Count() == 3 || cardGroups[2].Count() == 3))
                {
                    return 4;
                }
                else if (cardGroups.Count() == 3 && groupCounts[0] == 2 && groupCounts[1] == 2 && groupCounts[2] == 1)
                {
                    return 3;
                }
                else if (cardGroups.Count() == 4)
                {
                    return 2;
                }
                else
                {
                    return 1;
                }
            });
            
            var resultString = new StringBuilder();

            var maxFistDigit = Day7Task2.CardValues.Keys.Select(card => generateFirstDigit(Cards.Replace('J', card))).Max();

            resultString.Append(maxFistDigit);
            
            foreach (var card in Cards)
            {
                resultString.Append(Day7Task2.CardValues[card]);
            }

            Console.WriteLine("Cards:{0} Score:{1}",Cards,resultString);
            return long.Parse(resultString.ToString());
        }
    }
}