-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.rb
49 lines (42 loc) · 1.17 KB
/
2.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
# made with Copilot
# read all the lines of the file "2"
input = File.readlines("2")
# let x y be 0
x = 0
y = 0
# for each line, split the line by spaces and convert the second value to an integer
# if the first value is "forward", add second value to x
# if the first value is "down", add second value to y
# if the first value is "up", minus second value to y
# after the for loop, puts x times y
input.each do |line|
line = line.split(" ")
if line[0] == "forward"
x += line[1].to_i
elsif line[0] == "down"
y += line[1].to_i
elsif line[0] == "up"
y -= line[1].to_i
end
end
puts x * y
aim = 0
x = 0
y = 0
# for each line, split the line by spaces and convert the second value to an integer
# if the first value is "forward", add second value to x and add second value * aim to y
# if the first value is "down", add second value to aim
# if the first value is "up", minus second value to aim
# after the for loop, puts x times y
input.each do |line|
line = line.split(" ")
if line[0] == "forward"
x += line[1].to_i
y += line[1].to_i * aim
elsif line[0] == "down"
aim += line[1].to_i
elsif line[0] == "up"
aim -= line[1].to_i
end
end
puts x * y