Skip to content

Instantly share code, notes, and snippets.

@torazuka
Last active December 19, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torazuka/5939368 to your computer and use it in GitHub Desktop.
Save torazuka/5939368 to your computer and use it in GitHub Desktop.
第12回オフラインリアルタイムどう書く(Java解答)
import java.util.ArrayList;
import java.util.List;
enum Direction {
E {
@Override
Dice go(Dice d) {
return d.goEast();
}
},
W {
@Override
Dice go(Dice d) {
return d.goWest();
}
},
S {
@Override
Dice go(Dice d) {
return d.goSouth();
}
},
N {
@Override
Dice go(Dice d) {
return d.goNorth();
}
};
abstract Dice go(Dice d);
}
class Dice {
int top;
int west;
int south;
public Dice(int t, int w, int s) {
top = t;
west = w;
south = s;
}
public Dice goEast() {
return new Dice(west, 7 - top, south);
}
public Dice goWest() {
return new Dice(7 - west, top, south);
}
public Dice goSouth() {
return new Dice(7 - south, west, top);
}
public Dice goNorth() {
return new Dice(south, west, 7 - top);
}
@Override
public String toString() {
return String.valueOf(top);
}
}
public class Rotdice {
public String solve(String input) {
List<Direction> directions = parse(input);
Dice dice = new Dice(1, 3, 5);
StringBuilder sb = new StringBuilder(dice.toString());
for (Direction each : directions) {
dice = each.go(dice);
sb.append(dice.toString());
}
return new String(sb);
}
List<Direction> parse(String input) {
List<Direction> result = new ArrayList<>();
for (char each : input.toCharArray()) {
result.add(Direction.valueOf(String.valueOf(each)));
}
return result;
}
}
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class RotdiceTest {
Rotdice target;
@Test
public void testParse() throws Exception {
target = new Rotdice();
List<Direction> expected = new ArrayList<>();
expected.add(Direction.E);
expected.add(Direction.W);
expected.add(Direction.S);
expected.add(Direction.N);
assertEquals(expected, target.parse("EWSN"));
}
void test(String input, String expected) {
target = new Rotdice();
if (expected.equals(target.solve(input))) {
System.out.println(input + "== OK");
} else {
System.out.println(input + "== NG");
}
}
@Test
public void testSolve() throws Exception {
/* 0 */test("NNESWWS", "15635624");
/* 1 */test("EEEE", "13641");
/* 2 */test("WWWW", "14631");
/* 3 */test("SSSS", "12651");
/* 4 */test("NNNN", "15621");
/* 5 */test("EENN", "13651");
/* 6 */test("WWNN", "14651");
/* 7 */test("SSNN", "12621");
/* 8 */test("NENNN", "153641");
/* 9 */test("NWNNN", "154631");
/* 10 */test("SWWWSNEEEN", "12453635421");
/* 11 */test("SENWSWSNSWE", "123123656545");
/* 12 */test("SSSWNNNE", "126546315");
/* 13 */test("SWNWSSSWWE", "12415423646");
/* 14 */test("ENNWWS", "1354135");
/* 15 */test("ESWNNW", "1321365");
/* 16 */test("NWSSE", "154135");
/* 17 */test("SWNWEWSEEN", "12415154135");
/* 18 */test("EWNWEEEEWN", "13154532426");
/* 19 */test("WNEWEWWWSNW", "145151562421");
/* 20 */test("NNEE", "15631");
/* 21 */test("EEEEWNWSW", "1364145642");
/* 22 */test("SENNWWES", "123142321");
/* 23 */test("SWWWSNSNESWW", "1245363635631");
/* 24 */test("WESSENSE", "141263231");
/* 25 */test("SWNSSESESSS", "124146231562");
/* 26 */test("ENS", "1353");
/* 27 */test("WNN", "1453");
/* 28 */test("SSEENEEEN", "1263124536");
/* 29 */test("NWSNNNW", "15414632");
/* 30 */test("ESSSSSWW", "132453215");
/* 31 */test("ESE", "1326");
/* 32 */test("SNWNWWNSSSS", "121456232453");
/* 33 */test("SWEESEN", "12423653");
/* 34 */test("NEEWNSSWWW", "15323631562");
/* 35 */test("WSEW", "14212");
/* 36 */test("SWSNNNSNWE", "12464131353");
/* 37 */test("ENWEWSEEW", "1351513545");
/* 38 */test("WSEWN", "142124");
/* 39 */test("EWNEESEWE", "1315321414");
/* 40 */test("NESEEN", "1531263");
/* 41 */test("WSW", "1426");
/* 42 */test("ENEWE", "135656");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment