-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
41 lines (33 loc) · 995 Bytes
/
Program.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.Common;
var levels = Resources.GetInputFileLines()
.Select(line => line.SplitToNumbers(" "))
.ToList();
var safeLevels = levels.Where(IsSafe);
var almostSafeLevels = levels.Where(IsAlmostSafe);
Console.WriteLine($"Part 1: {safeLevels.Count()}");
Console.WriteLine($"Part 2: {almostSafeLevels.Count()}");
static bool IsSafe(IReadOnlyCollection<int> level)
{
var differences = level
.PairWithNext()
.Select(pair => pair.Second - pair.First)
.ToList();
return differences.Select(Math.Abs).All(p => p >= 1 && p <= 3)
&& (differences.All(p => p > 0) || differences.All(p => p < 0));
}
static bool IsAlmostSafe(IReadOnlyCollection<int> level)
{
if (IsSafe(level))
{
return true;
}
for (int i = 0; i < level.Count; i++)
{
var newLevel = level.Take(i).Concat(level.Skip(i + 1)).ToList();
if (IsSafe(newLevel))
{
return true;
}
}
return false;
}