require'benchmark'classBoardingPassdefself.from_binary(binary_position)rows=binary_position[0...7].tr("FB","01")columns=binary_position[7..].tr("LR","01")BoardingPass.new((rows+columns).to_i(2))enddefinitialize(seat)self.seat=seatenddefto_iself.seatendprivateattr_accessor:seatenddeffind_missing_id(passes)seat_ids=passes.map(&:to_i).sort(seat_ids.first..seat_ids.last).to_a.each_with_indexdo|expected_id,i|returnexpected_idifseat_ids[i]!=expected_idendendBenchmark.bmbmdo|b|b.report(:to_i_base_2)dopasses=File.readlines('input.txt').mapdo|line|BoardingPass.from_binary(line.chomp)endputsfind_missing_id(passes)endb.report(:binsearch)dodefbinary_position_search(l:,r:,position:)position.chars.inject([0,2**position.length-1])do|(low,high),half|mid=low+((high-low)/2.0)ifhalf==l[low,mid.floor]elsifhalf==r[mid.ceil,high]elseraise"Position character #{half} not expected. Expected #{l} or #{r}."endend.firstendpasses=File.readlines('input.txt').mapdo|line|binary_position=line.chomprow=binary_position_search(l: 'F',r: 'B',position: binary_position[0...7])column=binary_position_search(l: 'L',r: 'R',position: binary_position[7..])BoardingPass.new(row*8+column)endputsfind_missing_id(passes)endend
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Today I have two implementations in Ruby for ya.