1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Dartで第3回オフラインリアルタイムどう書く

Posted at

第3回オフラインリアルタイムどう書くの問題をDartで解いてみました。

import "dart:collection";

main() {
  test("b", "AB");
  test("l", "AD");
  test("r", "AC");
  test("bbb", "ABAB");
  test("rrr", "ACBA");
  test("blll", "ABCAB");
  test("llll", "ADEBA");
  test("rbrl", "ACADE");
  test("brrrr", "ABEDAB");
  test("llrrr", "ADEFDE");
  test("lrlll", "ADFEDF");
  test("lrrrr", "ADFCAD");
  test("rllll", "ACFDAC");
  test("blrrrr", "ABCFEBC");
  test("brllll", "ABEFCBE");
  test("bbrllrrr", "ABACFDEFD");
  test("rrrrblll", "ACBACABCA");
  test("llrlrrbrb", "ADEFCADABA");
  test("rrrbrllrr", "ACBABEFCAD");
  test("llrllblrll", "ADEFCBCADEB");
  test("lrrlllrbrl", "ADFCBEFDFCB");
  test("lllrbrrlbrl", "ADEBCBACFCAB");
  test("rrrrrrlrbrl", "ACBACBADFDEB");
  test("lbrbbrbrbbrr", "ADABABEBCBCFE");
  test("rrrrlbrblllr", "ACBACFCACFDAB");
  test("lbbrblrlrlbll", "ADADFDABCFDFED");
  test("rrbbrlrlrblrl", "ACBCBADFEBEFDA");
  test("blrllblbrrrrll", "ABCFDADEDABEDFE");
  test("blrllrlbllrrbr", "ABCFDABCBEFDEDA");
  test("lbrbbrllllrblrr", "ADABABEFCBEDEBCF");
  test("rrrrbllrlrbrbrr", "ACBACABCFDEDADFC");
  print("ok");
}

test(String input, String expect) {
  var q = new ListQueue.from(input.split(""));
  assert(walk(q, "B", "A") == expect);
}

final graph = {
  "A": "BCD",
  "B": "CAE",
  "C": "ABF",
  "D": "EAF",
  "E": "FBD",
  "F": "CED",
};

String walk(Queue input, String from, String to) {
  if(input.isEmpty) return to;

  String direction = input.removeFirst();
  if(direction == "b") return to.concat(walk(input, to, from));

  String nexts = graph[to].concat(graph[to]);
  int i = nexts.indexOf(from);
  String lr = nexts.slice(i + 1, i + 3);
  return to.concat(walk(input, to, lr[direction == "l" ? 1 : 0]));
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?