day 3 part 1
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Ben Harris 2021-12-03 01:05:45 -05:00
parent 437ad3ecfa
commit 15cf1a6a56
4 changed files with 1052 additions and 0 deletions

View File

@ -6,6 +6,7 @@ public class DayTests
[DataTestMethod]
[DataRow(typeof(Day01), "1616", "1645")]
[DataRow(typeof(Day02), "2272262", "2134882034")]
[DataRow(typeof(Day03), "3009600", "")]
public void CheckAllDays(Type dayType, string part1, string part2)
{
var s = Stopwatch.StartNew();
@ -38,6 +39,7 @@ public class DayTests
[DataTestMethod]
[DataRow(typeof(Day01), "7", "5")]
[DataRow(typeof(Day02), "150", "900")]
[DataRow(typeof(Day03), "198", "230")]
public void CheckTestInputs(Type dayType, string part1, string part2)
{
Day.UseTestInput = true;

38
aoc2021/Day03.cs Normal file
View File

@ -0,0 +1,38 @@
namespace aoc2021;
/// <summary>
/// Day 3: <see href="https://adventofcode.com/2021/day/3"/>
/// </summary>
public sealed class Day03 : Day
{
private readonly List<string> _report;
public Day03() : base(3, "Binary Diagnostic")
{
_report = Input.ToList();
}
public override string Part1()
{
var l = _report.Count / 2;
var g = new StringBuilder();
var e = new StringBuilder();
foreach (var i in Enumerable.Range(0, _report[0].Length))
{
var ones = _report.Select(r => r[i]).Count(c => c == '1');
g.Append(ones > l ? '1' : '0');
e.Append(ones > l ? '0' : '1');
}
var gamma = Convert.ToInt32(g.ToString(), 2);
var epsilon = Convert.ToInt32(e.ToString(), 2);
return $"{gamma * epsilon}";
}
public override string Part2()
{
return "";
}
}

1000
aoc2021/input/day03.in Normal file

File diff suppressed because it is too large Load Diff

12
aoc2021/input/test03.in Normal file
View File

@ -0,0 +1,12 @@
00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010