START_TIME = Time.utc.to_unix_ms TL = 900 RND = Random.new(2) X0 = (ENV["X0"]? || 41).to_i * 0.1 X1 = (ENV["X1"]? || 3).to_i * 0.1 X2 = (ENV["X2"]? || 21).to_i * 0.1 X3 = (ENV["X3"]? || 25).to_i * 0.1 X4 = (ENV["X4"]? || 45).to_i * 0.1 X5 = (ENV["X5"]? || 2).to_i * 0.1 macro debug(msg) {% if flag?(:local) %} STDERR.puts({{msg}}) {% end %} end macro debugf(format_string, *args) {% if flag?(:local) %} STDERR.printf({{format_string}}, {{*args}}) {% end %} end def crash(msg, caller_line = __LINE__) puts "[ERROR] line #{caller_line}: #{msg}" exit end macro assert(cond, msg = "", caller_line = __LINE__) {% if flag?(:local) %} if !({{cond}}) crash({{msg}}, {{caller_line}}) end {% end %} end class Solver def initialize @row_cnt = Array(Array(Int32)).new(6) { Array.new(6, 0) } @col_cnt = Array(Array(Int32)).new(6) { Array.new(6, 0) } @row_sum = Array(Array(Int32)).new(6) { Array.new(6, 0) } @col_sum = Array(Array(Int32)).new(6) { Array.new(6, 0) } @row_miss = Array(Array(Bool)).new(6) { Array.new(6, false) } @col_miss = Array(Array(Bool)).new(6) { Array.new(6, false) } @used = Array(Array(Bool)).new(6) { Array.new(6, false) } end def solve place = Array.new(36) { |i| i } 35.times do |i| dices = read_line.split.map(&.to_i) ds = Array.new(6) { |i| dices.count(i + 1) } best_r = 0 best_c = 0 best_v = -10000000.0 if i < 1 best_r = best_c = i else 6.times do |r| 6.times do |c| next if @used[r][c] v = 0.0 6.times do |e| if ds[e] > 0 v += @row_miss[r][e] ? -ds[e] * X0 : ds[e] * X1 + @row_sum[r][e] * X2 v += @col_miss[c][e] ? -ds[e] * X0 : ds[e] * X1 + @col_sum[c][e] * X2 else v += @row_miss[r][e] ? X3 : -@row_sum[r][e] * X4 - @row_cnt[r][e] * X5 v += @col_miss[c][e] ? X3 : -@col_sum[c][e] * X4 - @col_cnt[r][e] * X5 end end if v > best_v best_r = r best_c = c best_v = v end end end end @used[best_r][best_c] = true 6.times do |e| if ds[e] > 0 @row_cnt[best_r][e] += 1 @row_sum[best_r][e] += ds[e] @col_cnt[best_c][e] += 1 @col_sum[best_c][e] += ds[e] else @row_miss[best_r][e] = true @col_miss[best_c][e] = true end end puts "#{best_r + 1} #{best_c + 1}" STDOUT.flush end {% if flag?(:local) %} score = calc_score() debug("final_score=#{score}") {% end %} end def calc_score mr = 0 mc = 0 6.times do |i| 6.times do |j| if !@used[i][j] mr = i mc = j end end end calc_score_dfs(mr, mc, 0, Array.new(6, 0)) end def calc_score_dfs(mr, mc, i, cnt) ret = 0 if i == 6 6.times do |e| if cnt[e] > 0 @row_cnt[mr][e] += 1 @row_sum[mr][e] += cnt[e] @col_cnt[mc][e] += 1 @col_sum[mc][e] += cnt[e] end 6.times do |j| if @row_cnt[j][e] == 6 ret += @row_sum[j][e] - 3 end if @col_cnt[j][e] == 6 ret += @col_sum[j][e] - 3 end end if cnt[e] > 0 @row_cnt[mr][e] -= 1 @row_sum[mr][e] -= cnt[e] @col_cnt[mc][e] -= 1 @col_sum[mc][e] -= cnt[e] end end debug(ret) else 6.times do |e| cnt[e] += 1 ret += calc_score_dfs(mr, mc, i + 1, cnt) cnt[e] -= 1 end end return ret end end def main solver = Solver.new solver.solve end main