tidy up day3 variable names
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Ben Harris 2020-12-03 14:42:02 -05:00
parent b4f7783b45
commit 809efd2bb1
1 changed files with 21 additions and 11 deletions

View File

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