|
| 1 | +package net.zomis.brainf.analyze.analyzers |
| 2 | + |
| 3 | +import net.zomis.brainf.analyze.Brainalyze |
| 4 | +import net.zomis.brainf.analyze.BrainfuckAnalyzer |
| 5 | +import net.zomis.brainf.analyze.InspectionResult |
| 6 | +import net.zomis.brainf.analyze.MemoryCell |
| 7 | +import net.zomis.brainf.model.BrainfuckCommand |
| 8 | +import net.zomis.brainf.model.BrainfuckRunner |
| 9 | +import net.zomis.brainf.model.classic.BrainFCommand |
| 10 | + |
| 11 | +class PlusMinusOptimizer implements BrainfuckAnalyzer { |
| 12 | + |
| 13 | + private static final Set<BrainfuckCommand> handlableCommands = [BrainFCommand.ADD, BrainFCommand.SUBTRACT, |
| 14 | + BrainFCommand.NEXT, BrainFCommand.PREVIOUS] as Set |
| 15 | + |
| 16 | + private final List<InspectionResult> results = [] |
| 17 | + |
| 18 | + private List<Integer> changesLeft = new ArrayList<>() |
| 19 | + private List<Integer> changesRight = new ArrayList<>() |
| 20 | + private int pointerChange |
| 21 | + private int commandStart |
| 22 | + private int commandsUsed |
| 23 | + |
| 24 | + @Override |
| 25 | + void after(Brainalyze analyze, BrainfuckRunner runner) { |
| 26 | + finishAndReset(runner) |
| 27 | + results.each { |
| 28 | + analyze.addInspectionResult(it) |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + void beforePerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand command) { |
| 34 | + if (!(command instanceof BrainFCommand)) { |
| 35 | + return |
| 36 | + } |
| 37 | + if (command == BrainFCommand.NONE) { |
| 38 | + return |
| 39 | + } |
| 40 | + if (!handlableCommands.contains(command)) { |
| 41 | + finishAndReset(runner) |
| 42 | + return |
| 43 | + } |
| 44 | + BrainFCommand cmd = (BrainFCommand) command |
| 45 | + switch (cmd) { |
| 46 | + case BrainFCommand.PREVIOUS: |
| 47 | + pointerMove(-1) |
| 48 | + break |
| 49 | + case BrainFCommand.NEXT: |
| 50 | + pointerMove(1) |
| 51 | + break |
| 52 | + case BrainFCommand.SUBTRACT: |
| 53 | + valueChange(-1) |
| 54 | + break |
| 55 | + case BrainFCommand.ADD: |
| 56 | + valueChange(1) |
| 57 | + break |
| 58 | + default: |
| 59 | + throw new IllegalStateException('Unexpected command: ' + command) |
| 60 | + } |
| 61 | + commandsUsed++ |
| 62 | + } |
| 63 | + |
| 64 | + void valueChange(int i) { |
| 65 | + int index = pointerChange |
| 66 | + List list |
| 67 | + if (pointerChange >= 0) { |
| 68 | + list = changesRight |
| 69 | + } else { |
| 70 | + index = Math.abs(pointerChange) - 1 |
| 71 | + list = changesLeft |
| 72 | + } |
| 73 | + while (list.size() <= index) { |
| 74 | + list.add(0) |
| 75 | + } |
| 76 | + list.set(index, list.get(index) + i) |
| 77 | + } |
| 78 | + |
| 79 | + void pointerMove(int i) { |
| 80 | + pointerChange += i |
| 81 | + } |
| 82 | + |
| 83 | + boolean finishAndReset(BrainfuckRunner runner) { |
| 84 | + trim(changesLeft) |
| 85 | + trim(changesRight) |
| 86 | + // find out if it's better to go to the `pointerChange` from left or right |
| 87 | + // >>>>> + <<<<< <<<<< <<<<< - > + for example is better to go right first, then left |
| 88 | + int leftCost = changesLeft.size() + pointerChange |
| 89 | + int rightCost = changesRight.size() - 1 - pointerChange |
| 90 | + boolean fromLeftToEnd = leftCost < rightCost |
| 91 | + |
| 92 | + StringBuilder str = new StringBuilder() |
| 93 | + if (fromLeftToEnd) { |
| 94 | + if (!changesRight.isEmpty()) { |
| 95 | + str.append(code(changesRight, '>' as char)) |
| 96 | + str.append('<' * (changesRight.size() - 1)) |
| 97 | + } |
| 98 | + if (!changesLeft.isEmpty()) { |
| 99 | + str.append('<') |
| 100 | + str.append(code(changesLeft, '<' as char)) |
| 101 | + } |
| 102 | + if (leftCost > 0) { |
| 103 | + str.append('>' * leftCost) |
| 104 | + } else { |
| 105 | + str.append('<' * -leftCost) |
| 106 | + } |
| 107 | + } else { |
| 108 | + if (!changesLeft.isEmpty()) { |
| 109 | + str.append('<') |
| 110 | + str.append(code(changesLeft, '<' as char)) |
| 111 | + str.append('>' * changesLeft.size()) |
| 112 | + } |
| 113 | + if (!changesRight.isEmpty()) { |
| 114 | + str.append(code(changesRight, '>' as char)) |
| 115 | + } |
| 116 | + if (rightCost > 0) { |
| 117 | + str.append('<' * rightCost) |
| 118 | + } else { |
| 119 | + str.append('>' * -rightCost) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + String shortestCode = str.toString() |
| 125 | + if (shortestCode.length() < this.commandsUsed) { |
| 126 | + this.results.add(new InspectionResult(InspectionResult.InspectionSeverity.HINT, |
| 127 | + commandStart, runner.code.commandIndex, |
| 128 | + "Code is unnecessarily complicated. Can be written as '$shortestCode'")) |
| 129 | + } |
| 130 | + |
| 131 | + changesLeft.clear() |
| 132 | + changesRight.clear() |
| 133 | + pointerChange = 0 |
| 134 | + commandStart = 0 |
| 135 | + commandsUsed = 0 |
| 136 | + } |
| 137 | + |
| 138 | + static String code(List<Integer> list, char ch) { |
| 139 | + StringBuilder str = new StringBuilder() |
| 140 | + def it = list.listIterator() |
| 141 | + while (it.hasNext()) { |
| 142 | + int value = it.next() |
| 143 | + if (value > 0) { |
| 144 | + str.append('+' * value) |
| 145 | + } |
| 146 | + if (value < 0) { |
| 147 | + value = Math.abs(value) |
| 148 | + str.append('-' * value) |
| 149 | + } |
| 150 | + if (it.hasNext()) { |
| 151 | + str.append(ch) |
| 152 | + } |
| 153 | + } |
| 154 | + str.toString() |
| 155 | + } |
| 156 | + |
| 157 | + static void trim(List<Integer> integers) { |
| 158 | + while (!integers.isEmpty() && integers.get(integers.size() - 1) == 0) { |
| 159 | + integers.remove(integers.size() - 1) |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | +} |
0 commit comments