-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtag02.cs
64 lines (55 loc) · 2.04 KB
/
tag02.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
60
61
62
63
64
var input = File.ReadAllLines("../../../input.txt")
.Select(line => line.Split(' ').Select(int.Parse).ToList())
.Select(numbers => new Report(numbers));
Console.WriteLine(input.Count(r => r.Day2()));
Console.WriteLine(input.Count(r => r.Day2Part2()));
class Report(List<int> levels)
{
public bool Day2()
{
for (var i = 0; i < levels.Count - 1; i++)
if (Math.Abs(levels[i] - levels[i + 1]) > 3 || levels[i] - levels[i + 1] == 0 || DirectionChanged(levels[i], levels[i + 1]))
return false;
return true;
}
public bool Day2Part2()
{
for (var i = 0; i < levels.Count - 1; i++)
{
var next = i++;
var directionChanged = DirectionChanged(levels[i], levels[next]);
if (!IsOffLimits(levels[i], levels[next]) && !directionChanged)
continue;
if (directionChanged && i == 1)
{
var levelsWithoutFirstElement = levels.ToList();
levelsWithoutFirstElement.RemoveAt(0);
if(new Report(levelsWithoutFirstElement).Day2())
return true;
}
var levelsWithoutCurrent = levels.ToList();
levelsWithoutCurrent.RemoveAt(i);
if(new Report(levelsWithoutCurrent).Day2())
return true;
var levelsWithoutNext = levels.ToList();
levelsWithoutNext.RemoveAt(next);
return new Report(levelsWithoutNext).Day2();
}
return true;
}
private static bool IsOffLimits(int current, int next)
{ ;
return Math.Abs(next - current) > 3 || Math.Abs(next - current) == 0;
}
private bool DirectionChanged(int current, int next)
{
switch (levels[1] > levels[0] ? 1 /* inc */ : -1 /* desc */)
{
case 1 when !(next > current):
case -1 when next > current:
return true;
default:
return false;
}
}
}