-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
81 lines (70 loc) · 2.72 KB
/
Solution.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Linq;
namespace AdventOfCode2016.Day02
{
internal class Solution
{
private static readonly IDictionary<IntVector, char> Keypad1 = new Dictionary<IntVector, char>
{
[new IntVector(-1, 1)] = '1',
[new IntVector(0, 1)] = '2',
[new IntVector(1, 1)] = '3',
[new IntVector(-1, 0)] = '4',
[new IntVector(0, 0)] = '5',
[new IntVector(1, 0)] = '6',
[new IntVector(-1, -1)] = '7',
[new IntVector(0, -1)] = '8',
[new IntVector(1, -1)] = '9',
};
private static readonly IDictionary<IntVector, char> Keypad2 = new Dictionary<IntVector, char>
{
[new IntVector(0, 2)] = '1',
[new IntVector(-1, 1)] = '2',
[new IntVector(0, 1)] = '3',
[new IntVector(1, 1)] = '4',
[new IntVector(-2, 0)] = '5',
[new IntVector(-1, 0)] = '6',
[new IntVector(0, 0)] = '7',
[new IntVector(1, 0)] = '8',
[new IntVector(2, 0)] = '9',
[new IntVector(-1, -1)] = 'A',
[new IntVector(0, -1)] = 'B',
[new IntVector(1, -1)] = 'C',
[new IntVector(0, -2)] = 'D',
};
private readonly IReadOnlyCollection<IEnumerable<Direction>> _instructions;
public Solution(IEnumerable<string> input)
{
_instructions = input.Select(ParseInstructions).ToList();
}
public string PartOne() => FindCode(Keypad1, new IntVector(0, 0));
public string PartTwo() => FindCode(Keypad2, new IntVector(-2, 0));
private string FindCode(IDictionary<IntVector, char> keypad, IntVector start)
{
var position = start;
var code = "";
foreach (var instruction in _instructions)
{
foreach (var direction in instruction)
{
if (keypad.ContainsKey(position + direction.Move()))
{
position += direction.Move();
}
}
code += keypad[position];
}
return code;
}
private static IEnumerable<Direction> ParseInstructions(string instructions) => instructions.Select(
instruction => instruction switch
{
'U' => Direction.North,
'R' => Direction.East,
'D' => Direction.South,
'L' => Direction.West,
_ => throw new ArgumentException($"Unknown instruction {instruction}")
});
}
}