Skip to content

Commit

Permalink
Day 4: ⭐⭐
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktschlager committed Dec 4, 2023
1 parent 4db4c7d commit e9a39ca
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ jobs:

- name: Run
working-directory: adventOfCode23
run: dotnet run --ci
run: dotnet run -c Release -- --ci

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
My [Advent of Code 23](https://adventofcode.com/2023) solutions

The solutions of day 1 to 3 can be found in [FirstDays/FirstDays.cs](adventOfCode23/FirstDays/FirstDays.cs) <!-- solution days -->
The solutions of day 1 to 4 can be found in [FirstDays/FirstDays.cs](adventOfCode23/FirstDays/FirstDays.cs) <!-- solution days -->
49 changes: 49 additions & 0 deletions adventOfCode23/FirstDays/FirstDays.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
using System.Text.RegularExpressions;
using static System.Text.RegularExpressions.Regex;

namespace adventOfCode23.FirstDays;

public class FirstDays
{
private readonly bool _isCi;

public FirstDays(bool isCi)
{
_isCi = isCi;
}

private string[] CleanInput(string s)
{
var lines = s.Split(Environment.NewLine);
Expand Down Expand Up @@ -196,6 +204,44 @@ private void Day3()
Console.WriteLine($"Sum: {sum}");
}

private void Day4()
{
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Day 4");
// What about 10378710?
Console.ResetColor();
var input = CleanInputFile("day4.txt");
var result = 0;

var cards = new Dictionary<int, int>();
foreach (var line in input)
{
var groups = Match(line,
@"Card +(\d+): {1,2}(?:((\d+) {1,2})+)\| {1,2}(:?(:?(\d+) {0,2})+)").Groups;
var (cardNr, winNr, haveNr) = (int.Parse(groups[1].Value), groups[2].Captures, groups[5].Captures);
var winNrP = winNr.Select(n => int.Parse(n.Value));
var haveNrP = haveNr.Select(n => int.Parse(n.Value));
var wonCount = haveNrP.Count(n => winNrP.Contains(n));
cards[cardNr] = wonCount;
}


var todos = new Queue<int>(cards.Keys);

while (todos.Count > 0)
{
var nr = todos.Dequeue();
var wonCount = cards[nr];
foreach (var n in Enumerable.Range(nr + 1, wonCount)) todos.Enqueue(n);

//Console.WriteLine($"Won {wonCount} by card {card}");
result += 1;
}

Console.WriteLine($"Total Points are {result}");
}

public void Run(int? day = null)
{
var today = DateTime.Now.Day;
Expand All @@ -210,6 +256,9 @@ public void Run(int? day = null)
case 3:
Day3();
break;
case 4:
Day4();
break;
default:
Console.WriteLine("day is missing!");
break;
Expand Down
Loading

0 comments on commit e9a39ca

Please sign in to comment.