-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay13.java
199 lines (165 loc) · 5.75 KB
/
Day13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package net.eugenpaul.adventofcode.y2019.day13;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import net.eugenpaul.adventofcode.helper.MapOfSimplePos;
import net.eugenpaul.adventofcode.helper.SimplePos;
import net.eugenpaul.adventofcode.helper.SolutionTemplate;
import net.eugenpaul.adventofcode.helper.StringConverter;
import net.eugenpaul.adventofcode.helper.computer.IntcodeMapComputer;
public class Day13 extends SolutionTemplate {
@AllArgsConstructor
private enum Tile {
EMPTY(0L, ' '), WALL(1L, '#'), BLOCK(2L, '*'), PADDLE(3L, '-'), BALL(4L, 'o');
@Getter
private final long value;
@Getter
private final char drawChar;
private static final Map<Long, Tile> LONG_TO_TILE = Stream.of(Tile.values()).collect(Collectors.toMap(Tile::getValue, v -> v));
public static Tile fromLong(long value) {
return LONG_TO_TILE.get(value);
}
}
@Getter
private int blockCount;
@Getter
private int score;
@Setter
private boolean printDisplay = false;
public static void main(String[] args) {
Day13 puzzle = new Day13();
puzzle.doPuzzleFromFile("y2019/day13/puzzle1.txt");
}
@Override
public boolean doEvent(String eventData) {
long[] opcodesArray = StringConverter.toLongArray(eventData);
blockCount = doPuzzle1(opcodesArray);
score = doPuzzle2(opcodesArray);
logger.log(Level.INFO, () -> "blockCount : " + getBlockCount());
logger.log(Level.INFO, () -> "score : " + getScore());
return true;
}
private int doPuzzle1(long[] opcodesArray) {
Map<Long, Long> opcodes = initOpcodes(opcodesArray);
IntcodeMapComputer comp = new IntcodeMapComputer();
int pos = 0;
int outputCounter = 0;
int x = 0;
int y = 0;
Tile value = null;
Map<SimplePos, Tile> display = new HashMap<>();
while (!comp.isEnd(opcodes, pos)) {
pos = comp.runOpcodes(opcodes, pos);
if (comp.isOutput()) {
switch (outputCounter) {
case 0:
x = comp.removeOutput().intValue();
outputCounter++;
break;
case 1:
y = comp.removeOutput().intValue();
outputCounter++;
break;
case 2:
value = Tile.fromLong(comp.removeOutput());
display.put(//
new SimplePos(x, y), //
value //
);
outputCounter = 0;
break;
default:
break;
}
}
}
return (int) display.values().stream()//
.filter(v -> v == Tile.BLOCK)//
.count();
}
private int doPuzzle2(long[] opcodesArray) {
Map<Long, Long> opcodes = initOpcodes(opcodesArray);
opcodes.put(0L, 2L);
IntcodeMapComputer comp = new IntcodeMapComputer();
int pos = 0;
int outputCounter = 0;
int x = 0;
int y = 0;
Tile value = null;
Integer parrlePosX = null;
Integer ballPosX = null;
Map<SimplePos, Tile> display = new HashMap<>();
int currentScore = 0;
comp.setInput(0);
while (!comp.isEnd(opcodes, pos)) {
pos = comp.runOpcodes(opcodes, pos);
if (!comp.isOutput()) {
continue;
}
switch (outputCounter) {
case 0:
x = comp.removeOutput().intValue();
outputCounter++;
break;
case 1:
y = comp.removeOutput().intValue();
outputCounter++;
break;
case 2:
if (x == -1) {
currentScore = getScore(comp, display);
} else {
value = Tile.fromLong(comp.removeOutput());
display.put(//
new SimplePos(x, y), //
value //
);
if (value == Tile.BALL) {
ballPosX = x;
} else if (value == Tile.PADDLE) {
parrlePosX = x;
}
setInput(comp, parrlePosX, ballPosX);
}
outputCounter = 0;
break;
default:
break;
}
}
return currentScore;
}
private void setInput(IntcodeMapComputer comp, Integer parrlePosX, Integer ballPosX) {
if (ballPosX != null && parrlePosX != null) {
if (ballPosX > parrlePosX) {
comp.setInput(1L);
} else if (ballPosX < parrlePosX) {
comp.setInput(-1L);
} else {
comp.setInput(0L);
}
}
}
private int getScore(IntcodeMapComputer comp, Map<SimplePos, Tile> display) {
int currentScore;
currentScore = comp.removeOutput().intValue();
if (printDisplay) {
MapOfSimplePos.printList(MapOfSimplePos.mapToPrintList(display, v -> (v == null) ? ' ' : v.getDrawChar()));
}
return currentScore;
}
private Map<Long, Long> initOpcodes(long[] opcodesArray) {
Map<Long, Long> opcodes = new HashMap<>();
long counter = 0;
for (long l : opcodesArray) {
opcodes.put(counter, l);
counter++;
}
return opcodes;
}
}