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.

Revisions

  1. gouf revised this gist Jul 10, 2024. 1 changed file with 60 additions and 0 deletions.
    60 changes: 60 additions & 0 deletions order_string_analyzer_spec.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    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
  2. gouf created this gist Jul 10, 2024.
    8 changes: 8 additions & 0 deletions employee.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    class Employee
    attr_reader :number, :name

    def initialize(number, name)
    @number = number.to_i
    @name = name.to_s
    end
    end
    25 changes: 25 additions & 0 deletions employee_recorder.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    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
    2 changes: 2 additions & 0 deletions main.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    solver = Solver.new(Solver.read_input!)
    puts solver.solve!
    23 changes: 23 additions & 0 deletions order_string_analyzer.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    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
    22 changes: 22 additions & 0 deletions solver.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    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