9d365c3adb4d — Quintillus 2 years ago
Day 2 Silver.

Dunno why I'm doing this year's puzzle in Java.  Kotlin in 2020 was kinda nice, though I didn't wind up writing any Kotlin in 2021.  I've been writing mostly C# lately and that's also nice.

But this works.
A => Java/data/day2.txt +1000 -0
@@ 0,0 1,1000 @@ 
+forward 1
+down 5
+down 6
+down 2
+forward 8
+up 3
+up 2
+down 2
+forward 9
+forward 7
+down 2
+forward 5
+down 2
+forward 2
+forward 1
+down 8
+forward 5
+down 6
+up 9
+down 7
+forward 6
+forward 7
+forward 8
+up 3
+down 8
+forward 9
+forward 5
+forward 2
+down 3
+down 3
+down 6
+down 6
+down 8
+down 4
+forward 1
+up 9
+down 7
+up 6
+down 2
+down 8
+forward 1
+forward 7
+forward 4
+forward 1
+forward 7
+down 1
+down 9
+forward 6
+forward 6
+forward 7
+forward 3
+up 7
+forward 7
+up 1
+forward 3
+forward 4
+up 4
+forward 6
+down 8
+down 7
+forward 4
+up 6
+up 8
+down 5
+down 6
+forward 2
+forward 7
+forward 5
+forward 4
+down 5
+down 8
+down 4
+down 4
+down 3
+forward 3
+down 8
+forward 7
+up 6
+down 3
+down 7
+down 3
+forward 2
+up 8
+down 7
+down 6
+down 3
+up 5
+up 4
+down 2
+down 5
+up 9
+down 8
+up 7
+up 8
+forward 7
+down 9
+up 5
+down 5
+down 5
+up 4
+down 4
+up 3
+down 1
+down 4
+down 7
+forward 2
+forward 9
+up 8
+down 8
+forward 6
+forward 8
+down 6
+down 2
+forward 2
+forward 7
+up 7
+down 4
+forward 1
+down 4
+forward 2
+up 3
+forward 8
+forward 7
+forward 8
+down 2
+down 9
+down 5
+forward 7
+down 7
+forward 8
+forward 2
+up 2
+forward 9
+down 3
+up 7
+forward 2
+down 4
+up 3
+up 8
+forward 4
+forward 4
+forward 3
+up 3
+forward 5
+forward 5
+down 9
+down 4
+up 2
+down 8
+down 5
+down 8
+up 7
+down 3
+down 7
+up 9
+down 7
+up 5
+down 5
+forward 2
+forward 2
+forward 6
+down 4
+up 3
+forward 1
+forward 3
+forward 4
+down 2
+forward 2
+down 1
+down 2
+down 3
+forward 8
+forward 1
+down 5
+forward 3
+up 2
+down 2
+forward 3
+forward 1
+up 4
+up 1
+up 2
+forward 4
+forward 8
+down 7
+forward 2
+down 8
+forward 6
+forward 3
+up 7
+down 3
+down 3
+forward 7
+forward 9
+down 8
+down 8
+up 1
+up 8
+down 5
+up 3
+forward 6
+forward 4
+forward 8
+up 4
+down 6
+forward 2
+forward 8
+down 1
+up 5
+down 4
+forward 5
+up 1
+forward 2
+down 9
+down 2
+forward 8
+up 3
+up 7
+forward 3
+forward 4
+down 7
+up 3
+down 7
+forward 7
+up 1
+down 3
+down 5
+down 7
+forward 5
+down 1
+up 9
+forward 1
+down 6
+up 4
+down 1
+forward 7
+forward 2
+down 2
+forward 2
+down 7
+down 4
+down 7
+down 9
+down 6
+down 2
+up 6
+forward 8
+down 2
+down 1
+down 8
+down 8
+down 1
+down 7
+forward 4
+forward 1
+down 2
+down 1
+up 2
+down 5
+forward 3
+forward 7
+forward 9
+up 5
+down 7
+down 3
+forward 3
+down 4
+forward 6
+forward 1
+down 6
+up 5
+up 6
+down 9
+forward 9
+forward 5
+down 3
+forward 5
+up 4
+down 4
+down 6
+forward 2
+forward 2
+down 5
+down 1
+forward 3
+forward 5
+up 5
+down 1
+down 5
+down 3
+down 4
+down 2
+down 2
+forward 5
+down 1
+down 4
+down 6
+forward 4
+down 1
+down 1
+down 7
+forward 1
+down 5
+forward 6
+forward 7
+forward 3
+down 6
+forward 8
+down 8
+down 3
+up 9
+forward 3
+up 5
+forward 9
+forward 3
+forward 5
+down 3
+down 7
+down 5
+up 5
+down 1
+up 5
+down 5
+down 9
+down 4
+down 2
+down 2
+up 2
+up 1
+forward 2
+forward 4
+up 5
+down 1
+forward 6
+up 7
+up 5
+down 5
+forward 4
+down 1
+up 4
+down 2
+forward 3
+forward 4
+down 4
+down 8
+down 4
+up 3
+forward 6
+forward 3
+forward 1
+up 3
+forward 6
+down 1
+up 5
+down 5
+down 9
+down 9
+down 4
+forward 9
+down 6
+down 6
+forward 2
+up 7
+down 4
+down 4
+down 3
+up 1
+down 4
+up 2
+forward 9
+down 2
+down 6
+down 6
+up 5
+down 7
+down 6
+down 9
+down 8
+forward 5
+down 6
+up 3
+down 8
+up 3
+forward 5
+down 1
+forward 4
+forward 3
+down 9
+down 4
+forward 8
+forward 8
+up 4
+forward 7
+up 5
+down 9
+down 2
+forward 6
+down 4
+forward 9
+up 1
+down 7
+down 3
+forward 6
+up 7
+forward 2
+forward 1
+down 9
+up 4
+forward 2
+forward 6
+forward 9
+forward 9
+down 7
+forward 4
+down 2
+forward 1
+forward 8
+down 7
+down 1
+forward 5
+forward 5
+down 8
+up 8
+up 1
+forward 2
+down 8
+forward 6
+forward 1
+forward 2
+forward 7
+down 3
+forward 5
+up 6
+up 8
+down 7
+forward 2
+forward 5
+down 5
+forward 8
+down 6
+down 3
+up 2
+down 7
+forward 5
+down 4
+forward 3
+up 7
+up 6
+forward 4
+forward 7
+forward 6
+down 4
+forward 5
+up 3
+up 5
+down 8
+forward 7
+forward 2
+forward 3
+up 4
+down 1
+up 2
+up 1
+forward 6
+up 3
+down 3
+forward 7
+down 6
+forward 9
+forward 9
+down 7
+forward 1
+down 9
+down 9
+forward 8
+up 9
+up 7
+up 4
+forward 4
+down 3
+down 4
+forward 4
+forward 5
+down 2
+down 7
+down 1
+forward 7
+down 6
+down 6
+forward 3
+up 1
+forward 4
+down 5
+up 7
+up 3
+up 9
+forward 8
+up 4
+forward 2
+down 2
+down 2
+up 8
+forward 1
+forward 3
+forward 7
+down 7
+up 5
+down 3
+down 4
+down 6
+forward 5
+down 1
+forward 5
+down 4
+forward 8
+down 4
+forward 6
+forward 9
+down 5
+up 2
+down 2
+forward 1
+forward 5
+down 1
+down 4
+down 4
+up 6
+forward 7
+forward 2
+forward 9
+down 1
+forward 2
+forward 6
+forward 1
+up 9
+down 7
+down 8
+forward 3
+forward 5
+down 4
+forward 2
+forward 2
+up 8
+forward 3
+forward 8
+forward 5
+up 6
+forward 6
+forward 3
+down 4
+up 8
+down 3
+forward 4
+forward 3
+up 4
+forward 7
+down 9
+forward 3
+down 6
+forward 9
+down 8
+forward 6
+down 7
+down 1
+forward 9
+down 4
+forward 8
+forward 5
+forward 7
+up 5
+up 3
+down 8
+down 5
+down 2
+up 3
+up 7
+down 8
+forward 6
+forward 4
+forward 9
+forward 6
+forward 4
+forward 1
+down 1
+down 3
+up 3
+up 7
+down 9
+down 5
+forward 1
+forward 6
+forward 5
+down 9
+forward 9
+forward 5
+up 5
+forward 8
+forward 6
+forward 4
+down 4
+down 6
+down 1
+forward 1
+down 6
+forward 8
+down 3
+down 6
+down 5
+forward 3
+down 3
+down 8
+up 6
+forward 6
+down 6
+down 4
+down 8
+down 9
+down 3
+down 4
+down 5
+forward 4
+down 7
+down 8
+up 4
+forward 4
+down 9
+forward 1
+forward 1
+down 6
+up 3
+up 6
+down 8
+up 7
+forward 3
+down 6
+down 1
+down 4
+up 4
+down 9
+forward 1
+down 3
+up 1
+forward 8
+up 4
+up 6
+up 4
+down 4
+up 5
+up 3
+down 1
+down 6
+forward 9
+down 2
+forward 9
+forward 8
+down 2
+forward 8
+up 5
+forward 5
+forward 5
+forward 4
+up 8
+down 3
+forward 3
+down 4
+up 1
+forward 4
+up 7
+down 1
+down 4
+forward 1
+forward 9
+down 7
+forward 4
+down 5
+down 7
+forward 9
+down 2
+down 2
+down 1
+down 2
+forward 8
+down 6
+forward 5
+down 2
+down 1
+up 4
+forward 7
+down 1
+down 5
+down 7
+down 6
+down 6
+up 8
+forward 3
+up 8
+up 9
+down 2
+down 1
+down 5
+up 8
+forward 4
+forward 6
+down 2
+down 3
+forward 2
+forward 2
+forward 2
+forward 3
+down 7
+forward 3
+down 4
+up 1
+down 2
+down 9
+up 4
+down 8
+up 7
+down 6
+down 7
+down 6
+forward 6
+down 4
+forward 2
+down 7
+down 1
+forward 2
+forward 5
+forward 6
+down 1
+down 4
+forward 2
+down 4
+down 6
+down 8
+forward 9
+down 8
+forward 5
+down 7
+forward 5
+up 1
+down 1
+down 9
+down 3
+forward 3
+forward 5
+down 6
+forward 6
+up 3
+down 4
+down 4
+forward 7
+forward 3
+down 5
+down 3
+forward 7
+up 5
+forward 1
+down 6
+forward 1
+forward 9
+forward 9
+up 9
+forward 5
+up 5
+forward 6
+down 8
+up 4
+up 8
+forward 9
+forward 4
+down 5
+down 2
+down 9
+forward 3
+forward 6
+forward 2
+up 4
+down 4
+forward 3
+down 8
+down 3
+up 3
+forward 8
+down 4
+forward 4
+forward 4
+up 9
+down 1
+up 5
+up 5
+forward 8
+forward 4
+forward 5
+forward 1
+forward 3
+forward 8
+down 1
+forward 2
+forward 7
+down 4
+down 3
+down 4
+up 7
+forward 9
+forward 7
+down 8
+forward 2
+down 4
+up 9
+down 9
+down 9
+up 2
+down 9
+down 9
+down 6
+up 9
+up 6
+down 7
+forward 3
+forward 9
+forward 2
+down 4
+up 4
+forward 1
+up 1
+down 6
+down 3
+down 2
+up 6
+down 5
+down 8
+up 4
+forward 1
+down 1
+down 4
+down 8
+down 5
+forward 2
+down 5
+forward 3
+forward 1
+forward 9
+forward 4
+down 6
+down 4
+down 3
+forward 5
+forward 4
+down 1
+up 7
+down 4
+up 3
+forward 2
+forward 4
+up 2
+up 9
+forward 4
+down 5
+forward 9
+up 1
+forward 8
+down 9
+down 6
+down 6
+down 2
+down 2
+down 3
+forward 8
+down 2
+down 5
+forward 4
+up 3
+down 4
+up 6
+forward 7
+up 2
+down 6
+down 8
+forward 1
+down 8
+down 9
+down 2
+forward 3
+forward 9
+up 7
+down 1
+down 5
+up 7
+down 5
+up 8
+up 4
+down 6
+forward 1
+forward 6
+forward 9
+forward 6
+forward 6
+forward 9
+forward 8
+down 8
+down 8
+down 4
+down 7
+down 9
+up 9
+down 7
+down 2
+forward 2
+down 5
+down 3
+down 1
+down 4
+down 2
+down 1
+up 2
+forward 6
+down 3
+down 3
+down 1
+forward 8
+down 3
+forward 1
+forward 3
+down 1
+up 7
+down 9
+forward 5
+forward 8
+up 7
+down 4
+down 3
+down 1
+forward 2
+forward 9
+up 5
+down 2
+forward 6
+forward 6
+forward 2
+forward 1
+forward 1
+down 9
+forward 6
+forward 6
+down 2
+down 6
+up 5
+forward 5
+up 4
+forward 4
+down 2
+down 2
+down 7
+down 1
+down 4
+forward 3
+forward 3
+up 8
+down 8
+forward 5
+up 4
+up 9
+up 6
+up 6
+down 7
+forward 2
+up 1
+forward 2
+forward 4
+forward 4
+forward 2
+forward 4
+down 6
+down 3
+forward 9
+forward 3
+down 2
+forward 6
+forward 6
+forward 4
+forward 8
+up 1
+down 1
+forward 1
+down 7
+up 6
+up 8
+down 1
+down 4
+down 3
+up 4
+forward 4
+forward 5
+down 5
+down 8
+up 7
+down 2
+down 6
+forward 5
  No newline at end of file

          
M Java/pom.xml +7 -0
@@ 10,4 10,11 @@ 
         <maven.compiler.source>1.8</maven.compiler.source>
         <maven.compiler.target>1.8</maven.compiler.target>
     </properties>
+    <dependencies>
+        <dependency>
+            <groupId>org.junit.jupiter</groupId>
+            <artifactId>junit-jupiter-engine</artifactId>
+            <version>5.5.2</version>
+        </dependency>
+    </dependencies>
 </project>
  No newline at end of file

          
A => Java/src/main/java/com/ajtjp/advent2021/SubPiloting.java +31 -0
@@ 0,0 1,31 @@ 
+
+package com.ajtjp.advent2021;
+
+import java.util.Scanner;
+
+/**
+ *
+ * @author Andrew
+ */
+public class SubPiloting {
+    public static int Silver(Scanner scanner) {
+        int forward = 0;
+        int depth = 0;
+        while (scanner.hasNextLine()) {
+            String nextLine = scanner.nextLine();
+            String[] parts = nextLine.split(" ");
+            String command = parts[0];
+            Integer distance = Integer.parseInt(parts[1]);
+            if (command.equals("forward")) {
+                forward += distance;
+            }
+            else if (command.equals("up")) {
+                depth -= distance;
+            }
+            else if (command.equals("down")) {
+                depth += distance;
+            }
+        }
+        return forward * depth;
+    }
+}

          
A => Java/src/test/java/com/ajtjp/advent2021/SubPilotingTest.java +35 -0
@@ 0,0 1,35 @@ 
+
+package com.ajtjp.advent2021;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
+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 = 
+"forward 5\n" +
+"down 5\n" +
+"forward 8\n" +
+"up 3\n" +
+"down 8\n" +
+"forward 2";
+        Scanner scanner = new Scanner(basicData);
+        int result = SubPiloting.Silver(scanner);
+        assertEquals(150, result);
+    }
+    
+    @Test
+    public void testSilver() throws FileNotFoundException {
+        Scanner scanner = new Scanner(new File("data/day2.txt"));
+        int answer = SubPiloting.Silver(scanner);
+        assertEquals(1947824, answer);
+    }
+}