-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay13.java
106 lines (92 loc) · 2.89 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
package aoc19;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import myutils19.Point2d;
import myutils19.IntCodeComputer;
import myutils19.StaticUtils;
import myutils19.TileType;
public class Day13 {
private List<Long> initialProgram;
public Day13(File input) {
initialProgram = StaticUtils.commaSeperatedLongFileToList(input);
}
public int run1() {
Map<Point2d, TileType> tileMappings = new HashMap<>();
IntCodeComputer computer = new IntCodeComputer(new ArrayList<>(initialProgram));
computer.run();
Queue<Long> outputVals = computer.outputValues();
while (!outputVals.isEmpty()) {
int x = outputVals.poll().intValue();
int y = outputVals.poll().intValue();
int tileId = outputVals.poll().intValue();
tileMappings.put(new Point2d(x, y), TileType.of(tileId));
}
int blockTileCount = (int) tileMappings.entrySet().stream().map(t -> t.getValue())
.filter(tile -> tile == TileType.BLOCK).count();
return blockTileCount;
}
public int run2() {
int score = 0;
IntCodeComputer computer = new IntCodeComputer(new ArrayList<>(initialProgram));
computer.replace(0, 2);
Point2d padPosition = null;
Point2d ballPosition = null;
//initialize the full board, get the ball and pad positions
computer.run();
Queue<Long> initialOutput = computer.outputValues();
while(!initialOutput.isEmpty()) {
int x = initialOutput.poll().intValue();
int y = initialOutput.poll().intValue();
int id = initialOutput.poll().intValue();
TileType tp = TileType.of(id);
if(tp == TileType.BALL) {
ballPosition = new Point2d(x, y);
}
else if(tp == TileType.PADDLE) {
padPosition = new Point2d(x, y);
}
}
// computer waits for input at this point
int joystickVal = 0;
if(ballPosition.x() > padPosition.x()) {
joystickVal = 1;
}
else if(ballPosition.x() < padPosition.x()) {
joystickVal = -1;
}
computer.setInputValues(joystickVal);
int joystickPosition = 0;
while (!computer.isShutdown()) {
computer.run();
// flush buffer
Queue<Long> outputValues = computer.outputValues();
while (!outputValues.isEmpty()) {
int x = outputValues.poll().intValue();
int y = outputValues.poll().intValue();
int id = outputValues.poll().intValue();
if (x == -1 && y == 0) {
score = id;
} else {
TileType tp = TileType.of(id);
if(tp == TileType.PADDLE) {
padPosition = new Point2d(x, y);
}
else if(tp == TileType.BALL) {
joystickPosition = x > padPosition.x() ? 1 : (x < padPosition.x() ? -1 : 0);
}
}
}
if(computer.isStandby())
computer.setInputValues(joystickPosition);
}
return score;
}
public static void main(String[] args) {
Day13 test = new Day13(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2019\\Day 13\\InputFile.txt"));
System.out.println(test.run2());
}
}