-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.rb
61 lines (53 loc) · 1.56 KB
/
day2.rb
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
require_relative "spec_helper"
Coords = Struct.new(:depth, :horizontal_position, :aim)
def compute_coords(commands)
coords = Coords.new(0, 0, 0)
commands.each do |command|
order, unit = command.split(" ")
unit = unit.to_i
if order == "forward"
coords.horizontal_position += unit
coords.depth += coords.aim * unit
elsif order == "down"
coords.aim += unit
elsif order == "up"
coords.aim -= unit
end
end
coords
end
RSpec.describe "Day 2" do
let(:example) do
<<~INPUT
forward 5
down 5
forward 8
up 3
down 8
forward 2
INPUT
end
skip "part 1 - example" do
coords = compute_coords(example.split("\n"))
expect(coords.depth).to eql 10
expect(coords.horizontal_position).to eql 15
end
skip "part 1 - answer" do
coords = compute_coords(File.read("day2_input.txt").split("\n"))
expect(coords.depth).to eql 1091
expect(coords.horizontal_position).to eql 1927
expect(coords.depth * coords.horizontal_position).to eql 1091 * 1927 # 2102357
end
specify "part 2 - example" do
coords = compute_coords(example.split("\n"))
expect(coords.depth).to eql 60
expect(coords.horizontal_position).to eql 15
end
specify "part 2 - answer" do
coords = compute_coords(File.read("day2_input.txt").split("\n"))
expect(coords.depth).to eql 1090312
expect(coords.horizontal_position).to eql 1927
expect(coords.depth * coords.horizontal_position).to eql 1090312 * 1927 # 2102357
end
end
# puts compute_coords(File.read("day2_input.txt").split("\n"))