-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.d
80 lines (68 loc) · 1.59 KB
/
day02.d
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
module day02;
import std.algorithm;
import std.array;
import std.conv;
import std.stdio;
import std.string;
struct Instruction {
string direction;
int amount;
}
struct Position {
int x;
int y;
int aim;
}
void main() {
Instruction[] instructions = File("input/day02.txt")
.byLine()
.map!(to!string)
.map!strip
.filter!(line => line.length > 0)
.map!(function(string line) {
string[] pts = line.split(" ");
return Instruction(pts[0], pts[1].to!int);
})
.array();
part1(instructions);
part2(instructions);
}
void part1(Instruction[] instructions) {
auto pos = Position();
foreach (Instruction i; instructions) {
switch(i.direction) {
case "forward":
pos.x += i.amount;
break;
case "down":
pos.y += i.amount;
break;
case "up":
pos.y -= i.amount;
break;
default:
writeln("Unknown instruction: ", i.direction);
}
}
writeln(pos.x * pos.y);
}
void part2(Instruction[] instructions) {
auto pos = Position();
foreach (Instruction i; instructions) {
switch (i.direction) {
case "forward":
pos.x += i.amount;
pos.y += pos.aim * i.amount;
break;
case "down":
pos.aim += i.amount;
break;
case "up":
pos.aim -= i.amount;
break;
default:
writeln("Unknown instruction: ", i.direction);
}
}
writeln(pos.x * pos.y);
}