Day 7
This commit is contained in:
@@ -52,6 +52,9 @@
|
|||||||
<None Remove="problems\aoc2023\day5\sample.txt" />
|
<None Remove="problems\aoc2023\day5\sample.txt" />
|
||||||
<None Remove="problems\aoc2023\day6\input.txt" />
|
<None Remove="problems\aoc2023\day6\input.txt" />
|
||||||
<None Remove="problems\aoc2023\day6\sample.txt" />
|
<None Remove="problems\aoc2023\day6\sample.txt" />
|
||||||
|
<None Remove="problems\aoc2023\day7\input.txt" />
|
||||||
|
<None Remove="problems\aoc2023\day7\sample.txt" />
|
||||||
|
<None Remove="problems\aoc2023\day7\sara.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
36
AdventOfCode/Problems/AOC2023/Day7/CamelCards.cs
Normal file
36
AdventOfCode/Problems/AOC2023/Day7/CamelCards.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using AdventOfCode.Runner.Attributes;
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode.Problems.AOC2023.Day7;
|
||||||
|
[ProblemInfo(2023, 7, "Camel Cards")]
|
||||||
|
internal class CamelCards : Problem<int, int>
|
||||||
|
{
|
||||||
|
private CamelHand[] _hands = [];
|
||||||
|
private CamelHand[] _jokerHands = [];
|
||||||
|
|
||||||
|
public override void LoadInput()
|
||||||
|
{
|
||||||
|
var data = ReadInputLines("input.txt");
|
||||||
|
_hands = data.Select(d => new CamelHand(d)).ToArray();
|
||||||
|
_jokerHands = data.Select(d => new CamelHand(d, true)).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CalculatePart1()
|
||||||
|
{
|
||||||
|
var x = _hands.Order();
|
||||||
|
Part1 = x.Select((h, i) => (i + 1) * h.Bid).Sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void CalculatePart2()
|
||||||
|
{
|
||||||
|
var x = _jokerHands.Order().Print();
|
||||||
|
//x.Where(x => x.Hand.Contains('J')).Print();
|
||||||
|
Part2 = x.Select((h, i) => (i + 1) * h.Bid).Sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
106
AdventOfCode/Problems/AOC2023/Day7/CamelHand.cs
Normal file
106
AdventOfCode/Problems/AOC2023/Day7/CamelHand.cs
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode.Problems.AOC2023.Day7;
|
||||||
|
internal class CamelHand : IComparable<CamelHand>
|
||||||
|
{
|
||||||
|
public static readonly List<char> CARDS = [ '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A' ];
|
||||||
|
public static readonly List<char> CARDS_JOKER = [ 'J', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A' ];
|
||||||
|
|
||||||
|
public string Hand { get; set; }
|
||||||
|
public int Bid { get; set; }
|
||||||
|
public int Value { get; set; }
|
||||||
|
public HandType Type { get; set; }
|
||||||
|
public CamelHand(string data, bool useJoker = false)
|
||||||
|
{
|
||||||
|
var split = data.Split(' ');
|
||||||
|
Hand = split[0];
|
||||||
|
Bid = int.Parse(split[1]);
|
||||||
|
Type = useJoker ? GetJokerHandType(Hand) : GetHandType(Hand);
|
||||||
|
Value = CalculateValue(Hand, useJoker);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int CalculateValue(string hand, bool useJoker)
|
||||||
|
{
|
||||||
|
var total = 0;
|
||||||
|
for (int i = 0; i < hand.Length; i++)
|
||||||
|
{
|
||||||
|
var p = (hand.Length - i - 1);
|
||||||
|
var v = useJoker ? CARDS_JOKER.IndexOf(hand[i]) : CARDS.IndexOf(hand[i]);
|
||||||
|
total += (v + 1) * (int)Math.Pow(13, p);
|
||||||
|
}
|
||||||
|
return total;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool IsStrongerThan(CamelHand card)
|
||||||
|
{
|
||||||
|
if (Type > card.Type)
|
||||||
|
return true;
|
||||||
|
if(Type < card.Type)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return Value >= card.Value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static HandType GetJokerHandType(string hand)
|
||||||
|
{
|
||||||
|
var type = GetHandType(hand);
|
||||||
|
if (type == HandType.FiveOfKind)
|
||||||
|
return type;
|
||||||
|
|
||||||
|
if (!hand.Contains('J'))
|
||||||
|
return type;
|
||||||
|
var bestCard = hand.GroupBy(c => c)
|
||||||
|
.OrderByDescending(c => c.Count())
|
||||||
|
.First(c => c.Key != 'J').Key;
|
||||||
|
|
||||||
|
var newHand = hand.Replace('J', bestCard);
|
||||||
|
return GetHandType(newHand);
|
||||||
|
}
|
||||||
|
private static HandType GetHandType(string hand)
|
||||||
|
{
|
||||||
|
var cardGroups = hand.GroupBy(c => c).Select(g => g.Count()).ToArray();
|
||||||
|
|
||||||
|
if (cardGroups.Length == 1)
|
||||||
|
return HandType.FiveOfKind;
|
||||||
|
if(cardGroups.Contains(4))
|
||||||
|
return HandType.FourOfKind;
|
||||||
|
if (cardGroups.Contains(3) && cardGroups.Contains(2))
|
||||||
|
return HandType.FullHouse;
|
||||||
|
if(cardGroups.Contains(3))
|
||||||
|
return HandType.ThreeOfKind;
|
||||||
|
|
||||||
|
var pairs = cardGroups.Count(c => c == 2);
|
||||||
|
if (pairs == 2)
|
||||||
|
return HandType.TwoPair;
|
||||||
|
if (pairs == 1)
|
||||||
|
return HandType.OnePair;
|
||||||
|
|
||||||
|
return HandType.HighCard;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareTo(CamelHand? other)
|
||||||
|
{
|
||||||
|
if(other == null) return 1;
|
||||||
|
return IsStrongerThan(other) ? 1 : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return $"[{Value}] {Hand}: {Type} | {Bid}";
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum HandType
|
||||||
|
{
|
||||||
|
HighCard,
|
||||||
|
OnePair,
|
||||||
|
TwoPair,
|
||||||
|
ThreeOfKind,
|
||||||
|
FullHouse,
|
||||||
|
FourOfKind,
|
||||||
|
FiveOfKind
|
||||||
|
}
|
||||||
|
}
|
||||||
1000
AdventOfCode/Problems/AOC2023/Day7/input.txt
Normal file
1000
AdventOfCode/Problems/AOC2023/Day7/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
5
AdventOfCode/Problems/AOC2023/Day7/sample.txt
Normal file
5
AdventOfCode/Problems/AOC2023/Day7/sample.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KTJJT 220
|
||||||
|
KK677 28
|
||||||
|
QQQJA 483
|
||||||
1000
AdventOfCode/Problems/AOC2023/Day7/sara.txt
Normal file
1000
AdventOfCode/Problems/AOC2023/Day7/sara.txt
Normal file
File diff suppressed because it is too large
Load Diff
18
AdventOfCode/Runner/Extensions.cs
Normal file
18
AdventOfCode/Runner/Extensions.cs
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace AdventOfCode.Runner;
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static IEnumerable<T> Print<T>(this IEnumerable<T> values)
|
||||||
|
{
|
||||||
|
foreach (var item in values)
|
||||||
|
{
|
||||||
|
Console.WriteLine(item);
|
||||||
|
}
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user