Add solution for 2015 day 01

This commit is contained in:
Patrick Auernig 2021-02-07 05:52:14 +01:00
commit 9ee16f6e3b
4 changed files with 66 additions and 0 deletions

15
.editorconfig Normal file
View File

@ -0,0 +1,15 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
[*.rb]
indent_size = 2

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,9 @@
(())
()()
(((
(()(()(
))(((((
())
))(
)))
)())())

41
2015/day-01/main.rb Executable file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env ruby
require "pathname"
INPUTS = ["inputs/sample.txt", "inputs/puzzle.txt"].map { |x| Pathname(x) }
def match_paren(char)
case char
when "(" then 1
when ")" then -1
end
end
def solve_part_1(content)
content.chars.sum { |x| match_paren(x) }
end
def solve_part_2(content)
content.chars.each.with_index(1).reduce(0) do |acc, (char, index)|
acc += match_paren(char)
return index if acc == -1
acc
end
nil
end
def main(files)
files.each do |file|
puts "File: #{file}"
file.read.lines do |line|
next if line.empty?
result = solve_part_1(line.chomp)
puts " Floor: #{result}"
result = solve_part_2(line.chomp)
puts " Basement Floor Position: #{result}"
puts " -" * 15
end
end
end
main(INPUTS)