Implemented Part 7 part 1 and 2 and their tests.

This commit is contained in:
Jean-Francois Gilsdorf 2023-12-13 21:44:08 +01:00
parent a5a47fb773
commit 7f02586e94
7 changed files with 1218 additions and 2 deletions

View File

@ -14,6 +14,7 @@
<None Remove="Inputs\Day4.txt" /> <None Remove="Inputs\Day4.txt" />
<None Remove="Inputs\Day5.txt" /> <None Remove="Inputs\Day5.txt" />
<None Remove="Inputs\Day6.txt" /> <None Remove="Inputs\Day6.txt" />
<None Remove="Inputs\Day7.txt" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -23,6 +24,7 @@
<EmbeddedResource Include="Inputs\Day4.txt" /> <EmbeddedResource Include="Inputs\Day4.txt" />
<EmbeddedResource Include="Inputs\Day5.txt" /> <EmbeddedResource Include="Inputs\Day5.txt" />
<EmbeddedResource Include="Inputs\Day6.txt" /> <EmbeddedResource Include="Inputs\Day6.txt" />
<EmbeddedResource Include="Inputs\Day7.txt" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,93 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCode2023.Day7
{
public class Day7_1 : DayBase
{
string input = string.Empty;
Regex rxHand = new Regex(@"^(?<hand>[\d\w]{5})\s*(?<bid>\d+)", RegexOptions.Multiline);
public Day7_1()
{
input = GetInput("Day7.txt");
}
public Day7_1(string testInput)
{
input = testInput;
}
public override string Execute()
{
var hands = rxHand.Matches(input)
.Select(x => new Hand_Day1(x.Groups["hand"].Value, int.Parse(x.Groups["bid"].Value)))
.OrderBy(x => x.GetHandStrength())
.ThenBy(x => x.Cards[0])
.ThenBy(x => x.Cards[1])
.ThenBy(x => x.Cards[2])
.ThenBy(x => x.Cards[3])
.ThenBy(x => x.Cards[4]);
return hands.Select((hand, index) => hand.Bid * (index + 1)).Sum().ToString();
}
}
internal class Hand_Day1
{
private string _hand;
private int[] _cards;
private int _bid;
public Hand_Day1(string hand, int bid)
{
_hand = hand;
_cards = hand.Select(CardToInt).ToArray();
_bid = bid;
}
public int GetHandStrength()
{
var cardGroups = _hand.GroupBy(x => x).OrderByDescending(x => x.Count());
return (cardGroups.FirstOrDefault()?.Count() ?? 0) switch
{
5 => 6,
4 => 5,
3 => cardGroups.ElementAtOrDefault(1)?.Count() == 2 ? 4 : 3,
2 => cardGroups.ElementAtOrDefault(1)?.Count() == 2 ? 2 : 1,
1 => 0,
_ => throw new ArgumentException($"Too many or no cards found."),
};
}
private static int CardToInt(char x)
{
return x switch
{
'A' => 12,
'K' => 11,
'Q' => 10,
'J' => 9,
'T' => 8,
'9' => 7,
'8' => 6,
'7' => 5,
'6' => 4,
'5' => 3,
'4' => 2,
'3' => 1,
'2' => 0,
_ => throw new ArgumentException($"Can't convert {x} to int."),
};
}
public int[] Cards { get => _cards; }
public int Bid { get => _bid; }
}
}

View File

@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace AdventOfCode2023.Day7
{
public class Day7_2 : DayBase
{
string input = string.Empty;
Regex rxHand = new Regex(@"^(?<hand>[\d\w]{5})\s*(?<bid>\d+)", RegexOptions.Multiline);
public Day7_2()
{
input = GetInput("Day7.txt");
}
public Day7_2(string testInput)
{
input = testInput;
}
public override string Execute()
{
var hands = rxHand.Matches(input)
.Select(x => new Hand_Day2(x.Groups["hand"].Value, int.Parse(x.Groups["bid"].Value)))
.OrderBy(x => x.GetHandStrength())
.ThenBy(x => x.Cards[0])
.ThenBy(x => x.Cards[1])
.ThenBy(x => x.Cards[2])
.ThenBy(x => x.Cards[3])
.ThenBy(x => x.Cards[4]);
return hands.Select((hand, index) => hand.Bid * (index + 1)).Sum().ToString();
}
}
internal class Hand_Day2
{
private string _hand;
private int[] _cards;
private int _bid;
public Hand_Day2(string hand, int bid)
{
_hand = hand;
_cards = hand.Select(CardToInt).ToArray();
_bid = bid;
}
public int GetHandStrength()
{
var cardGroups = _hand.GroupBy(x => x).OrderByDescending(x => x.Count());
var jokers = cardGroups.FirstOrDefault(x => x.Key == 'J')?.Count() ?? 0;
var noJokerCardGroups = cardGroups;
if (jokers > 0)
{
noJokerCardGroups = cardGroups.Where(x => x.Key != 'J').OrderByDescending(x => x.Count());
}
return (noJokerCardGroups.FirstOrDefault()?.Count() + jokers ?? 0 + jokers) switch
{
5 => 6,
4 => 5,
3 => noJokerCardGroups.ElementAtOrDefault(1)?.Count() == 2 ? 4 : 3,
2 => noJokerCardGroups.ElementAtOrDefault(1)?.Count() == 2 ? 2 : 1,
1 => 0,
_ => throw new ArgumentException($"Too many or no cards found."),
};
}
private static int CardToInt(char x)
{
return x switch
{
'A' => 12,
'K' => 11,
'Q' => 10,
'T' => 9,
'9' => 8,
'8' => 7,
'7' => 6,
'6' => 5,
'5' => 4,
'4' => 3,
'3' => 2,
'2' => 1,
'J' => 0,
_ => throw new ArgumentException($"Can't convert {x} to int."),
};
}
public int[] Cards { get => _cards; }
public int Bid { get => _bid; }
}
}

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@ using AdventOfCode2023.Day3;
using AdventOfCode2023.Day4; using AdventOfCode2023.Day4;
using AdventOfCode2023.Day5; using AdventOfCode2023.Day5;
using AdventOfCode2023.Day6; using AdventOfCode2023.Day6;
using AdventOfCode2023.Day7;
namespace AdventOfCode2023 namespace AdventOfCode2023
{ {
@ -22,6 +23,8 @@ namespace AdventOfCode2023
new Day5_2(), new Day5_2(),
new Day6_1(), new Day6_1(),
new Day6_2(), new Day6_2(),
new Day7_1(),
new Day7_2(),
}; };
private static void ListDays() private static void ListDays()

View File

@ -125,7 +125,7 @@ namespace AdventOfCode2023Tests
#endregion #endregion
#region Day 6 #region Day6
[Test] [Test]
public void Day6_1Test() public void Day6_1Test()
@ -142,5 +142,23 @@ namespace AdventOfCode2023Tests
} }
#endregion #endregion
#region Day7
[Test]
public void Day7_1Test()
{
var day7_1 = new Day7_1("32T3K 765\r\nT55J5 684\r\nKK677 28\r\nKTJJT 220\r\nQQQJA 483\r\n");
Assert.That(day7_1.Execute(), Is.EqualTo("6440"));
}
[Test]
public void Day7_21Test()
{
var day7_2 = new Day7_2("32T3K 765\r\nT55J5 684\r\nKK677 28\r\nKTJJT 220\r\nQQQJA 483\r\n");
Assert.That(day7_2.Execute(), Is.EqualTo("5905"));
}
#endregion
} }
} }

View File

@ -4,4 +4,5 @@ global using AdventOfCode2023.Day2;
global using AdventOfCode2023.Day3; global using AdventOfCode2023.Day3;
global using AdventOfCode2023.Day4; global using AdventOfCode2023.Day4;
global using AdventOfCode2023.Day5; global using AdventOfCode2023.Day5;
global using AdventOfCode2023.Day6; global using AdventOfCode2023.Day6;
global using AdventOfCode2023.Day7;