Add solution for 2015 day 01
This commit is contained in:
commit
9ee16f6e3b
15
.editorconfig
Normal file
15
.editorconfig
Normal 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
|
1
2015/day-01/inputs/puzzle.txt
Normal file
1
2015/day-01/inputs/puzzle.txt
Normal file
File diff suppressed because one or more lines are too long
9
2015/day-01/inputs/sample.txt
Normal file
9
2015/day-01/inputs/sample.txt
Normal file
@ -0,0 +1,9 @@
|
||||
(())
|
||||
()()
|
||||
(((
|
||||
(()(()(
|
||||
))(((((
|
||||
())
|
||||
))(
|
||||
)))
|
||||
)())())
|
41
2015/day-01/main.rb
Executable file
41
2015/day-01/main.rb
Executable 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)
|
Loading…
Reference in New Issue
Block a user