Created
March 13, 2014 04:05
-
-
Save odaillyjp/9521741 to your computer and use it in GitHub Desktop.
オフラインリアルタイムどう書く #7 の問題を解いた http://nabetani.sakura.ne.jp/hena/ord7xysort/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort_with_other_row(table, idx) | |
row = table[idx] | |
# [ソート前のカラムのインデックス値, ...] | |
ex_sort_idxs = row.map | |
.with_index { |e, idx| [e, idx] } | |
.sort_by | |
.with_index { |cell, idx| [cell.first, idx] } | |
.map(&:last) | |
next_table = Array.new(6).map { Array.new } | |
# 全て行のカラムを ex_sort_idxs の並び順に従って、並び替える | |
ex_sort_idxs.each do |column_idx| | |
table.each_with_index { |row, row_idx| next_table[row_idx] << row[column_idx] } | |
end | |
next_table | |
end | |
def solve(str) | |
table = [ | |
[4,1,4,2,1,3], | |
[7,3,2,0,5,0], | |
[2,3,6,0,6,7], | |
[6,4,5,7,5,1], | |
[3,1,6,6,2,4], | |
[6,0,5,5,5,1] | |
] | |
str.chars.each do |char| | |
if char =~ %r{[A-F]} | |
idx = char.ord - "A".ord | |
table = sort_with_other_row(table, idx) | |
end | |
if char =~ %r{[u-z]} | |
table = table.transpose | |
idx = char.ord - "u".ord | |
table = sort_with_other_row(table, idx).transpose | |
end | |
end | |
table.first.join | |
end | |
require 'rspec' | |
describe "#solve" do | |
it { expect(solve("AvEx")).to eq "305027" } | |
it { expect(solve("A")).to eq "112344" } | |
it { expect(solve("C")).to eq "241413" } | |
it { expect(solve("F")).to eq "134214" } | |
it { expect(solve("u")).to eq "236067" } | |
it { expect(solve("w")).to eq "732050" } | |
it { expect(solve("y")).to eq "414213" } | |
it { expect(solve("yx")).to eq "732050" } | |
it { expect(solve("ux")).to eq "236067" } | |
it { expect(solve("FAuFBvFCw")).to eq "300527" } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment