Skip to content

Commit a2f083f

Browse files
author
Robb Kidd
committed
day 15, part 2
1 parent 507b216 commit a2f083f

File tree

1 file changed

+54
-27
lines changed

1 file changed

+54
-27
lines changed

2023/ruby/day15.rb

Lines changed: 54 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,42 @@ class Day15 < Day # >
66
# day.part1 #=> 1320
77
def part1
88
initialization_sequence
9-
.reduce(0) { |sum, step| sum += HASH.hash(step) }
9+
.reduce(0) { |sum, step| sum += hash(step) }
1010
end
1111

12-
# example
13-
# day.part2 #=> 'how are you'
12+
# @example
13+
# day.part2 #=> 145
1414
def part2
15+
# "... a series of 256 boxes numbered 0 through 255 ..."
16+
boxes = Array.new(256) { Hash.new }
17+
18+
# "... perform each step in the initialization sequence ..."
19+
initialization_sequence
20+
.each do |step|
21+
label, op, focal_length = /\A(.*)([-,\=])(.*)?\z/.match(step)[1..3]
22+
case op
23+
when "-" ; boxes[hash(label)].delete(label)
24+
when "=" ; boxes[hash(label)].store(label, focal_length.to_i)
25+
else ; raise("lolwut")
26+
end
27+
end
28+
29+
# "To confirm that all of the lenses are installed correctly,
30+
# add up the focusing power of all of the lenses."
31+
boxes
32+
.map.with_index { |box, box_number| # box numbers are 0-255
33+
box
34+
.values # only need the focal lengths not the labels
35+
.map.with_index(1) { |focal_length, slot_number| # box slot numbers start at 1
36+
# "The focusing power of a single lens is the result of multiplying together:
37+
# " - One plus the box number of the lens in question.
38+
# " - The slot number of the lens within the box: 1 for the first lens, ...
39+
# " - The focal length of the lens."
40+
(box_number + 1) * slot_number * focal_length
41+
}
42+
}
43+
.flatten
44+
.reduce(&:+)
1545
end
1646

1747
# @example parses_input
@@ -25,33 +55,30 @@ def initialization_sequence
2555
EXAMPLE_INPUT = <<~INPUT
2656
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
2757
INPUT
28-
end
2958

30-
#class Holiday_ASCII_String_Helper
31-
module HASH
32-
# @example
33-
# HASH.hash("") #=> 0
59+
# @example default_start
60+
# new.hash("") #=> 0
3461
# @example HASH
35-
# HASH.hash("HASH") #=> 52
62+
# new.hash("HASH") #=> 52
3663
# @example from_example_input
37-
# HASH.hash("rn=1") #=> 30
38-
# HASH.hash("cm-") #=> 253
39-
# HASH.hash("qp=3") #=> 97
40-
# HASH.hash("cm=2") #=> 47
41-
# HASH.hash("qp-") #=> 14
42-
# HASH.hash("pc=4") #=> 180
43-
# HASH.hash("ot=9") #=> 9
44-
# HASH.hash("ab=5") #=> 197
45-
# HASH.hash("pc-") #=> 48
46-
# HASH.hash("pc=6") #=> 214
47-
# HASH.hash("ot=7") #=> 231
48-
def self.hash(str="")
49-
str
50-
.each_byte # Determine the ASCII code for the current character of the string.
51-
.inject(0) do |current_value, ord| # start with a current value of 0
52-
current_value += ord # Increase the current value by the ASCII code you just determined.
53-
current_value *= 17 # Set the current value to itself multiplied by 17.
54-
current_value %= 256 # Set the current value to the remainder of dividing itself by 256.
64+
# new.hash("rn=1") #=> 30
65+
# new.hash("cm-") #=> 253
66+
# new.hash("qp=3") #=> 97
67+
# new.hash("cm=2") #=> 47
68+
# new.hash("qp-") #=> 14
69+
# new.hash("pc=4") #=> 180
70+
# new.hash("ot=9") #=> 9
71+
# new.hash("ab=5") #=> 197
72+
# new.hash("pc-") #=> 48
73+
# new.hash("pc=6") #=> 214
74+
# new.hash("ot=7") #=> 231
75+
def hash(str="")
76+
str # "... for each character in the string starting from the beginning:"
77+
.each_byte # "Determine the ASCII code for the current character of the string."
78+
.inject(0) do |current_value, ord| # "... start with a current value of 0."
79+
current_value += ord # "Increase the current value by the ASCII code you just determined."
80+
current_value *= 17 # "Set the current value to itself multiplied by 17."
81+
current_value %= 256 # "Set the current value to the remainder of dividing itself by 256."
5582
end
5683
end
5784
end

0 commit comments

Comments
 (0)