tidy up day3 variable names
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ben Harris 2020-12-03 14:42:02 -05:00
parent b4f7783b45
commit 809efd2bb1

View File

@ -5,19 +5,21 @@ namespace aoc2020
public sealed class Day3 : Day public sealed class Day3 : Day
{ {
private readonly string[] _grid; private readonly string[] _grid;
private readonly int _width;
public Day3() public Day3()
{ {
_grid = Input.ToArray(); _grid = Input.ToArray();
_width = _grid[0].Length;
} }
public override int DayNumber => 3; public override int DayNumber => 3;
private int CountSlope(int di, int dx) private long CountSlope(int dx, int dy)
{ {
var hits = 0; long hits = 0;
for (int i = 0, x = 0; i < _grid.Length; i += di, x = (x + dx) % _grid.First().Length) for (int x = 0, y = 0; y < _grid.Length; y += dy, x = (x + dx) % _width)
if (_grid[i][x] == '#') if (_grid[y][x] == '#')
hits++; hits++;
return hits; return hits;
@ -25,16 +27,24 @@ namespace aoc2020
public override string Part1() public override string Part1()
{ {
return $"{CountSlope(1, 3)}"; return $"{CountSlope(3, 1)}";
} }
public override string Part2() public override string Part2()
{ {
return $@"{CountSlope(1, 1) var slopes = new[]
* CountSlope(1, 3) {
* CountSlope(1, 5) (1, 1),
* CountSlope(1, 7) (3, 1),
* CountSlope(2, 1)}"; (5, 1),
(7, 1),
(1, 2)
};
return slopes
.Select(s => CountSlope(s.Item1, s.Item2))
.Aggregate((acc, i) => acc * i)
.ToString();
} }
} }
} }