-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathDay02.cs
41 lines (36 loc) · 1.29 KB
/
Day02.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
using AdventOfCode.CSharp.Common;
using System;
namespace AdventOfCode.CSharp.Y2021.Solvers;
public class Day02 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
int horizontal = 0;
int part1DepthPart2Aim = 0; // depth for part 1, aim for part 2
int part2Depth = 0;
int i = 0;
while (i < input.Length)
{
switch (input[i])
{
case (byte)'f': // forward
int amount = CharToValue(input[i + "forward ".Length]);
horizontal += amount;
part2Depth += part1DepthPart2Aim * amount;
i += "forward x\n".Length;
break;
case (byte)'d': // down
part1DepthPart2Aim += CharToValue(input[i + "down ".Length]);
i += "down x\n".Length;
break;
default: // up
part1DepthPart2Aim -= CharToValue(input[i + "up ".Length]);
i += "up x\n".Length;
break;
}
}
solution.SubmitPart1(horizontal * part1DepthPart2Aim);
solution.SubmitPart2(horizontal * part2Depth);
}
private static int CharToValue(byte c) => c - '0';
}