Day 7 part 2
parent
886e57882e
commit
7a99f86e01
|
@ -18,12 +18,11 @@ public class Main {
|
|||
this.values = v;
|
||||
}
|
||||
|
||||
boolean testIt() {
|
||||
boolean part1() {
|
||||
int possibleCombinations = (int) Math.pow(2, this.values.size());
|
||||
|
||||
for (int i = 0; i < possibleCombinations; i++) {
|
||||
long newResult = this.values.get(0);
|
||||
String out = "" + newResult;
|
||||
for (int j = 1; j < this.values.size(); j++) {
|
||||
if ((i >> j) % 2 == 0) {
|
||||
newResult += this.values.get(j);
|
||||
|
@ -37,6 +36,29 @@ public class Main {
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
boolean part2() {
|
||||
int possibleCombinations = (int) Math.pow(3, this.values.size());
|
||||
|
||||
for (int i = 0; i < possibleCombinations; i++) {
|
||||
long newResult = this.values.get(0);
|
||||
for (int j = 1; j < this.values.size(); j++) {
|
||||
long value = this.values.get(j);
|
||||
long trin = ((long) (i / Math.pow(3, j)) % 3);
|
||||
if (trin == 0) {
|
||||
newResult += value;
|
||||
} else if (trin == 1) {
|
||||
newResult *= value;
|
||||
} else {
|
||||
newResult = Long.valueOf("" + newResult + value);
|
||||
}
|
||||
}
|
||||
if (newResult == this.result) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static ArrayList<Test> parseInput() {
|
||||
|
@ -63,8 +85,15 @@ public class Main {
|
|||
|
||||
public static void main(String args[]) {
|
||||
ArrayList<Test> tests = parseInput();
|
||||
tests.removeIf(t -> !t.testIt());
|
||||
tests.removeIf(t -> !t.part1());
|
||||
long total = tests.stream().mapToLong(t -> t.result).sum();
|
||||
System.out.println("Part 1");
|
||||
System.out.println(total);
|
||||
|
||||
tests = parseInput();
|
||||
tests.removeIf(t -> !t.part2());
|
||||
total = tests.stream().mapToLong(t -> t.result).sum();
|
||||
System.out.println("Part 2");
|
||||
System.out.println(total);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue