-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.koto
71 lines (61 loc) · 1.93 KB
/
12.koto
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
# https://adventofcode.com/2020/day/12
from geometry import vec2
north = vec2 0, 1
south = vec2 0, -1
east = vec2 1, 0
west = vec2 -1, 0
parse_instructions = |input|
input.lines()
.each |line|
action = line[0]
value = line[1..].to_number()
action, value
.to_tuple()
rotate_clockwise = |xy, degrees|
match degrees
90 then vec2 xy.y(), -xy.x()
180 then -xy
270 then vec2 -xy.y(), xy.x()
run_instructions_part_one = |instructions|
ship, direction = (vec2 0, 0), east
for action, value in instructions
match action
"N" then ship += north * value
"S" then ship += south * value
"E" then ship += east * value
"W" then ship += west * value
"L" then direction = rotate_clockwise direction, (360 - value)
"R" then direction = rotate_clockwise direction, value
"F" then ship += direction * value
(ship.x().abs() + ship.y().abs()).to_int()
run_instructions_part_two = |instructions|
ship, waypoint = (vec2 0, 0), (vec2 10, 1)
for action, value in instructions
match action
"N" then waypoint += north * value
"S" then waypoint += south * value
"E" then waypoint += east * value
"W" then waypoint += west * value
"L" then waypoint = rotate_clockwise waypoint, (360 - value)
"R" then waypoint = rotate_clockwise waypoint, value
"F" then ship += waypoint * value
(ship.x().abs() + ship.y().abs()).to_int()
@main = ||
instructions = io.extend_path koto.script_dir, "input", "12"
>> io.read_to_string
>> parse_instructions
print "Part one: ${run_instructions_part_one instructions}" # 319
print "Part two: ${run_instructions_part_two instructions}" # 50157
@tests =
@pre_test: ||
self.instructions = parse_instructions "\
F10
N3
F7
R90
F11
"
@test part_one: ||
assert_eq (run_instructions_part_one self.instructions), 25
@test part_two: ||
assert_eq (run_instructions_part_two self.instructions), 286