From a216d73e9e77a902d334c006d4801a88c7e3af9d Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Thu, 9 Dec 2021 09:19:27 +0100 Subject: [PATCH] feat: Day 2 --- puzzles/02_1/main.rb | 19 +++++++++++++++++++ puzzles/02_2/main.rb | 21 +++++++++++++++++++++ run.rb | 8 +++++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 puzzles/02_1/main.rb create mode 100644 puzzles/02_2/main.rb diff --git a/puzzles/02_1/main.rb b/puzzles/02_1/main.rb new file mode 100644 index 0000000..c12fba8 --- /dev/null +++ b/puzzles/02_1/main.rb @@ -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 diff --git a/puzzles/02_2/main.rb b/puzzles/02_2/main.rb new file mode 100644 index 0000000..a601867 --- /dev/null +++ b/puzzles/02_2/main.rb @@ -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 diff --git a/run.rb b/run.rb index 4302903..dbba713 100755 --- a/run.rb +++ b/run.rb @@ -5,7 +5,13 @@ part = ARGV[1] ? ARGV[1] : '1' require './puzzles/%s_%s/main.rb' % [puzzle, part] -f = File.open('./puzzles/%s_%s/input.txt' % [puzzle, part], 'r') +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