-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC192019.java
91 lines (79 loc) · 2.72 KB
/
AoC192019.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
package com.adventofcode.aoc2019;
import static com.adventofcode.utils.Utils.getFirstString;
import static com.adventofcode.utils.Utils.itoa;
import static com.adventofcode.utils.Utils.matrixToLongStream;
import static com.adventofcode.utils.Utils.toLongList;
import com.adventofcode.Solution;
import com.adventofcode.utils.Computer2019;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.Stream;
class AoC192019 implements Solution {
private static final boolean PRINT = Boolean.parseBoolean( System.getProperty( "print" ) );
public String solveFirstPart( final Stream<String> input ) {
final BlockingQueue<Long> in = new LinkedBlockingQueue<>();
final BlockingDeque<Long> out = new LinkedBlockingDeque<>();
final List<Long> program = toLongList( getFirstString( input ) );
final int size = 50;
final long[][] matrix = new long[size][size];
for ( int y = 0; y < size; y++ ) {
for ( int x = 0; x < size; x++ ) {
matrix[y][x] = getNumber( program, in, out, x, y );
}
}
if ( PRINT ) {
printMatrix( matrix );
}
return itoa( matrixToLongStream( matrix ).filter( e -> e == 1L ).count() );
}
public String solveSecondPart( final Stream<String> input ) {
final BlockingQueue<Long> in = new LinkedBlockingQueue<>();
final BlockingDeque<Long> out = new LinkedBlockingDeque<>();
final List<Long> program = toLongList( getFirstString( input ) );
final int size = 100;
int y = size;
int x = 1;
while ( true ) {
//find the left-most part of the beam on this row
long number;
do {
number = getNumber( program, in, out, x, y );
if ( number == 0 ) {
/* move to the right only if number is 0. if 1, will retry the same x on the next row */
x++;
}
} while ( number == 0 );
//if top right corner of the square is inside the beam
if ( getNumber( program, in, out, x + ( size - 1 ), y - ( size - 1 ) ) == 1L ) {
//find the top left corner of the square
y -= ( size - 1 );
return itoa( x * 10000L + y );
}
//move to next row
y++;
}
}
private void printMatrix( final long[][] matrix ) {
for ( final var row : matrix ) {
for ( final var e : row ) {
System.out.print( e );
}
System.out.println();
}
}
private long getNumber( final List<Long> program, final BlockingQueue<Long> in,
final BlockingDeque<Long> out, final int x, final int y ) {
in.add( (long) x );
in.add( (long) y );
try {
Computer2019.runComputer( program, in, out, false );
return out.take();
} catch ( InterruptedException e ) {
e.printStackTrace();
}
throw new IllegalArgumentException();
}
}