-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday02_numpad.dart
120 lines (107 loc) · 2.74 KB
/
day02_numpad.dart
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'dart:io';
import 'dart:math';
// down is positive
// right is positive
Map<Point, String> NUMBER_PAD = {
Point(0, 0): "1",
Point(1, 0): "2",
Point(2, 0): "3",
Point(0, 1): "4",
Point(1, 1): "5",
Point(2, 1): "6",
Point(0, 2): "7",
Point(1, 2): "8",
Point(2, 2): "9",
};
// down is positive
// right is positive
Map<Point, String> DIAMOND_NUMBER_PAD = {
// first row
Point(0, -2): "1",
// second row
Point(-1, -1): "2",
Point(0, -1): "3",
Point(1, -1): "4",
// third row
Point(-2, 0): "5",
Point(-1, 0): "6",
Point(0, 0): "7",
Point(1, 0): "8",
Point(2, 0): "9",
// fourth row
Point(-1, 1): "A",
Point(0, 1): "B",
Point(1, 1): "C",
// fifth row
Point(0, 2): "D",
};
// ########
// SOLUTION
// ########
String useNumPad(
List<String> directions,
Map<Point, String> numPad,
Point<int> startPosition,
) {
var currentPosition = startPosition;
Point<int> possiblePosition;
String dialedNumber = "";
for (final direction in directions) {
for (final nextMove in direction.split("")) {
switch (nextMove) {
case 'U':
possiblePosition = Point(currentPosition.x, currentPosition.y - 1);
break;
case 'D':
possiblePosition = Point(currentPosition.x, currentPosition.y + 1);
break;
case 'L':
possiblePosition = Point(currentPosition.x - 1, currentPosition.y);
break;
case 'R':
possiblePosition = Point(currentPosition.x + 1, currentPosition.y);
break;
default:
throw Exception("should not get here");
}
if (numPad.containsKey(possiblePosition))
currentPosition = possiblePosition;
}
dialedNumber += numPad[currentPosition]!;
}
return dialedNumber;
}
String part1(puzzleInput) {
var directions = parseInput(puzzleInput);
return useNumPad(directions, NUMBER_PAD, Point(1, 1));
}
String part2(puzzleInput) {
var directions = parseInput(puzzleInput);
return useNumPad(directions, DIAMOND_NUMBER_PAD, Point(-2, 0));
}
// ###########
// RUN PROGRAM
// ###########
List<String> parseInput(String puzzleInput) {
return puzzleInput.trim().split("\n").map((item) => item.trim()).toList();
}
const TEST_INPUT = """ULL
RRDDD
LURDL
UUUUD
""";
String puzzleInput = File('data/day02_input.txt').readAsStringSync();
void main() {
// part 1
assert(part1(TEST_INPUT) == "1985");
final stopwatchPart1 = Stopwatch()..start();
print("part 1: ${part1(puzzleInput)}");
stopwatchPart1.stop();
print("Elapsed time: ${stopwatchPart1.elapsed}");
// part 2
assert(part2(TEST_INPUT) == "5DB3");
final stopwatchPart2 = Stopwatch()..start();
print("part 2: ${part2(puzzleInput)}");
stopwatchPart2.stop();
print("Elapsed time: ${stopwatchPart2.elapsed}");
}