This repository was archived by the owner on Aug 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay8.java
More file actions
99 lines (90 loc) · 2.89 KB
/
Copy pathDay8.java
File metadata and controls
99 lines (90 loc) · 2.89 KB
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
package aoc2019;
import common.Util;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
public class Day8
{
private static List<int[][]> parseInput(String input, int width, int height )
{
List<int[][]> layers = new ArrayList<>();
char[] arr = input.toCharArray();
int index=0;
while( index < input.length() - 1 )
{
int[][] layer = new int[height][width];
for( int i=0; i<height; i++ )
{
for( int j=0; j<width; j++ )
layer[i][j] = Character.digit( arr[index++], 10);
}
layers.add( layer );
}
return layers;
}
private static int getCountOfDigit( int[][] layer, int digit, int width, int height )
{
int count = 0;
for( int i=0; i<height; i++ )
{
for( int j=0; j<width; j++ )
{
if( layer[i][j] == digit )
count++;
}
}
return count;
}
private static int getIndexOfLayerContainingFewestDigit( List<int[][]> layers, int digit, int width, int height )
{
int leastCount = Integer.MAX_VALUE;
int indexOfLayerHavingLeastCount = 0;
for( int i=0; i<layers.size(); i++ )
{
int[][] layer = layers.get( i );
int count = getCountOfDigit( layer, digit, width, height );
if( count < leastCount )
{
leastCount = count;
indexOfLayerHavingLeastCount = i;
}
}
return indexOfLayerHavingLeastCount;
}
private static int part1( List<int[][]> layers, int width, int height )
{
int[][] layer = layers.get( getIndexOfLayerContainingFewestDigit( layers, 0, width, height ) );
return getCountOfDigit( layer, 1, width, height ) * getCountOfDigit( layer, 2, width, height );
}
private static int findFirstValidPixel( List<int[][]> layers, int i, int j )
{
for( int[][] layer : layers )
{
if( layer[i][j] == 2 )
continue;
return layer[i][j];
}
return -1;
}
private static void printImage( List<int[][]> layers, int width, int height )
{
for( int i=0; i<height; i++ )
{
for( int j=0; j<width; j++ )
{
System.out.print( findFirstValidPixel( layers, i, j) == 1 ? "#" : " ");
}
System.out.println();
}
}
public static void main(String[] args) throws IOException
{
String input = new String( Files.readAllBytes(Util.getInputFilePath()));
int width = 25;
int height = 6;
List<int[][]> layers = parseInput( input, width, height );
System.out.println( part1( layers, width, height));
printImage( layers, width, height );
}
}