-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay01.cs
More file actions
70 lines (61 loc) · 1.88 KB
/
Day01.cs
File metadata and controls
70 lines (61 loc) · 1.88 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
68
69
70
using System.Diagnostics;
using System;
namespace _2025._01;
public enum Rotation
{
LEFT,
RIGHT
}
public sealed class Day01 : Base
{
public Day01(bool example) : base(example)
{
Day = "01";
}
public IEnumerable<(Rotation, int)> Parse(string[] input)
{
return input.Select(x => (x[0] == 'L' ? Rotation.LEFT : Rotation.RIGHT, int.Parse(x[1..])));
}
public override object PartOne()
{
string[] input = ReadInput();
int dialPosition = 50;
int password = 0;
foreach((Rotation rotation, int steps) in Parse(input))
{
int dir = rotation == Rotation.LEFT ? -steps : steps;
dialPosition = MathExtensions.Modulo(dialPosition+dir, 100);
if(dialPosition == 0)
{
password++;
}
}
return password.ToString();
}
public override object PartTwo()
{
string[] input = ReadInput();
int dialPosition = 50;
int password = 0;
foreach((Rotation rotation, int steps) in Parse(input))
{
if(rotation == Rotation.LEFT)
{
// wrap around first to prevent counting starting from zero as hitting a zero once
if(dialPosition == 0)
{
dialPosition = 100;
}
dialPosition -= steps;
password += (int)Math.Ceiling((dialPosition*-1) / 100.0f);
dialPosition = MathExtensions.Modulo(dialPosition+steps, 100);
} else
{
password += (int)Math.Floor((dialPosition + steps) / 100.0f);
dialPosition = MathExtensions.Modulo(dialPosition+steps, 100);
}
}
return password.ToString();
}
}