Skip to content

Instantly share code, notes, and snippets.

@gouf
Last active July 10, 2024 01:55
Show Gist options
  • Save gouf/e87bdd2c507f41244ad7db33d4965c2a to your computer and use it in GitHub Desktop.
Save gouf/e87bdd2c507f41244ad7db33d4965c2a to your computer and use it in GitHub Desktop.
class Employee
attr_reader :number, :name
def initialize(number, name)
@number = number.to_i
@name = name.to_s
end
end
class EmployeeRecorder
attr_reader :employees
def initialize
@employees = []
end
def make(number, name)
@employees << Employee.new(number, name)
nil
end
def getnum(index)
@employees.each.with_index(1) do |employee, i|
return employee.number if index.to_i.eql?(i)
end
end
def getname(index)
@employees.each.with_index(1) do |employee, i|
return employee.name if index.to_i.eql?(i)
end
end
end
solver = Solver.new(Solver.read_input!)
puts solver.solve!
class OrderStringAnalyzer
attr_reader :order_string, :employee_recorder
def initialize(order_string)
@order_string = order_string
@employee_recorder = EmployeeRecorder.new
@execution_histories = []
end
def read_and_execute!
@execution_histories =
order_string.lines.map do |line|
command, *args = line.split
employee_recorder.public_send(command, *args)
end
nil
end
def execution_histories
@execution_histories.compact
end
end
describe OrderStringAnalyzer do
describe '#read_and_execute!' do
context 'order string pattern 1' do
let(:order_string) do
<<~ORDER_STRING.lines.map(&:strip).join("\n")
make 10 nana
getnum 1
getname 1
ORDER_STRING
end
it 'reads and executes the commands' do
analyzer = OrderStringAnalyzer.new(order_string)
analyzer.read_and_execute!
expect(analyzer.employee_recorder.getnum(1)).to eq(10)
end
context 'when the command was done successfully' do
it 'can read data from the execution histories' do
analyzer = OrderStringAnalyzer.new(order_string)
analyzer.read_and_execute!
result = [10, 'nana']
expect(analyzer.execution_histories).to eq(result)
end
end
end
context 'order string pattern 2' do
let(:order_string) do
<<~ORDER_STRING.lines.map(&:strip).join("\n")
make 2742 mako
getnum 1
make 2782 taisei
getname 2
make 31 megumi
getname 1
getname 3
ORDER_STRING
end
it 'reads and executes the commands' do
analyzer = OrderStringAnalyzer.new(order_string)
analyzer.read_and_execute!
expect(analyzer.employee_recorder.getnum(1)).to eq(2742)
end
context 'when the command was done successfully' do
it 'can read data from the execution histories' do
analyzer = OrderStringAnalyzer.new(order_string)
analyzer.read_and_execute!
result = [2742, 'taisei', 'mako', 'megumi']
expect(analyzer.execution_histories).to eq(result)
end
end
end
end
end
class Solver
attr_accessor :order_string
def initialize(order_string)
@order_string = order_string
end
def solve!
OrderStringAnalyzer.new(order_string)
.tap { _1.read_and_execute! }
.then { _1.execution_histories }
end
class << self
def read_input!
gets.to_i.times.map do
gets.chomp
end.join("\n")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment