-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay03.cs
47 lines (39 loc) · 1.26 KB
/
Day03.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Runtime.CompilerServices;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2020.Solvers;
public class Day03 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int w = input.IndexOf((byte)'\n');
int h = (input.Length + 1) / (w + 1);
long slope_1_1 = CountTrees(input, w, h, 1, 1);
long slope_3_1 = CountTrees(input, w, h, 3, 1);
long slope_5_1 = CountTrees(input, w, h, 5, 1);
long slope_7_1 = CountTrees(input, w, h, 7, 1);
long slope_1_2 = CountTrees(input, w, h, 1, 2);
long part1 = slope_3_1;
long part2 = slope_1_1 * slope_3_1 * slope_5_1 * slope_7_1 * slope_1_2;
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static long CountTrees(ReadOnlySpan<byte> input, int w, int h, int dx, int dy)
{
int stride = w + 1;
int x = 0;
long total = 0;
for (int y = 0; y < h; y += dy)
{
if (input[y * stride + x] == '#')
{
total += 1;
}
x += dx;
if (x >= w)
x -= w;
}
return total;
}
}