refactor p1 and p2 to return objects
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
7feb07944a
commit
18a5d9aae3
|
@ -29,7 +29,7 @@ public class DayTests
|
||||||
// part 1
|
// part 1
|
||||||
s.Reset();
|
s.Reset();
|
||||||
s.Start();
|
s.Start();
|
||||||
var part1Actual = day.Part1();
|
var part1Actual = day.Part1().ToString();
|
||||||
s.Stop();
|
s.Stop();
|
||||||
Console.Write($"Part 1: {part1Actual,-25} ");
|
Console.Write($"Part 1: {part1Actual,-25} ");
|
||||||
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
||||||
|
@ -38,7 +38,7 @@ public class DayTests
|
||||||
// part 2
|
// part 2
|
||||||
s.Reset();
|
s.Reset();
|
||||||
s.Start();
|
s.Start();
|
||||||
var part2Actual = day.Part2();
|
var part2Actual = day.Part2().ToString();
|
||||||
s.Stop();
|
s.Stop();
|
||||||
Console.Write($"Part 2: {part2Actual,-25} ");
|
Console.Write($"Part 2: {part2Actual,-25} ");
|
||||||
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
||||||
|
@ -72,7 +72,7 @@ public class DayTests
|
||||||
// part 1
|
// part 1
|
||||||
s.Reset();
|
s.Reset();
|
||||||
s.Start();
|
s.Start();
|
||||||
var part1Actual = day.Part1();
|
var part1Actual = day.Part1().ToString();
|
||||||
s.Stop();
|
s.Stop();
|
||||||
Console.Write($"Part 1: {part1Actual,-25} ");
|
Console.Write($"Part 1: {part1Actual,-25} ");
|
||||||
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
||||||
|
@ -81,7 +81,7 @@ public class DayTests
|
||||||
// part 2
|
// part 2
|
||||||
s.Reset();
|
s.Reset();
|
||||||
s.Start();
|
s.Start();
|
||||||
var part2Actual = day.Part2();
|
var part2Actual = day.Part2().ToString();
|
||||||
s.Stop();
|
s.Stop();
|
||||||
Console.Write($"Part 2: {part2Actual,-25} ");
|
Console.Write($"Part 2: {part2Actual,-25} ");
|
||||||
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
Console.WriteLine($"{s.ScaleMilliseconds()} ms elapsed");
|
||||||
|
|
|
@ -20,8 +20,8 @@ public abstract class Day
|
||||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
|
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
|
||||||
$"input/{(UseTestInput ? "test" : "day")}{DayNumber,2:00}.in");
|
$"input/{(UseTestInput ? "test" : "day")}{DayNumber,2:00}.in");
|
||||||
|
|
||||||
public abstract string Part1();
|
public abstract object Part1();
|
||||||
public abstract string Part2();
|
public abstract object Part2();
|
||||||
|
|
||||||
public void AllParts(bool verbose = true)
|
public void AllParts(bool verbose = true)
|
||||||
{
|
{
|
||||||
|
|
|
@ -12,9 +12,9 @@ public sealed class Day01 : Day
|
||||||
_readings = Input.Select(int.Parse).ToList();
|
_readings = Input.Select(int.Parse).ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() =>
|
public override object Part1() =>
|
||||||
Enumerable.Range(0, _readings.Count - 1).Count(i => _readings[i + 1] > _readings[i]).ToString();
|
Enumerable.Range(0, _readings.Count - 1).Count(i => _readings[i + 1] > _readings[i]);
|
||||||
|
|
||||||
public override string Part2() =>
|
public override object Part2() =>
|
||||||
Enumerable.Range(0, _readings.Count - 3).Count(i => _readings[i + 3] > _readings[i]).ToString();
|
Enumerable.Range(0, _readings.Count - 3).Count(i => _readings[i + 3] > _readings[i]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public sealed class Day02 : Day
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1()
|
public override object Part1()
|
||||||
{
|
{
|
||||||
int horiz = 0, depth = 0;
|
int horiz = 0, depth = 0;
|
||||||
foreach (var line in Input)
|
foreach (var line in Input)
|
||||||
|
@ -30,10 +30,10 @@ public sealed class Day02 : Day
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{horiz * depth}";
|
return horiz * depth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
int aim = 0, depth = 0, horiz = 0;
|
int aim = 0, depth = 0, horiz = 0;
|
||||||
foreach (var line in Input)
|
foreach (var line in Input)
|
||||||
|
@ -55,6 +55,6 @@ public sealed class Day02 : Day
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $"{horiz * depth}";
|
return horiz * depth;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ public sealed class Day03 : Day
|
||||||
_report = Input.ToList();
|
_report = Input.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1()
|
public override object Part1()
|
||||||
{
|
{
|
||||||
var l = _report.Count / 2;
|
var l = _report.Count / 2;
|
||||||
var g = new StringBuilder();
|
var g = new StringBuilder();
|
||||||
|
@ -28,10 +28,10 @@ public sealed class Day03 : Day
|
||||||
var gamma = g.ToString().BigIntegerFromBinaryString();
|
var gamma = g.ToString().BigIntegerFromBinaryString();
|
||||||
var epsilon = e.ToString().BigIntegerFromBinaryString();
|
var epsilon = e.ToString().BigIntegerFromBinaryString();
|
||||||
|
|
||||||
return $"{gamma * epsilon}";
|
return gamma * epsilon;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
var o = _report;
|
var o = _report;
|
||||||
var c = _report;
|
var c = _report;
|
||||||
|
@ -57,6 +57,6 @@ public sealed class Day03 : Day
|
||||||
}
|
}
|
||||||
var co2 = c.Single().BigIntegerFromBinaryString();
|
var co2 = c.Single().BigIntegerFromBinaryString();
|
||||||
|
|
||||||
return $"{o2 * co2}";
|
return o2 * co2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,7 +32,7 @@ public sealed class Day04 : Day
|
||||||
_size = (int)Math.Sqrt(currentBoard.Count);
|
_size = (int)Math.Sqrt(currentBoard.Count);
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1()
|
public override object Part1()
|
||||||
{
|
{
|
||||||
int i = _size, b = FirstWin(i);
|
int i = _size, b = FirstWin(i);
|
||||||
while (b == -1)
|
while (b == -1)
|
||||||
|
@ -42,10 +42,10 @@ public sealed class Day04 : Day
|
||||||
}
|
}
|
||||||
|
|
||||||
var called = _call.Take(i).ToHashSet();
|
var called = _call.Take(i).ToHashSet();
|
||||||
return $"{called.Last() * _boards[b].Where(x => !called.Contains(x)).Sum()}";
|
return called.Last() * _boards[b].Where(x => !called.Contains(x)).Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
Dictionary<int, bool> wonBoards = new();
|
Dictionary<int, bool> wonBoards = new();
|
||||||
for (var i = 0; i < _boards.Count; i++)
|
for (var i = 0; i < _boards.Count; i++)
|
||||||
|
@ -62,7 +62,7 @@ public sealed class Day04 : Day
|
||||||
|
|
||||||
var called = _call.Take(j).ToHashSet();
|
var called = _call.Take(j).ToHashSet();
|
||||||
var b = wonBoards.Single(kvp => !kvp.Value).Key;
|
var b = wonBoards.Single(kvp => !kvp.Value).Key;
|
||||||
return $"{called.Last() * _boards[b].Where(x => !called.Contains(x)).Sum()}";
|
return called.Last() * _boards[b].Where(x => !called.Contains(x)).Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
private int FirstWin(int i)
|
private int FirstWin(int i)
|
||||||
|
|
|
@ -21,7 +21,7 @@ public sealed class Day05 : Day
|
||||||
.GroupBy(k => k)
|
.GroupBy(k => k)
|
||||||
.Count(k => k.Count() > 1);
|
.Count(k => k.Count() > 1);
|
||||||
|
|
||||||
public override string Part1() => $"{Solve()}";
|
public override object Part1() => Solve();
|
||||||
|
|
||||||
public override string Part2() => $"{Solve(diagonals: true)}";
|
public override object Part2() => Solve(diagonals: true);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ public sealed class Day06 : Day
|
||||||
_p2 = counts.Values.Sum();
|
_p2 = counts.Values.Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() => $"{_p1}";
|
public override object Part1() => _p1;
|
||||||
|
|
||||||
public override string Part2() => $"{_p2}";
|
public override object Part2() => _p2;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,17 +14,17 @@ public sealed class Day07 : Day
|
||||||
|
|
||||||
private static long ArithmeticSumTo(long n) => n * (n + 1) / 2L;
|
private static long ArithmeticSumTo(long n) => n * (n + 1) / 2L;
|
||||||
|
|
||||||
public override string Part1()
|
public override object Part1()
|
||||||
{
|
{
|
||||||
var i = _tape[_tape.Count / 2];
|
var i = _tape[_tape.Count / 2];
|
||||||
return $"{_tape.Select(t => Math.Abs(t - i)).Sum()}";
|
return _tape.Select(t => Math.Abs(t - i)).Sum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
var avg = (decimal)_tape.Sum() / _tape.Count;
|
var avg = (decimal)_tape.Sum() / _tape.Count;
|
||||||
var floor = _tape.Select(t => ArithmeticSumTo(Math.Abs(t - (long)Math.Floor(avg)))).Sum();
|
var floor = _tape.Select(t => ArithmeticSumTo(Math.Abs(t - (long)Math.Floor(avg)))).Sum();
|
||||||
var ceil = _tape.Select(t => ArithmeticSumTo(Math.Abs(t - (long)Math.Ceiling(avg)))).Sum();
|
var ceil = _tape.Select(t => ArithmeticSumTo(Math.Abs(t - (long)Math.Ceiling(avg)))).Sum();
|
||||||
return $"{Math.Min(floor, ceil)}";
|
return Math.Min(floor, ceil);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,12 +75,11 @@ public sealed class Day08 : Day
|
||||||
return int.Parse(digits.ToString());
|
return int.Parse(digits.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() =>
|
public override object Part1() =>
|
||||||
Input
|
Input
|
||||||
.Select(line => line.Split(" | ")[1].Split(' '))
|
.Select(line => line.Split(" | ")[1].Split(' '))
|
||||||
.Select(outputs => outputs.Count(o => new[] { 2, 3, 4, 7 }.Contains(o.Length)))
|
.Select(outputs => outputs.Count(o => new[] { 2, 3, 4, 7 }.Contains(o.Length)))
|
||||||
.Sum()
|
.Sum();
|
||||||
.ToString();
|
|
||||||
|
|
||||||
public override string Part2() => Input.Select(Decode).Sum().ToString();
|
public override object Part2() => Input.Select(Decode).Sum();
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,9 +31,9 @@ public sealed class Day09 : Day
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() => _part1Sum.ToString();
|
public override object Part1() => _part1Sum;
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
var sizes = new List<long>();
|
var sizes = new List<long>();
|
||||||
foreach (var (x, y) in _lowPoints)
|
foreach (var (x, y) in _lowPoints)
|
||||||
|
@ -66,7 +66,6 @@ public sealed class Day09 : Day
|
||||||
return sizes
|
return sizes
|
||||||
.OrderByDescending(x => x)
|
.OrderByDescending(x => x)
|
||||||
.Take(3)
|
.Take(3)
|
||||||
.Aggregate(1L, (a, b) => a * b)
|
.Aggregate(1L, (a, b) => a * b);
|
||||||
.ToString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,11 +67,11 @@ public sealed class Day10 : Day
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() => $"{_score1}";
|
public override object Part1() => _score1;
|
||||||
|
|
||||||
public override string Part2()
|
public override object Part2()
|
||||||
{
|
{
|
||||||
var sorted = _scores2.OrderBy(i => i).ToList();
|
var sorted = _scores2.OrderBy(i => i).ToList();
|
||||||
return $"{sorted[sorted.Count / 2]}";
|
return sorted[sorted.Count / 2];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ public sealed class Day11 : Day
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() => $"{_flashesAfter100}";
|
public override object Part1() => _flashesAfter100;
|
||||||
|
|
||||||
public override string Part2() => $"{_totalTurns}";
|
public override object Part2() => _totalTurns;
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,9 +54,9 @@ public sealed class Day12 : Day
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() =>
|
public override object Part1() =>
|
||||||
$"{WalkGraph(_edges, "start", new())}";
|
WalkGraph(_edges, "start", new());
|
||||||
|
|
||||||
public override string Part2() =>
|
public override object Part2() =>
|
||||||
$"{TraverseGraph(_edges, "start", new())}";
|
TraverseGraph(_edges, "start", new());
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ public sealed class DayXX : Day
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string Part1() => "";
|
public override object Part1() => "";
|
||||||
|
|
||||||
public override string Part2() => "";
|
public override object Part2() => "";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue