-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.dart
70 lines (59 loc) · 1.22 KB
/
day2.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
import './utils.dart';
class Command {
final String name;
final int value;
@override
String toString() {
return "Cmd($name, $value)";
}
Command(this.name, this.value);
}
class Position {
int x;
int y;
int aim;
Position(this.x, this.y, this.aim);
@override
String toString() {
return "Pos(x:$x, y:$y, aim:$aim)";
}
applyCommand(Command cmd) {
switch (cmd.name) {
case 'down':
{
this.aim += cmd.value;
}
break;
case 'up':
{
this.aim -= cmd.value;
}
break;
case 'forward':
{
this.x += cmd.value;
this.y += this.aim * cmd.value;
}
break;
}
}
}
List<Command> linesToCommands(List<String> lines) {
return lines.map((line) {
List<String> args = line.trim().split(' ');
return Command(args[0], int.parse(args[1]));
}).toList();
}
int solve(List<String> lines) {
List<Command> commands = linesToCommands(lines);
Position pos = Position(0, 0, 0);
commands.forEach((cmd) {
pos.applyCommand(cmd);
});
return pos.x * pos.y;
}
void main() async {
List<String> lines = await readlines('day2.txt');
int result = solve(lines);
print(result);
}