123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- require 'set'
- @input = File.read("3.input").split("\n")
- def prio(char)
- if char.ord >= 'a'.ord then
- return char.ord - 'a'.ord + 1
- else
- return char.ord - 'A'.ord + 27
- end
- end
- def part1()
- score = 0
- for sack in @input do
- left, right = sack.slice(0, sack.length / 2), sack.slice(sack.length / 2, sack.length)
- right.each_char do |char|
- if left.include? char then
- score += prio(char)
- break
- end
- end
- end
- puts score
- end
- def part2()
- score = 0
- @input.each_slice(3) do |group|
- sets = group.map {|sack| Set.new(sack.chars)}
- result = sets[0] & sets[1] & sets[2]
- score += prio(result.to_a[0])
- end
- puts score
- end
- part1()
- part2()
|