Skip to content

Commit 7cb7fd8

Browse files
author
Robb Kidd
committed
2022 day 3 - part 2
1 parent 6b28622 commit 7cb7fd8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

2022/ruby/day03.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def initialize(input=nil)
99
@input = input || real_input
1010
end
1111

12-
PRIORITIES = [nil] + ('a'..'z').to_a + ('A'..'Z').to_a
12+
PRIORITIES = [:dont_be_off_by_one] + ('a'..'z').to_a + ('A'..'Z').to_a
1313

1414
# @example
1515
# day.part1 #=> 157
@@ -27,7 +27,19 @@ def part1
2727
.reduce(&:+)
2828
end
2929

30+
# @example
31+
# day.part2 #=> 70
3032
def part2
33+
@input
34+
.split("\n")
35+
.map{ |line| line.chars }
36+
.each_slice(3)
37+
.map { |group_items|
38+
group_items[0] & (group_items[1] & group_items[2])
39+
}
40+
.flatten
41+
.map{ |badge| PRIORITIES.index(badge) }
42+
.reduce(&:+)
3143
end
3244

3345
def real_input

0 commit comments

Comments
 (0)