From 7a99f86e0175e630b251d206ba0e50e8ef71b436 Mon Sep 17 00:00:00 2001
From: kirbylife <kirbylife@protonmail.com>
Date: Sat, 14 Dec 2024 00:49:27 -0600
Subject: [PATCH] Day 7 part 2

---
 day-7/Main.java | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

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<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);
     }
 }