Skip to content

Commit 96bc8af

Browse files
committed
Day 01
1 parent 481401c commit 96bc8af

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

01_equal_to_next.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def match(input, dist:)
2+
# Doesn't matter whether we go forward or backward,
3+
# (either way we get a yes on one half of the matching pair)
4+
# so let's go backward to take advantage of negative index.
5+
input.each_char.select.with_index { |c, i| c == input[i - dist] }.sum(&method(:Integer))
6+
end
7+
8+
input = (!ARGV.empty? && ARGV.first.match?(/^\d+$/) ? ARGV.first : ARGF.read).chomp.freeze
9+
10+
[1, input.size / 2].each { |n| puts match(input, dist: n) }

0 commit comments

Comments
 (0)