Completed day 7
This commit is contained in:
parent
bf28d5e69c
commit
a6113210aa
@ -4,12 +4,12 @@ using AdventOfCode.Core;
|
||||
|
||||
InputReader inputReader = new()
|
||||
{
|
||||
IsDebug = true
|
||||
//IsDebug = true
|
||||
};
|
||||
|
||||
//inputReader.SetInputByChallange(3);
|
||||
|
||||
IChallange challange = new Day05(inputReader);
|
||||
IChallange challange = new Day07(inputReader);
|
||||
|
||||
Console.WriteLine($"Part 1: {await challange.GetSolutionPart1()}");
|
||||
|
||||
|
||||
@ -81,7 +81,7 @@ namespace AdventOfCode.Solutions._2023
|
||||
long lowestLocation = long.MaxValue;
|
||||
|
||||
List<long> proccessed = [];
|
||||
|
||||
// change to use ranges and shift those
|
||||
for (int seedDataIndex = 0; seedDataIndex < seedData.Length; seedDataIndex += 2)
|
||||
{
|
||||
for (long seedLocaton = seedData[seedDataIndex]; seedLocaton < seedData[seedDataIndex] + seedData[seedDataIndex + 1]; seedLocaton++)
|
||||
|
||||
19
AdventOfCode.Solutions/2023/Day 06/Day06.cs
Normal file
19
AdventOfCode.Solutions/2023/Day 06/Day06.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using AdventOfCode.Core;
|
||||
|
||||
namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
public class Day06(InputReader reader) : IChallange
|
||||
{
|
||||
private InputReader _inputReader = reader;
|
||||
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
2
AdventOfCode.Solutions/2023/Day 06/day-06-input.txt
Normal file
2
AdventOfCode.Solutions/2023/Day 06/day-06-input.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Time: 41 66 72 66
|
||||
Distance: 244 1047 1228 1040
|
||||
137
AdventOfCode.Solutions/2023/Day 07/Day07.cs
Normal file
137
AdventOfCode.Solutions/2023/Day 07/Day07.cs
Normal file
@ -0,0 +1,137 @@
|
||||
using AdventOfCode.Core;
|
||||
|
||||
namespace AdventOfCode.Solutions._2023
|
||||
{
|
||||
public class Day07(InputReader reader) : IChallange
|
||||
{
|
||||
private InputReader _inputReader = reader;
|
||||
|
||||
private static readonly List<char> CardValues = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'];
|
||||
private static readonly List<char> CardValuesJoker = ['J', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'Q', 'K', 'A'];
|
||||
private enum SetRanks { High, OnePair, TwoPair, ThreeKind, FullHouse, FourKind, FiveKind }
|
||||
|
||||
private record Hand : IComparable<Hand>
|
||||
{
|
||||
public int Bid { get; set; }
|
||||
public SetRanks Rank { get; set; }
|
||||
public int HighValue { get; set; }
|
||||
|
||||
public string Source { get; set; }
|
||||
|
||||
public string JokerSource { get; set; } = string.Empty;
|
||||
|
||||
public List<char> CharCompairList => string.IsNullOrEmpty(JokerSource) ? CardValues : CardValuesJoker;
|
||||
|
||||
public Hand(string line, bool jokerMode = false)
|
||||
{
|
||||
string[] splitted = line.Split(' ');
|
||||
Source = splitted[0];
|
||||
Bid = int.Parse(splitted[1]);
|
||||
|
||||
string finalTestLine = Source;
|
||||
if (jokerMode)
|
||||
{
|
||||
JokerSource = Source;
|
||||
if (finalTestLine.Contains('J') && finalTestLine.Any(c => c != 'J'))
|
||||
{
|
||||
// fun time
|
||||
char toReplace = finalTestLine.Where(c => c != 'J').GroupBy(c => c).OrderByDescending(g => g.Count()).First().Key;
|
||||
finalTestLine = finalTestLine.Replace('J', toReplace);
|
||||
JokerSource = finalTestLine;
|
||||
Console.WriteLine($"{Source} > {JokerSource}");
|
||||
}
|
||||
}
|
||||
|
||||
var grouped = finalTestLine.GroupBy(c => c).OrderByDescending(g => g.Count());
|
||||
|
||||
switch(grouped.Count())
|
||||
{
|
||||
case 1:
|
||||
Rank = SetRanks.FiveKind;
|
||||
break;
|
||||
case 4:
|
||||
Rank = SetRanks.OnePair;
|
||||
break;
|
||||
case 5:
|
||||
Rank = SetRanks.High;
|
||||
break;
|
||||
case 3:
|
||||
if (grouped.Skip(1).First().Count() == 2)
|
||||
Rank = SetRanks.TwoPair;
|
||||
else
|
||||
Rank = SetRanks.ThreeKind;
|
||||
break;
|
||||
case 2:
|
||||
if (grouped.First().Count() == 4)
|
||||
Rank = SetRanks.FourKind;
|
||||
else
|
||||
Rank = SetRanks.FullHouse;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{JokerSource} [{Source}] {Rank} {Bid}";
|
||||
}
|
||||
|
||||
public int CompareTo(Hand? other)
|
||||
{
|
||||
if (other == null)
|
||||
return 1;
|
||||
|
||||
if (this.Rank == other.Rank && this.Source == other.Source)
|
||||
return 0;
|
||||
|
||||
if (this.Rank > other.Rank)
|
||||
return 1;
|
||||
if (this.Rank < other.Rank)
|
||||
return -1;
|
||||
|
||||
for (int i = 0; i < Source.Length; i++)
|
||||
{
|
||||
if (this.Source[i] == other.Source[i])
|
||||
continue;
|
||||
if (CharCompairList.IndexOf(this.Source[i]) > CharCompairList.IndexOf(other.Source[i]))
|
||||
return 1;
|
||||
if (CharCompairList.IndexOf(this.Source[i]) < CharCompairList.IndexOf(other.Source[i]))
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetSolutionPart1()
|
||||
{
|
||||
List<Hand> hands =
|
||||
[
|
||||
.. (await _inputReader.ReadAsArrayString()).Select(l => new Hand(l)).Order()
|
||||
];
|
||||
|
||||
int sum = 0;
|
||||
for (int index = 0; index < hands.Count; index++)
|
||||
{
|
||||
sum += hands[index].Bid * (index + 1);
|
||||
}
|
||||
|
||||
return sum.ToString();
|
||||
}
|
||||
|
||||
public async Task<string> GetSolutionPart2()
|
||||
{
|
||||
List<Hand> hands =
|
||||
[
|
||||
.. (await _inputReader.ReadAsArrayString()).Select(l => new Hand(l, true)).Order()
|
||||
];
|
||||
|
||||
int sum = 0;
|
||||
for (int index = 0; index < hands.Count; index++)
|
||||
{
|
||||
sum += hands[index].Bid * (index + 1);
|
||||
}
|
||||
|
||||
return sum.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
1000
AdventOfCode.Solutions/2023/Day 07/day-07-input.txt
Normal file
1000
AdventOfCode.Solutions/2023/Day 07/day-07-input.txt
Normal file
File diff suppressed because it is too large
Load Diff
@ -12,6 +12,12 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Update="2023\Day 07\Day07.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2023\Day 06\Day06.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
<Compile Update="2023\Day 00\Day00.cs">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</Compile>
|
||||
|
||||
@ -1,33 +1,5 @@
|
||||
seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
||||
Loading…
Reference in New Issue
Block a user