START_TIME = Time.utc.to_unix_ms TL = 1970 IC = (ENV["IC"]? || 50).to_i * 0.01 FC = (ENV["FC"]? || 300).to_i * 0.01 INITIAL_CNT_COEF = (ENV["INI_COEF"]? || 3).to_i PART = (ENV["PART"]? || 20).to_i ROW_MASK = Array.new(6) { |i| 0x3Fi64 << (6 * i) } COL_MASK = Array.new(6) { |i| 0x414141i64 << i } RND = Random.new(2) 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 Result property :pos, :score def initialize(@pos : Array(Int32), @score : Int32) end def to_s(io) pos.each do |p| io << p // 6 + 1 << " " << p % 6 + 1 << "\n" end io end end class Solver @cnt_coef : Int32 def initialize(@dices : Array(Array(Int32))) @dices_sum = Array(Int64).new(36) do |i| v = 0i64 6.times do |j| v |= @dices[i][j].to_i64 << (6 * j) end v end @dices_exist = Array(Int32).new(36) do |i| v = 0 6.times do |j| v |= (@dices[i][j] != 0 ? 1 : 0) << (3 * j) end v end @cnt_coef = INITIAL_CNT_COEF end def solve(timelimit) row_cnt = Array.new(6, 0) col_cnt = Array.new(6, 0) row_sum = Array.new(6, 0i64) col_sum = Array.new(6, 0i64) place = Array.new(36) { |i| i } 6.times do |i| 6.times do |j| row_cnt[i] += @dices_exist[place[i * 6 + j]] row_sum[i] += @dices_sum[place[i * 6 + j]] col_cnt[j] += @dices_exist[place[i * 6 + j]] col_sum[j] += @dices_sum[place[i * 6 + j]] end end row_val = Array.new(6) { |i| eval_line(row_cnt[i], row_sum[i]) } col_val = Array.new(6) { |i| eval_line(col_cnt[i], col_sum[i]) } cur_val = row_val.sum + col_val.sum @cnt_coef = INITIAL_CNT_COEF best_res = Result.new([] of Int32, 0) initial_cooler = IC final_cooler = FC cooler = initial_cooler turn = 0 begin_time = Time.utc.to_unix_ms total_time = timelimit - begin_time if total_time == 0 return best_res end while true if (turn & 0xFF) == 0 cur_time = Time.utc.to_unix_ms if cur_time > timelimit debug("turn: #{turn}") break end ratio = (cur_time - begin_time) / total_time cooler = initial_cooler + (final_cooler - initial_cooler) * ratio new_cnt_coef = (INITIAL_CNT_COEF * (1.0 - ratio)).round.to_i if new_cnt_coef != @cnt_coef @cnt_coef = new_cnt_coef 6.times do |i| row_val[i] = eval_line(row_cnt[i], row_sum[i]) col_val[i] = eval_line(col_cnt[i], col_sum[i]) end cur_val = row_val.sum + col_val.sum end end p0 = RND.rand(36) p1 = RND.rand(35) p1 += 1 if p0 <= p1 r0, c0 = p0.divmod(6) r1, c1 = p1.divmod(6) d0 = place[p0] d1 = place[p1] diff = 0 if r0 != r1 row_cnt[r0] -= @dices_exist[d0] row_sum[r0] -= @dices_sum[d0] row_cnt[r1] += @dices_exist[d0] row_sum[r1] += @dices_sum[d0] row_cnt[r1] -= @dices_exist[d1] row_sum[r1] -= @dices_sum[d1] row_cnt[r0] += @dices_exist[d1] row_sum[r0] += @dices_sum[d1] diff += eval_line(row_cnt[r0], row_sum[r0]) + eval_line(row_cnt[r1], row_sum[r1]) - row_val[r0] - row_val[r1] end if c0 != c1 col_cnt[c0] -= @dices_exist[d0] col_sum[c0] -= @dices_sum[d0] col_cnt[c1] += @dices_exist[d0] col_sum[c1] += @dices_sum[d0] col_cnt[c1] -= @dices_exist[d1] col_sum[c1] -= @dices_sum[d1] col_cnt[c0] += @dices_exist[d1] col_sum[c0] += @dices_sum[d1] diff += eval_line(col_cnt[c0], col_sum[c0]) + eval_line(col_cnt[c1], col_sum[c1]) - col_val[c0] - col_val[c1] end if accept(diff, cooler) cur_val += diff place.swap(p0, p1) row_val[r0] = eval_line(row_cnt[r0], row_sum[r0]) row_val[r1] = eval_line(row_cnt[r1], row_sum[r1]) col_val[c0] = eval_line(col_cnt[c0], col_sum[c0]) col_val[c1] = eval_line(col_cnt[c1], col_sum[c1]) # debug("score:#{cur_val} turn:#{turn}") if @cnt_coef == 0 && best_res.score < cur_val debug("best_score:#{cur_val} turn:#{turn}") best_res.score = cur_val best_res.pos = place.clone assert(cur_val == calc_score(place)) end else row_cnt[r0] += @dices_exist[d0] row_sum[r0] += @dices_sum[d0] row_cnt[r1] -= @dices_exist[d0] row_sum[r1] -= @dices_sum[d0] col_cnt[c0] += @dices_exist[d0] col_sum[c0] += @dices_sum[d0] col_cnt[c1] -= @dices_exist[d0] col_sum[c1] -= @dices_sum[d0] row_cnt[r1] += @dices_exist[d1] row_sum[r1] += @dices_sum[d1] row_cnt[r0] -= @dices_exist[d1] row_sum[r0] -= @dices_sum[d1] col_cnt[c1] += @dices_exist[d1] col_sum[c1] += @dices_sum[d1] col_cnt[c0] -= @dices_exist[d1] col_sum[c0] -= @dices_sum[d1] end turn += 1 end if best_res.score > 0 real_pos = Array.new(36, 0) 36.times do |i| real_pos[best_res.pos[i]] = i end best_res.pos = real_pos end return best_res end def calc_score(place) score = 0 6.times do |i| 6.times do |j| sum = 0 cnt = 0 6.times do |k| ds = @dices[place[j * 6 + k]] if ds[i] > 0 sum += ds[i] cnt += 1 end end if cnt == 6 score += 3 + (sum - 6) end sum = 0 cnt = 0 6.times do |k| ds = @dices[place[k * 6 + j]] if ds[i] > 0 sum += ds[i] cnt += 1 end end if cnt == 6 score += 3 + (sum - 6) end end end return score end def eval_line(cnt, sum) 6.times.sum do |i| c = (cnt >> (3 * i)) & 0x7 s = (sum >> (6 * i)) & 0x3F c * @cnt_coef + (c == 6 ? s - 3 : 0) end end def accept(diff, cooler) return true if diff >= 0 v = diff * cooler return false if v < -8 return RND.rand < Math.exp(v) end end def main dices = Array.new(36) do vs = read_line.split.map(&.to_i) Array.new(6) { |i| vs.count(i + 1) } end solver = Solver.new(dices) part = PART best_res = Result.new([] of Int32, 0) part.times do |i| res = solver.solve(START_TIME + TL * (i + 1) // part) if res.score > best_res.score best_res = res end end print best_res.to_s debug("final_score=#{best_res.score}") end main