fork download
  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.function.*;
  4. import java.util.regex.*;
  5. import java.util.stream.*;
  6.  
  7. class Leds{
  8.  
  9. public enum ACTION {
  10. LD(Pattern.compile("^\\s*ld ([a-z]),(\\d*)"),
  11. input -> REG.put(input.get(0), Byte.valueOf(input.get(1)))),
  12. OUT(Pattern.compile("^\\s*out \\(0\\),([a-z])"),
  13. input -> {
  14. LEDS = REG.get(input.get(0));
  15. outputLEDs();
  16. }),
  17. RLCA(Pattern.compile("^\\s*rlca"),
  18. input -> REG.put("a", toByte(Integer.rotateLeft(REG.get("a"), 1)))),
  19. RRCA(Pattern.compile("^\\s*rrca"),
  20. input -> {
  21. Byte a = (byte) (REG.get("a"));
  22. byte rolled = (byte) ((((a & 0xff) >> 1) + ((a & 0x01) == 1 ? 1 << 7 : 0)));
  23. REG.put("a", rolled);
  24. }),
  25. LABEL(Pattern.compile("^\\s*(.*):"),
  26. input -> { }),
  27. DJNZ(Pattern.compile("^\\s*djnz (.*)"),
  28. input -> {
  29. Byte b = REG.get("b");
  30. REG.put("b", (byte) (b - 1));
  31. if (b > 1) {
  32. IntStream.range(0, PROGRAM.size())
  33. .filter(index -> (PROGRAM.get(index).action == LABEL && PROGRAM.get(index).params.get(0).equals(input.get(0))))
  34. .findFirst()
  35. .ifPresent(index -> PC = index);
  36. }
  37. });
  38.  
  39. public Pattern pattern;
  40. public Consumer<List<String>> consumer;
  41.  
  42. private ACTION(Pattern pattern, Consumer<List<String>> consumer) {
  43. this.pattern = pattern;
  44. this.consumer = consumer;
  45. }
  46. }
  47.  
  48. public static byte toByte(int num) {
  49. int tmp = num & 0xff;
  50. return (byte) ((tmp & 0x80) == 0 ? tmp : tmp - 256);
  51. }
  52.  
  53. public static final HashMap<String, Byte> REG = new HashMap<>();
  54. public static Byte LEDS = 0x00;
  55. public static final List<ParsedAction> PROGRAM = new LinkedList<>();
  56. public static int PC = 0;
  57.  
  58. public static Optional<ParsedAction> parseAction(String line) {
  59. return Arrays.stream(ACTION.values())
  60. .filter(action -> action.pattern.matcher(line).matches())
  61. .map(action -> {
  62. Matcher m = action.pattern.matcher(line);
  63. List<String> params = new ArrayList<>();
  64. m.find();
  65.  
  66. return new ParsedAction(action, IntStream.range(1, m.groupCount()+1)
  67. .mapToObj(index -> m.group(index))
  68. .collect(Collectors.toList())
  69. );
  70. })
  71. .findFirst();
  72. }
  73.  
  74. public static void outputLEDs() {
  75. for (int i = 7; i >= 0; i--) {
  76. System.out.print((LEDS >> i & 0x01) == 1 ? "*" : ".");
  77. }
  78. System.out.println();
  79. }
  80.  
  81. public static void main(String[] args) {
  82.  
  83. .sequential()
  84. .map(Leds::parseAction)
  85. .filter(Optional::isPresent)
  86. .map(Optional::get)
  87. .forEachOrdered(action -> PROGRAM.add(action));
  88.  
  89. for (PC = 0; PC < PROGRAM.size(); PC++) {
  90. ParsedAction action = PROGRAM.get(PC);
  91. action.perform();
  92. }
  93. }
  94.  
  95. public static class ParsedAction {
  96.  
  97. public ACTION action;
  98. public List<String> params;
  99.  
  100. public ParsedAction(ACTION action, List<String> params) {
  101. this.action = action;
  102. this.params = params;
  103. }
  104.  
  105. public void perform() {
  106. action.consumer.accept(params);
  107. }
  108. }
  109. }
Success #stdin #stdout 0.11s 712192KB
stdin
  ld a,14
  out (0),a
  ld a,12
  out (0),a
  ld a,8
  out (0),a

  out (0),a
  ld a,12
  out (0),a
  ld a,14
  out (0),a
  
    ld b,3

triple:
  ld a,126
  out (0),a
  ld a,60
  out (0),a
  ld a,24
  out (0),a
  djnz triple
  
  ld a,1
  ld b,9

loop:
  out (0),a
  rlca
  djnz loop
  
  ld a,2
  ld b,9

loop2:
  out (0),a
  rrca
  djnz loop2
stdout
....***.
....**..
....*...
....*...
....**..
....***.
.******.
..****..
...**...
.******.
..****..
...**...
.******.
..****..
...**...
.......*
......*.
.....*..
....*...
...*....
..*.....
.*......
*.......
.......*
......*.
.......*
*.......
.*......
..*.....
...*....
....*...
.....*..
......*.