-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day2.cs
49 lines (44 loc) · 1.67 KB
/
Day2.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
public class Day2
{
public static async Task<string> Part1Async(bool example = false)
{
var input = await File.ReadAllLinesAsync($"inputs/day2{(example ? ".example" : string.Empty)}.txt");
var courseSteps = input.Select(line =>
{
var parts = line.Split();
return (direction: parts[0], count: int.Parse(parts[1]));
}).ToArray();
var foreward = courseSteps.Where(s => s.direction == "forward").Sum(s => s.count);
var up = courseSteps.Where(s => s.direction == "up").Sum(s => s.count);
var down = courseSteps.Where(s => s.direction == "down").Sum(s => s.count);
var depth = down - up;
return (foreward * depth).ToString();
}
public static async Task<string> Part2Async(bool example = false)
{
var input = await File.ReadAllLinesAsync($"inputs/day2{(example ? ".example" : string.Empty)}.txt");
int aim = 0;
int position = 0;
int depth = 0;
input.ToList()
.ForEach(line =>
{
var parts = line.Split();
var count = int.Parse(parts[1]);
switch (parts[0])
{
case "forward":
position += count;
depth += count * aim;
break;
case "down":
aim += count;
break;
case "up":
aim -= count;
break;
}
});
return (position * depth).ToString();
}
}