41361f62dbe2 — Quintillus 2 years ago
Day 2 Gold
M Java/src/main/java/com/ajtjp/advent2021/SubPiloting.java +23 -0
@@ 28,4 28,27 @@ public class SubPiloting {
         }
         return forward * depth;
     }
+    
+    public static int Gold(Scanner scanner) {
+        int forward = 0;
+        int depth = 0;
+        int aim = 0;
+        while (scanner.hasNextLine()) {
+            String nextLine = scanner.nextLine();
+            String[] parts = nextLine.split(" ");
+            String command = parts[0];
+            Integer amount = Integer.parseInt(parts[1]);
+            if (command.equals("forward")) {
+                forward += amount;
+                depth += amount * aim;
+            }
+            else if (command.equals("up")) {
+                aim -= amount;
+            }
+            else if (command.equals("down")) {
+                aim += amount;
+            }
+        }
+        return forward * depth;
+    }
 }

          
M Java/src/test/java/com/ajtjp/advent2021/SubPilotingTest.java +21 -5
@@ 4,24 4,26 @@ package com.ajtjp.advent2021;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.util.Scanner;
+import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.assertEquals;
-import org.junit.jupiter.api.Test;
 
 /**
  *
  * @author Andrew
  */
 public class SubPilotingTest {
-    @Test
-    public void testSampleInput_Silver() throws Exception {
-        String basicData = 
+    
+    final String sampleData = 
 "forward 5\n" +
 "down 5\n" +
 "forward 8\n" +
 "up 3\n" +
 "down 8\n" +
 "forward 2";
-        Scanner scanner = new Scanner(basicData);
+    
+    @Test
+    public void testSampleInput_Silver() throws Exception {
+        Scanner scanner = new Scanner(sampleData);
         int result = SubPiloting.Silver(scanner);
         assertEquals(150, result);
     }

          
@@ 32,4 34,18 @@ public class SubPilotingTest {
         int answer = SubPiloting.Silver(scanner);
         assertEquals(1947824, answer);
     }
+    
+    @Test
+    public void testGoldSample() throws Exception {
+        Scanner scanner = new Scanner(sampleData);
+        int result = SubPiloting.Gold(scanner);
+        assertEquals(900, result);
+    }
+    
+    @Test
+    public void testGold() throws Exception {
+        Scanner scanner = new Scanner(new File("data/day2.txt"));
+        int result = SubPiloting.Gold(scanner);
+        assertEquals(900, result);
+    }
 }