diff --git a/day-7/Main.java b/day-7/Main.java index 030b87a..dfa4dba 100644 --- a/day-7/Main.java +++ b/day-7/Main.java @@ -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 parseInput() { @@ -63,8 +85,15 @@ public class Main { public static void main(String args[]) { ArrayList 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); } }