AoC2020/day05/solution01.rb

34 lines
741 B
Ruby
Executable File

#!/bin/env ruby
ids = []
File.readlines('./input').each do |line|
line.strip!
row = [0, 127]
col = [0, 7]
step = 64
line[..6].each_char do |char|
row[0] += step if char == 'B'
row[1] -= step if char == 'F'
step /= 2
end
step = 4
line[-3..].each_char do |char|
col[0] += step if char == 'R'
col[1] -= step if char == 'L'
step /= 2
end
id = (row[0] * 8) + col[0]
ids.push(id)
puts "Seat #{line} is at row #{row} and column #{col}. Seat id: #{id}"
end
puts "The highest seat ID is #{ids.max}"
ids.sort!
ids.each_index do |i|
next if i == 0
if ids[i] != (ids[i-1] + 1)
puts "My seat is #{ids[i] - 1}"
break
end
end