From 24e81aea1ab7f92033552e2d26292b1aea5cd7fe Mon Sep 17 00:00:00 2001
From: Christopher Bohn <bohn@unl.edu>
Date: Sat, 11 Dec 2021 22:11:19 -0600
Subject: [PATCH] Completed 2021 day 2

---
 2021/README.md                                | 12 ++++
 .../java/edu/unl/cse/bohn/year2021/Day1.java  |  2 +-
 .../java/edu/unl/cse/bohn/year2021/Day2.java  | 70 +++++++++++++++++++
 README.md                                     |  7 +-
 4 files changed, 87 insertions(+), 4 deletions(-)
 create mode 100644 2021/README.md
 create mode 100644 2021/src/main/java/edu/unl/cse/bohn/year2021/Day2.java

diff --git a/2021/README.md b/2021/README.md
new file mode 100644
index 0000000..eefe460
--- /dev/null
+++ b/2021/README.md
@@ -0,0 +1,12 @@
+# 2021 Advent of Coding Solutions
+
+## Day 1
+
+A nice little warmup exercise in which we iterate over a list. Notice that I
+get to use Java's new multiline strings.
+
+## Day 2
+
+Can we follow instructions? Yes we can. This isn't a particularly challenging
+problem. Beside using Java's new Records, I'm also using Java's new style of
+switch statements.
\ No newline at end of file
diff --git a/2021/src/main/java/edu/unl/cse/bohn/year2021/Day1.java b/2021/src/main/java/edu/unl/cse/bohn/year2021/Day1.java
index 2ad7cd2..65aaee9 100644
--- a/2021/src/main/java/edu/unl/cse/bohn/year2021/Day1.java
+++ b/2021/src/main/java/edu/unl/cse/bohn/year2021/Day1.java
@@ -8,7 +8,7 @@ import java.util.stream.Collectors;
 
 @SuppressWarnings("unused")
 public class Day1 extends Puzzle {
-    List<Integer> depths;
+    private List<Integer> depths;
 
     public Day1() {
         day = 1;
diff --git a/2021/src/main/java/edu/unl/cse/bohn/year2021/Day2.java b/2021/src/main/java/edu/unl/cse/bohn/year2021/Day2.java
new file mode 100644
index 0000000..c0a5fc1
--- /dev/null
+++ b/2021/src/main/java/edu/unl/cse/bohn/year2021/Day2.java
@@ -0,0 +1,70 @@
+package edu.unl.cse.bohn.year2021;
+
+import edu.unl.cse.bohn.Puzzle;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+@SuppressWarnings("unused")
+public class Day2 extends Puzzle {
+    private record NavigationInstruction(String direction, int magnitude) {
+    }
+
+    private List<NavigationInstruction> navigationInstructions;
+
+    private int range;
+    private int depth;
+    @SuppressWarnings("FieldCanBeLocal")
+    private int aim;
+
+    public Day2() {
+        day = 2;
+        sampleData = """
+                forward 5
+                down 5
+                forward 8
+                up 3
+                down 8
+                forward 2""";
+        isProductionReady = true;
+    }
+
+    @Override
+    public int computePart1(List<String> data) {
+        navigationInstructions = data.stream()
+                .map(instruction -> new NavigationInstruction(
+                        instruction.split(" ")[0],
+                        Integer.parseInt(instruction.split(" ")[1])))
+                .collect(Collectors.toList());
+        range = 0;
+        depth = 0;
+        for (NavigationInstruction instruction : navigationInstructions) {
+            switch (instruction.direction()) {
+                case ("forward") -> range += instruction.magnitude();
+                case ("down") -> depth += instruction.magnitude();
+                case ("up") -> depth -= instruction.magnitude();
+                default -> throw new IllegalStateException("Unknown direction: " + instruction.direction());
+            }
+        }
+        return range * depth;
+    }
+
+    @Override
+    public int computePart2(List<String> data) {
+        range = 0;
+        depth = 0;
+        aim = 0;
+        for (NavigationInstruction instruction : navigationInstructions) {
+            switch (instruction.direction()) {
+                case ("forward") -> {
+                    range += instruction.magnitude();
+                    depth += aim * instruction.magnitude();
+                }
+                case ("down") -> aim += instruction.magnitude();
+                case ("up") -> aim -= instruction.magnitude();
+                default -> throw new IllegalStateException("Unknown direction: " + instruction.direction());
+            }
+        }
+        return range * depth;
+    }
+}
diff --git a/README.md b/README.md
index 4c081cc..1b9a1d6 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,6 @@
-# 2021 Advent of Coding Solutions
+# Advent of Code
 
-## Day 1
+My solutions to the [Advent of Code](https://adventofcode.com/) puzzles.
+
+I'm making [notes](2021/README.md) about the 2021 puzzles and my solutions.
 
-A nice little warmup exercise in which we iterate over a list.
-- 
GitLab