Skip to content

Commit

Permalink
dec-15 pt2 (#17)
Browse files Browse the repository at this point in the history
pt2 was more tedious than anticipated.
  • Loading branch information
srecnig committed Dec 16, 2023
1 parent 2d628d9 commit 3f33620
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 3 deletions.
40 changes: 40 additions & 0 deletions lib/dec-15/lens_library.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,44 @@ def self.hash(str)
current_value % 256
end
end

Box = Struct.new(:multiplier, :content)
Lens = Struct.new(:label, :focal_length)

class HashMap
def initialize
@boxes = {}
256.times do |i|
@boxes[i] = Box.new(i + 1, [])
end
end

def [](key)
@boxes[LensLibrary.hash(key)]
end

def operate!(lens, operation)
box = @boxes[LensLibrary.hash(lens.label)]
lens_locations = box.content.each.with_index.with_object([]) do |(l, i), locations|
locations << i if l.label == lens.label
end
if operation == :remove
lens_locations.reverse.each { |index| box.content.delete_at(index) }
elsif lens_locations.empty?
box.content << lens
else
lens_locations.each { |index| box.content[index] = lens }
end
end

def focusing_power(key)
box = @boxes[LensLibrary.hash(key)]
box_multiplier = box.multiplier
key_index = box.content.find_index { |l| l.label == key }
slot_multiplier = key_index.nil? ? 0 : key_index + 1
focal_length = key_index.nil? ? 0 : box.content[slot_multiplier - 1].focal_length

box_multiplier * slot_multiplier * focal_length
end
end
end
24 changes: 21 additions & 3 deletions lib/dec-15/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@ def main1(filepath)
end

def main2(filepath)
something = File.readlines(File.join(File.dirname(__FILE__), filepath), chomp: true)
p something
single_line = File.readlines(File.join(File.dirname(__FILE__), filepath), chomp: true)
hash_map = LensLibrary::HashMap.new
operations = single_line[0].split(',').map do |op_string|
op = op_string[-1] == '-' ? :remove : :set
if op == :remove
key = op_string[..-2]
value = nil
else
set_arr = op_string.split('=')
key = set_arr[0]
value = set_arr[1].to_i
end
[LensLibrary::Lens.new(key, value), op]
end
operations.each { |(lens, op)| hash_map.operate!(lens, op) }
# || # .inject(0) { |_op| [] }
result = operations.map { |(lens, _op)| lens.label }.uniq.inject(0) do |r, key|
r + hash_map.focusing_power(key)
end
p result
end

main1('input.txt')
# main2('input1.txt')
main2('input.txt')
22 changes: 22 additions & 0 deletions test/dec-15/lens_library_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,26 @@ def test_hash
assert_equal 214, LensLibrary.hash('pc=6')
assert_equal 231, LensLibrary.hash('ot=7')
end

def test_hash_map
hash_map = HashMap.new
lens = Lens.new('rn', 1)
hash_map.operate!(lens, :set)
assert_equal [Lens.new('rn', 1)], hash_map['rn'].content
lens = Lens.new('cm', nil)
hash_map.operate!(lens, :remove)
assert_equal [Lens.new('rn', 1)], hash_map['rn'].content
lens = Lens.new('qp', 3)
hash_map.operate!(lens, :set)
assert_equal [Lens.new('rn', 1)], hash_map['rn'].content
assert_equal [Lens.new('qp', 3)], hash_map['qp'].content
lens = Lens.new('cm', 2)
hash_map.operate!(lens, :set)
assert_equal [Lens.new('rn', 1), Lens.new('cm', 2)], hash_map['rn'].content
assert_equal [Lens.new('rn', 1), Lens.new('cm', 2)], hash_map['cm'].content
assert_equal [Lens.new('qp', 3)], hash_map['qp'].content
assert_equal 1, hash_map.focusing_power('rn')
assert_equal 4, hash_map.focusing_power('cm')
assert_equal 6, hash_map.focusing_power('qp')
end
end

0 comments on commit 3f33620

Please sign in to comment.