blob: 4e66caab442d4199beede992271b51ad5e769dd2 (
plain) (
blame)
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
|
#!/usr/bin/env raku
unit sub MAIN (
Int $part where * == 1|2 = 1 #= part to run (1 or 2)
);
my @instructions = "input".IO.lines;
# east/west, north/south. east/north are positive.
my Int @ship[2] = 0, 0;
my token Instruction { (N|S|E|W|L|R|F) (\d+) };
if $part == 1 {
my Str @directions[4] = <E S W N>;
my Int $facing = 0;
for @instructions -> $instruction {
if $instruction ~~ &Instruction -> $match {
given $match[0] {
when 'N' { @ship[1] += $match[1]; }
when 'S' { @ship[1] -= $match[1]; }
when 'E' { @ship[0] += $match[1]; }
when 'W' { @ship[0] -= $match[1]; }
when 'L' {
for 1..($match[1] / 90).Int {
if $facing == 0 {
$facing = 3;
} else {
$facing -= 1;
}
}
}
when 'R' {
for 1..($match[1] / 90).Int {
if $facing == 3 {
$facing = 0;
} else {
$facing += 1;
}
}
}
when 'F' {
given @directions[$facing] {
when 'N' { @ship[1] += $match[1]; }
when 'S' { @ship[1] -= $match[1]; }
when 'E' { @ship[0] += $match[1]; }
when 'W' { @ship[0] -= $match[1]; }
}
}
}
}
}
} elsif $part == 2 {
# east/west, north/south. east/north are positive.
my Int @waypoint[2] = 10, 1;
for @instructions -> $instruction {
if $instruction ~~ &Instruction -> $match {
given $match[0] {
when 'N' { @waypoint[1] += $match[1]; }
when 'S' { @waypoint[1] -= $match[1]; }
when 'E' { @waypoint[0] += $match[1]; }
when 'W' { @waypoint[0] -= $match[1]; }
when 'L' {
for 1..($match[1] / 90).Int {
@waypoint = -@waypoint[1], @waypoint[0];
}
}
when 'R' {
for 1..($match[1] / 90).Int {
@waypoint = @waypoint[1], -@waypoint[0];
}
}
when 'F' {
@ship[0] += @waypoint[0] * $match[1];
@ship[1] += @waypoint[1] * $match[1];
}
}
}
}
}
say "Part $part: ", [+] @ship.map(*.abs);
|