SteveDinn

joined 2 years ago
[–] [email protected] 2 points 2 years ago

Thanks for that state machine. I was a bit lost in part 2, but after reading this, it totally makes sense.

[–] [email protected] 2 points 2 years ago* (last edited 2 years ago)

Snippet:

        static void Part1(string data)
        {
            var result = ParseInput(data)
                .Select(history => ProcessHistory(history, g => g.Length - 1, (a, b) => a + b))
                .Sum();

            Console.WriteLine(result);
        }

        static void Part2(string data)
        {
            var result = ParseInput(data)
                .Select(history => ProcessHistory(history, g => 0, (a, b) => a - b))
                .Sum();

            Console.WriteLine(result);
        }

        static int ProcessHistory(
            int[] history,
            Func guessIndex,
            Func collateGuess)
        {
            bool allZeros = true;

            var diffs = new int[history.Length - 1];
            for (int i = 0; i < diffs.Length; i++)
            {
                var diff = history[i + 1] - history[i];
                diffs[i] = diff;
                allZeros = allZeros && (diff == 0);
            }

            var guess = history[guessIndex(history)];

            if (!allZeros)
            {
                guess = collateGuess(
                    guess,
                    ProcessHistory(diffs, guessIndex, collateGuess));
            }

            return guess;
        }
[–] [email protected] 2 points 2 years ago (1 children)

C#

Used recursion to determine the differences for part 1 and then extracted the variations in processing from predicting the end vs. the beginning of the history and passed them in as Func variables to the recursive method.

Day 9

[–] [email protected] 2 points 2 years ago* (last edited 2 years ago) (1 children)

I barely registered a difference between part 1 and part 2.

Part 1: 00:00:00.0018302
Part 2: 00:00:00.0073136

I suppose it took about 3.5 times as long, but I didn't notice :P

Edit: I realize that I made the implicit assumption in my solution that it doesn't make sense to have multiple jokers be interpreted as different values. i.e., The hand with the maximum value will have all Jokers interpreted as the same other card. I think that is true though. It worked out for me anyway.

[–] [email protected] 2 points 2 years ago* (last edited 2 years ago) (1 children)

Wow, this is exactly what I did, but in C#. That's cool.

    public class Hand
    {
        public string Cards;
        public int Rank;
        public int Bid;
    }

    public static HandType IdentifyHandType(string hand)
    {
        var cardCounts = hand
            .Aggregate(
                new Dictionary(),
                (counts, card) => 
                {
                    counts[card] = counts.TryGetValue(card, out var count) ? (count + 1) : 1;
                    return counts;
                })
            .OrderByDescending(kvp => kvp.Value);

        using (var cardCount = cardCounts.GetEnumerator())
        {
            cardCount.MoveNext();
            switch (cardCount.Current.Value)
            {
                case 5: return HandType.FiveOfAKind;
                case 4: return HandType.FourOfAKind;
                case 3: { cardCount.MoveNext(); return (cardCount.Current.Value == 2) ? HandType.FullHouse : HandType.ThreeOfAKind; }
                case 2: { cardCount.MoveNext(); return (cardCount.Current.Value == 2) ? HandType.TwoPairs : HandType.OnePair; }
            }
        }

        return HandType.HighCard;
    }

    public static Hand SetHandRank(Hand hand, Dictionary cardValues)
    {
        int rank = 0;
        int offset = 0;

        var cardValueHand = hand.Cards;
        for (int i = cardValueHand.Length - 1; i >= 0; i--)
        {
            var card = cardValueHand[i];
            var cardValue = cardValues[card];
            var offsetCardValue = cardValue << offset;
            rank |= offsetCardValue;
            offset += 4; // To store values up to 13 we need 4 bits.
        }

        // Put the hand type at the high end because it is the most
        // important factor in the rank.
        var handType = (int)IdentifyHandType(hand.Cards);
        var offsetHandType = handType << offset;
        rank |= offsetHandType;

        hand.Rank = rank;
        return hand;
    }
[–] [email protected] 5 points 2 years ago (1 children)

My first version ran for about 90 minutes. My second version, for about 45 seconds. I'm sure there's more optimization to be done, but that was good enough for me :)

[–] [email protected] 2 points 2 years ago

Also worked for the Commodore 64 and Vic20.

[–] [email protected] 2 points 2 years ago* (last edited 2 years ago)

I have investigated many, many subsonic compatible apps for one as full featured as DSub. Tempo looks great, but does it have Chromecast and Android Auto clients? Those are both necessities for me.

The fact that DSub has all of these and hasn't really been in active development for years speaks volumes about its quality.

I will keep a close eye on Tempo, though!

[–] [email protected] 2 points 2 years ago (1 children)

Looks creepy AF in both photos.

[–] [email protected] 35 points 2 years ago (1 children)

Facebook can get fucked. They offer even less to me than YouTube.

[–] [email protected] 8 points 2 years ago

I heard she was also the drummer for Sex Bob-omb.

view more: ‹ prev next ›