-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDay8.java
88 lines (74 loc) · 2.43 KB
/
Day8.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
package aoc19;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import myutils19.StaticUtils;
public class Day8 {
private final int imageWidth = 25;
private final int imageHeight = 6;
private final int transparent = 2;
private final List<Integer> rawImageData;
public Day8(File input) {
rawImageData = StaticUtils.digitFileToList(input);
}
public int run1() {
List<int[][]> layers = getAllLayers();
int[][] targetLayer = null;
int zeroCount = Integer.MAX_VALUE;
for(int[][] layer : layers) {
int count = countDigits(layer, 0);
if(count < zeroCount) {
zeroCount = count;
targetLayer = layer;
}
}
int oneDigitCount = countDigits(targetLayer, 1);
int twoDigitCount = countDigits(targetLayer, 2);
return oneDigitCount * twoDigitCount;
}
// prints out the decoded layer, can be a bit difficult to read
// it's easier if you just draw it on a piece of paper after printing it on the console
public void run2() {
int[][] decodedLayer = new int[imageHeight][imageWidth];
List<int[][]> layers = getAllLayers();
for(int i = 0; i < imageHeight; i++) {
for(int j = 0; j < imageWidth; j++) {
decodedLayer[i][j] = getCorrectColor(layers, i, j);
}
}
for(int[] width : decodedLayer) {
System.out.println(Arrays.toString(width));
}
}
private int getCorrectColor(List<int[][]> layers, int i, int j) {
int targetLayer = 0;
while(layers.get(targetLayer)[i][j] == transparent) {
targetLayer++;
}
return layers.get(targetLayer)[i][j];
}
private int countDigits(int[][] layer, int digit) {
return (int) Arrays.stream(layer).flatMapToInt(x -> Arrays.stream(x)).filter(n -> n == digit).count();
}
private List<int[][]> getAllLayers() {
List<int[][]> layers = new ArrayList<>();
int rawDataLen = imageWidth * imageHeight;
for(int i = 0; i < rawImageData.size(); i += rawDataLen) {
List<Integer> layerList = rawImageData.subList(i, i + rawDataLen);
int index = 0;
int[][] layer = new int[imageHeight][imageWidth];
for(int j = 0; j < imageHeight; j++) {
for(int k = 0; k < imageWidth; k++) {
layer[j][k] = layerList.get(index++);
}
}
layers.add(layer);
}
return layers;
}
public static void main(String[] args) {
Day8 test = new Day8(new File("C:\\Users\\Timucin\\Desktop\\Advent of code 2019\\Day 8\\InputFile.txt"));
test.run2();
}
}