diff options
Diffstat (limited to 'day03/part1.rb')
-rw-r--r-- | day03/part1.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/day03/part1.rb b/day03/part1.rb new file mode 100644 index 0000000..0ae732b --- /dev/null +++ b/day03/part1.rb @@ -0,0 +1,19 @@ +def priority(ch) + if ch >= ?a && ch <= ?z + ch.ord - 96 + elsif ch >= ?A && ch <= ?Z + ch.ord - 64 + 26 + end +end + +backpacks = $stdin.readlines.map do |line| + line = line.strip + half = line.length / 2 + [line[0...half].chars, line[half..-1].chars] +end + +commons = backpacks.map do |f, s| + (f & s)[0] +end + +puts commons.map{|ch|priority(ch)}.sum |