Skip to content

Commit 6b28622

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

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

2022/ruby/day03.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Day03
2+
def self.go
3+
day = new
4+
puts "Part 1: #{day.part1}"
5+
puts "Part 2: #{day.part2}"
6+
end
7+
8+
def initialize(input=nil)
9+
@input = input || real_input
10+
end
11+
12+
PRIORITIES = [nil] + ('a'..'z').to_a + ('A'..'Z').to_a
13+
14+
# @example
15+
# day.part1 #=> 157
16+
def part1
17+
@input
18+
.split("\n")
19+
.map{ |line| line.chars }
20+
.map{ |items| items
21+
.each_slice( (items.length/2.0).floor )
22+
.to_a
23+
.reduce(&:&)
24+
}
25+
.flatten
26+
.map{ |common_item| PRIORITIES.index(common_item) }
27+
.reduce(&:+)
28+
end
29+
30+
def part2
31+
end
32+
33+
def real_input
34+
File.read('../inputs/day03-input.txt')
35+
end
36+
37+
EXAMPLE_INPUT = <<~INPUT
38+
vJrwpWtwJgWrhcsFMMfFFhFp
39+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
40+
PmmdzqPrVvPwwTWBwg
41+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
42+
ttgJtRGJQctTZtZT
43+
CrZsJsPPZsGzwwsLwLmpwMDw
44+
INPUT
45+
end

0 commit comments

Comments
 (0)