Skip to content

Commit

Permalink
day(25): Done!
Browse files Browse the repository at this point in the history
  • Loading branch information
adamrodger committed Dec 25, 2024
1 parent 08aa3ee commit 3905834
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/AdventOfCode/Day25.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;

namespace AdventOfCode
{
/// <summary>
/// Solver for Day 25
/// </summary>
public class Day25
{
public int Part1(string input)
{
List<Schematic> locks = [];
List<Schematic> keys = [];

foreach (string chunk in input.TrimEnd().Split("\n\n"))
{
string[] lines = chunk.Split("\n");

bool isLock = lines[0] == "#####";

IEnumerable<string> rows = isLock ? lines.Skip(1) : lines.Reverse().Skip(1);

int height0 = 0, height1 = 0, height2 = 0, height3 = 0, height4 = 0;

foreach (string row in rows)
{
height0 += row[0] == '#' ? 1 : 0;
height1 += row[1] == '#' ? 1 : 0;
height2 += row[2] == '#' ? 1 : 0;
height3 += row[3] == '#' ? 1 : 0;
height4 += row[4] == '#' ? 1 : 0;
}

if (isLock)
{
locks.Add(new Schematic(height0, height1, height2, height3, height4));
}
else
{
keys.Add(new Schematic(height0, height1, height2, height3, height4));
}
}

return keys.Sum(k => locks.Count(l => k.Height0 + l.Height0 < 6
&& k.Height1 + l.Height1 < 6
&& k.Height2 + l.Height2 < 6
&& k.Height3 + l.Height3 < 6
&& k.Height4 + l.Height4 < 6));
}

private record Schematic(int Height0, int Height1, int Height2, int Height3, int Height4);
}
}
Binary file added src/AdventOfCode/inputs/day25.txt
Binary file not shown.
35 changes: 35 additions & 0 deletions tests/AdventOfCode.Tests/Day25Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;
using Xunit;
using Xunit.Abstractions;

namespace AdventOfCode.Tests
{
public class Day25Tests
{
private readonly ITestOutputHelper output;
private readonly Day25 solver;

public Day25Tests(ITestOutputHelper output)
{
this.output = output;
this.solver = new Day25();
}

private static string GetRealInput()
{
string input = File.ReadAllText("inputs/day25.txt");
return input;
}

[Fact]
public void Part1_RealInput_ProducesCorrectResponse()
{
var expected = 3021;

var result = solver.Part1(GetRealInput());
output.WriteLine($"Day 25 - Part 1 - {result}");

Assert.Equal(expected, result);
}
}
}

0 comments on commit 3905834

Please sign in to comment.