-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day03.cs
59 lines (49 loc) · 1.54 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
48
49
50
51
52
53
54
55
56
57
58
59
using AdventOfCode.CSharp.Common;
using System;
using System.Collections.Generic;
namespace AdventOfCode.CSharp.Y2015.Solvers;
public class Day03 : ISolver
{
public static void Solve(ReadOnlySpan<byte> input, Solution solution)
{
var seen = new HashSet<uint>();
RunSanta(input, seen, start: 0, step: 1);
int part1 = seen.Count;
seen.Clear();
RunSanta(input, seen, start: 0, step: 2);
RunSanta(input, seen, start: 1, step: 2);
int part2 = seen.Count;
solution.SubmitPart1(part1);
solution.SubmitPart2(part2);
}
private static void RunSanta(ReadOnlySpan<byte> moves, HashSet<uint> seen, int start, int step)
{
const int unitX = 1 << 16;
const int unitY = 1;
// get starting coordinates
uint x = ushort.MaxValue / 2;
uint y = ushort.MaxValue / 2;
// pack them into a single uint
uint encodedPos = x << 16 | y;
seen.Add(encodedPos);
for (int i = start; i < moves.Length; i += step)
{
switch (moves[i])
{
case (byte)'^':
encodedPos += unitY;
break;
case (byte)'v':
encodedPos -= unitY;
break;
case (byte)'>':
encodedPos += unitX;
break;
case (byte)'<':
encodedPos -= unitX;
break;
}
seen.Add(encodedPos);
}
}
}