-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDay06.cs
More file actions
67 lines (54 loc) · 2.03 KB
/
Day06.cs
File metadata and controls
67 lines (54 loc) · 2.03 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using AdventOfCode.CSharp.Common;
namespace AdventOfCode.CSharp.Y2025.Solvers;
public class Day06 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
var lineLength = input.IndexOf((byte)'\n') + 1;
long part1 = 0;
long part2 = 0;
var operatorIndex = input.Length - lineLength;
var col = 0;
while (col < lineLength - 1)
{
var op = input[operatorIndex]; // '+' or '*'
var width = input[(operatorIndex + 1)..].IndexOfAnyExcept((byte)' ');
// handle last column not having a space separator
if (col + width == lineLength - 2)
width++;
var isAddition = op == (byte)'+';
var opBase = isAddition ? 0 : 1;
long part1Val = opBase;
for (var rowStartIndex = col; rowStartIndex < operatorIndex; rowStartIndex += lineLength)
{
var operand = 0;
for (var i = rowStartIndex; i < rowStartIndex + width; i++)
{
var c = input[i];
if (c != ' ')
operand = operand * 10 + (c - (byte)'0');
}
part1Val = isAddition ? part1Val + operand : part1Val * operand;
}
part1 += part1Val;
long part2Val = opBase;
for (var colStartIndex = col; colStartIndex < col + width; colStartIndex++)
{
var operand = 0;
for (var i = colStartIndex; i < operatorIndex; i += lineLength)
{
var c = input[i];
if (c != ' ')
operand = operand * 10 + (c - (byte)'0');
}
part2Val = isAddition ? part2Val + operand : part2Val * operand;
}
part2 += part2Val;
col += width + 1;
operatorIndex += width + 1;
}
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
}