fork download
  1. import java.io.BufferedReader;
  2. import java.io.StringReader;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Optional;
  9. import java.util.function.Consumer;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12. import java.util.stream.Collectors;
  13. import java.util.stream.IntStream;
  14.  
  15. class Leds{
  16.  
  17. public final static String INPUT
  18. = " ld a,14\n" +
  19. " out (0),a\n" +
  20. " ld a,12\n" +
  21. " out (0),a\n" +
  22. " ld a,8\n" +
  23. " out (0),a\n" +
  24. "\n" +
  25. " out (0),a\n" +
  26. " ld a,12\n" +
  27. " out (0),a\n" +
  28. " ld a,14\n" +
  29. " out (0),a\n" +
  30. " \n" +
  31. " ld b,3\n" +
  32. "\n" +
  33. "triple:\n" +
  34. " ld a,126\n" +
  35. " out (0),a\n" +
  36. " ld a,60\n" +
  37. " out (0),a\n" +
  38. " ld a,24\n" +
  39. " out (0),a\n" +
  40. " djnz triple\n" +
  41. " \n" +
  42. " ld a,1\n" +
  43. " ld b,9\n" +
  44. "\n" +
  45. "loop:\n" +
  46. " out (0),a\n" +
  47. " rlca\n" +
  48. " djnz loop\n" +
  49. " \n" +
  50. " ld a,2\n" +
  51. " ld b,9\n" +
  52. "\n" +
  53. "loop2:\n" +
  54. " out (0),a\n" +
  55. " rrca\n" +
  56. " djnz loop2";
  57.  
  58. public enum ACTION {
  59. LD(Pattern.compile("^\\s*ld ([a-z]),(\\d*)"),
  60. input -> REG.put(input.get(0), Byte.valueOf(input.get(1)))),
  61. OUT(Pattern.compile("^\\s*out \\(0\\),([a-z])"),
  62. input -> {
  63. LEDS = REG.get(input.get(0));
  64. outputLEDs();
  65. }),
  66. RLCA(Pattern.compile("^\\s*rlca"),
  67. input -> REG.put("a", toByte(Integer.rotateLeft(REG.get("a"), 1)))),
  68. RRCA(Pattern.compile("^\\s*rrca"),
  69. input -> {
  70. Byte a = (byte) (REG.get("a"));
  71. byte rolled = (byte) ((((a & 0xff) >> 1) + ((a & 0x01) == 1 ? 1 << 7 : 0)));
  72. REG.put("a", rolled);
  73. }),
  74. LABEL(Pattern.compile("^\\s*(.*):"),
  75. input -> { }),
  76. DJNZ(Pattern.compile("^\\s*djnz (.*)"),
  77. input -> {
  78. Byte b = REG.get("b");
  79. REG.put("b", (byte) (b - 1));
  80. if (b > 1) {
  81. IntStream.range(0, PROGRAM.size())
  82. .filter(index -> (PROGRAM.get(index).action == LABEL && PROGRAM.get(index).params.get(0).equals(input.get(0))))
  83. .findFirst()
  84. .ifPresent(index -> PC = index);
  85. }
  86. });
  87.  
  88. public Pattern pattern;
  89. public Consumer<List<String>> consumer;
  90.  
  91. private ACTION(Pattern pattern, Consumer<List<String>> consumer) {
  92. this.pattern = pattern;
  93. this.consumer = consumer;
  94. }
  95. }
  96.  
  97. public static byte toByte(int num) {
  98. int tmp = num & 0xff;
  99. return (byte) ((tmp & 0x80) == 0 ? tmp : tmp - 256);
  100. }
  101.  
  102. public static final HashMap<String, Byte> REG = new HashMap<>();
  103. public static Byte LEDS = 0x00;
  104. public static final List<ParsedAction> PROGRAM = new LinkedList<>();
  105. public static int PC = 0;
  106.  
  107. public static Optional<ParsedAction> parseAction(String line) {
  108. return Arrays.stream(ACTION.values())
  109. .filter(action -> action.pattern.matcher(line).matches())
  110. .map(action -> {
  111. Matcher m = action.pattern.matcher(line);
  112. List<String> params = new ArrayList<>();
  113. m.find();
  114.  
  115. return new ParsedAction(action, IntStream.range(1, m.groupCount()+1)
  116. .mapToObj(index -> m.group(index))
  117. .collect(Collectors.toList())
  118. );
  119. })
  120. .findFirst();
  121. }
  122.  
  123. public static void outputLEDs() {
  124. for (int i = 7; i >= 0; i--) {
  125. System.out.print((LEDS >> i & 0x01) == 1 ? "*" : ".");
  126. }
  127. System.out.println();
  128. }
  129.  
  130. public static void main(String[] args) {
  131.  
  132. new BufferedReader(new StringReader(INPUT)).lines()
  133. .sequential()
  134. .map(Leds::parseAction)
  135. .filter(Optional::isPresent)
  136. .map(Optional::get)
  137. .forEachOrdered(action -> PROGRAM.add(action));
  138.  
  139. for (PC = 0; PC < PROGRAM.size(); PC++) {
  140. ParsedAction action = PROGRAM.get(PC);
  141. action.perform();
  142. }
  143. }
  144.  
  145. public static class ParsedAction {
  146.  
  147. public ACTION action;
  148. public List<String> params;
  149.  
  150. public ParsedAction(ACTION action, List<String> params) {
  151. this.action = action;
  152. this.params = params;
  153. }
  154.  
  155. public void perform() {
  156. action.consumer.accept(params);
  157. }
  158. }
  159. }
  160.  
Success #stdin #stdout 0.11s 711680KB
stdin
Standard input is empty
stdout
....***.
....**..
....*...
....*...
....**..
....***.
.******.
..****..
...**...
.******.
..****..
...**...
.******.
..****..
...**...
.......*
......*.
.....*..
....*...
...*....
..*.....
.*......
*.......
.......*
......*.
.......*
*.......
.*......
..*.....
...*....
....*...
.....*..
......*.