Skip to content

Commit 507b216

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

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

2023/ruby/day15.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require_relative 'day'
2+
3+
class Day15 < Day # >
4+
5+
# @example
6+
# day.part1 #=> 1320
7+
def part1
8+
initialization_sequence
9+
.reduce(0) { |sum, step| sum += HASH.hash(step) }
10+
end
11+
12+
# example
13+
# day.part2 #=> 'how are you'
14+
def part2
15+
end
16+
17+
# @example parses_input
18+
# h = new(Day15::EXAMPLE_INPUT)
19+
# h.initialization_sequence #=> %w{rn=1 cm- qp=3 cm=2 qp- pc=4 ot=9 ab=5 pc- pc=6 ot=7}
20+
def initialization_sequence
21+
@initialization_sequence ||=
22+
input.tr("\n", "").split(",")
23+
end
24+
25+
EXAMPLE_INPUT = <<~INPUT
26+
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
27+
INPUT
28+
end
29+
30+
#class Holiday_ASCII_String_Helper
31+
module HASH
32+
# @example
33+
# HASH.hash("") #=> 0
34+
# @example HASH
35+
# HASH.hash("HASH") #=> 52
36+
# @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.
55+
end
56+
end
57+
end

0 commit comments

Comments
 (0)