-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay01.cs
56 lines (45 loc) · 1.39 KB
/
Day01.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
using static csharp.Helper;
namespace csharp.impl;
public class Day01 : IPuzzle
{
private readonly string[] _input;
public Day01(string filename)
{
_input = File.ReadAllLines(filename);
}
public string? FirstPuzzle()
{
var numbers = _input
.Select(line => line.Trim())
.Select(int.Parse).ToList();
var countIncreases = 0;
for (var i = 1; i < numbers.Count; i++)
{
if (numbers[i] > numbers[i - 1])
{
countIncreases++;
}
}
DebugMsg($"There was {countIncreases} out of total {numbers.Count} numbers.");
return countIncreases.ToString();
}
public string? SecondPuzzle()
{
var numbers = _input
.Select(line => line.Trim())
.Select(int.Parse).ToList();
var zipped = numbers.Zip(numbers.Skip(1), (a, b) => new { a, b })
.Zip(numbers.Skip(2), (ab, c) => (ab.a, ab.b, c));
var summed = zipped.Select(tuple => tuple.a + tuple.b + tuple.c).ToList();
var countIncreases = 0;
for (var i = 1; i < summed.Count; i++)
{
if (summed[i] > summed[i - 1])
{
countIncreases++;
}
}
DebugMsg($"There was {countIncreases} out of total {summed.Count} tuples.");
return countIncreases.ToString();
}
}