feat: Day 2
This commit is contained in:
parent
6835506dea
commit
a216d73e9e
3 changed files with 47 additions and 1 deletions
19
puzzles/02_1/main.rb
Normal file
19
puzzles/02_1/main.rb
Normal file
|
@ -0,0 +1,19 @@
|
|||
def run(f)
|
||||
horizontal = 0
|
||||
depth = 0
|
||||
|
||||
f.each do |line|
|
||||
command, distance = line.split(' ')
|
||||
distance = Integer(distance)
|
||||
case command
|
||||
when 'forward'
|
||||
horizontal += distance
|
||||
when 'down'
|
||||
depth += distance
|
||||
when 'up'
|
||||
depth -= distance
|
||||
end
|
||||
end
|
||||
|
||||
return horizontal * depth
|
||||
end
|
21
puzzles/02_2/main.rb
Normal file
21
puzzles/02_2/main.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
def run(f)
|
||||
horizontal = 0
|
||||
depth = 0
|
||||
aim = 0
|
||||
|
||||
f.each do |line|
|
||||
command, distance = line.split(' ')
|
||||
distance = Integer(distance)
|
||||
case command
|
||||
when 'forward'
|
||||
horizontal += distance
|
||||
depth += (aim * distance)
|
||||
when 'down'
|
||||
aim += distance
|
||||
when 'up'
|
||||
aim -= distance
|
||||
end
|
||||
end
|
||||
|
||||
return horizontal * depth
|
||||
end
|
6
run.rb
6
run.rb
|
@ -5,7 +5,13 @@ part = ARGV[1] ? ARGV[1] : '1'
|
|||
|
||||
require './puzzles/%s_%s/main.rb' % [puzzle, part]
|
||||
|
||||
if part == '1'
|
||||
f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r')
|
||||
elsif File.exists?('./puzzles/%s_%s/input.txt' % [puzzle, part])
|
||||
f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r')
|
||||
else
|
||||
f = File.open('./puzzles/%s_1/input.txt' % [puzzle, part], 'r')
|
||||
end
|
||||
|
||||
res = run f
|
||||
puts "Result: " + res.to_s
|
||||
|
|
Loading…
Add table
Reference in a new issue