-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay3.java
151 lines (131 loc) · 3.66 KB
/
Day3.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.kasteelharry.aoc;
import java.util.ArrayList;
import java.util.List;
public class Day3 extends Day {
private static final String FILENAME = "\\input\\day3.txt";
private static final String FILENAME_TEST = "\\input\\day3Test.txt";
private int lsb;
private int msb;
private char[] arrayLSB;
private char[] arrayMSB;
private int[] equals;
private final int length;
private List<String> input;
public Day3(int length, boolean partTwo) {
super(FILENAME, partTwo);
this.length = length;
setValues();
}
public Day3(int length, int exp, boolean partTwo) {
super(FILENAME_TEST, partTwo, exp);
this.length = length;
setValues();
}
public void findMSB(List<String> list) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < this.length; i++) {
int one = 0;
int zero = 0;
for (String s : list) {
if (s.charAt(i) == '0') {
zero++;
} else {
one++;
}
}
if (one == zero) {
equals[i] = 2;
}
if (one > zero) {
result.append("1");
arrayMSB[i] = '1';
arrayLSB[i] = '0';
} else {
result.append("0");
arrayMSB[i] = '0';
arrayLSB[i] = '1';
}
}
msb = Integer.parseInt(result.toString(), 2);
}
public char getMSBChar(int pos) {
if (equals[pos] == 0) {
return arrayMSB[pos];
} else {
return '1';
}
}
public char getLSBChar(int pos) {
if (equals[pos] == 0) {
return arrayLSB[pos];
} else {
return '0';
}
}
public int filterOxygen(List<String> list, int pos) {
if (list.size() == 1) {
return Integer.parseInt(list.get(0), 2);
}
List<String> copy = new ArrayList<>();
for (String s : list) {
if (s.charAt(pos) == getMSBChar(pos)) {
copy.add(s);
}
}
pos++;
findMSB(copy);
return filterOxygen(copy, pos);
}
public int filterCO2(List<String> list, int pos) {
if (list.size() == 1) {
return Integer.parseInt(list.get(0), 2);
}
List<String> copy = new ArrayList<>();
for (String s : list) {
if (s.charAt(pos) == getLSBChar(pos)) {
copy.add(s);
}
}
pos++;
findMSB(copy);
findLSB();
return filterCO2(copy, pos);
}
public void findLSB() {
int mask = (1 << length) - 1;
lsb = ~msb & mask;
}
public void start() {
startUp();
System.out.println(msb * lsb);
}
public void startPart2() {
startUp();
int ox = filterOxygen(input, 0);
int co = filterCO2(input, 0);
System.out.println(ox * co);
}
private void startUp() {
setValues();
this.input = getLines();
findMSB(input);
findLSB();
}
private void setValues() {
this.arrayMSB = new char[length];
this.arrayLSB = new char[length];
this.msb = 0;
this.lsb = 0;
this.equals = new int[length];
}
public static void main(String[] args) {
Day3 dayTest = new Day3(5, 198, false);
dayTest.run();
Day3 day3Test = new Day3(5, 230, true);
day3Test.run();
Day3 day3 = new Day3(12, false);
day3.run();
day3 = new Day3(12, true);
day3.run();
}
}