1
+ import static com .github .pareronia .aoc .IntegerSequence .Range .range ;
2
+ import static java .util .stream .Collectors .toList ;
3
+
4
+ import java .util .ArrayDeque ;
5
+ import java .util .ArrayList ;
6
+ import java .util .Deque ;
7
+ import java .util .List ;
8
+
9
+ import com .github .pareronia .aoc .intcode .IntCode ;
10
+ import com .github .pareronia .aocd .Aocd ;
11
+ import com .github .pareronia .aocd .Puzzle ;
12
+
13
+ public class AoC2019_13 extends AoCBase {
14
+
15
+ private final List <Long > program ;
16
+
17
+ private AoC2019_13 (final List <String > input , final boolean debug ) {
18
+ super (debug );
19
+ assert input .size () == 1 ;
20
+ this .program = IntCode .parse (input .get (0 ));
21
+ }
22
+
23
+ public static AoC2019_13 create (final List <String > input ) {
24
+ return new AoC2019_13 (input , false );
25
+ }
26
+
27
+ public static AoC2019_13 createDebug (final List <String > input ) {
28
+ return new AoC2019_13 (input , true );
29
+ }
30
+
31
+ @ Override
32
+ public Long solvePart1 () {
33
+ final IntCode intCode = new IntCode (this .debug );
34
+ final Deque <Long > input = new ArrayDeque <>();
35
+ final Deque <Long > output = new ArrayDeque <>();
36
+ intCode .run (this .program , input , output );
37
+ final List <Long > list = output .stream ().collect (toList ());
38
+ return range (2 , list .size (), 3 ).intStream ()
39
+ .mapToLong (i -> list .get (i ))
40
+ .filter (o -> o == 2L )
41
+ .count ();
42
+ }
43
+
44
+ @ Override
45
+ public Long solvePart2 () {
46
+ final IntCode intCode = new IntCode (this .debug );
47
+ final Deque <Long > input = new ArrayDeque <>();
48
+ final Deque <Long > output = new ArrayDeque <>();
49
+ final List <Long > quarters = new ArrayList <>();
50
+ quarters .add (2L );
51
+ quarters .addAll (this .program .subList (1 , this .program .size ()));
52
+ final List <Long > buffer = new ArrayList <>();
53
+ Long ball = null , paddle = null ;
54
+ long score = 0 ;
55
+
56
+ intCode .runTillHasOutput (quarters , input , output );
57
+ while (!intCode .isHalted ()) {
58
+ buffer .add (output .pop ());
59
+ if (buffer .size () < 3 ) {
60
+ intCode .continueTillHasOutput (input , output );
61
+ continue ;
62
+ }
63
+
64
+ final long x = buffer .get (0 );
65
+ final long y = buffer .get (1 );
66
+ final long id = buffer .get (2 );
67
+ log (buffer );
68
+ buffer .clear ();
69
+ if (x == -1 && y == 0 ) {
70
+ score = id ;
71
+ } else if (id == 3 ) {
72
+ paddle = x ;
73
+ } else if (id == 4 ) {
74
+ ball = x ;
75
+ }
76
+ if (ball != null && paddle != null ) {
77
+ final long joystick = Long .compare (ball , paddle );
78
+ log (" -> " + joystick );
79
+ input .add (joystick );
80
+ ball = null ;
81
+ }
82
+
83
+ intCode .continueTillHasOutput (input , output );
84
+ }
85
+ return score ;
86
+ }
87
+
88
+ public static void main (final String [] args ) throws Exception {
89
+ final Puzzle puzzle = Aocd .puzzle (2019 , 13 );
90
+ final List <String > inputData = puzzle .getInputData ();
91
+ puzzle .check (
92
+ () -> lap ("Part 1" , AoC2019_13 .create (inputData )::solvePart1 ),
93
+ () -> lap ("Part 2" , AoC2019_13 .create (inputData )::solvePart2 )
94
+ );
95
+ }
96
+ }
0 commit comments