-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay02Task2.cs
More file actions
33 lines (30 loc) · 1.2 KB
/
Copy pathDay02Task2.cs
File metadata and controls
33 lines (30 loc) · 1.2 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
namespace AdventOfCode2021;
public class Day02Task2
{
public Day02Task2()
{
moveFuncByCommand = new Dictionary<string, Func<int, PositionWithAim, PositionWithAim>>
{
["forward"] = (commandValue, position) => position with {
Horizontal = position.Horizontal + commandValue,
Depth = position.Depth + (position.Aim * commandValue)
},
["down"] = (commandValue, position) => position with { Aim = position.Aim + commandValue },
["up"] = (commandValue, position) => position with { Aim = position.Aim - commandValue },
};
}
protected readonly Dictionary<string, Func<int, PositionWithAim, PositionWithAim>> moveFuncByCommand;
public Position MoveSubmarine(string[] commands)
{
var result = new PositionWithAim(0, 0, 0);
foreach (var cmd in commands)
{
var nameAndValue = cmd.Split(" ", StringSplitOptions.TrimEntries);
result = moveFuncByCommand[nameAndValue[0]].Invoke(int.Parse(nameAndValue[1]), result);
}
return result;
}
}
public record PositionWithAim(int Horizontal, int Depth, int Aim) : Position(Horizontal, Depth)
{
}