diff --git a/2021/README.md b/2021/README.md new file mode 100644 index 0000000000000000000000000000000000000000..eefe460fe93a733b3721f3c237688dff5673c5ff --- /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 2ad7cd2dbd863120672cbc78ec8431534f7fe6a4..65aaee9fbc03a329b6b67d8d3885433377cfff06 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 0000000000000000000000000000000000000000..c0a5fc1b2bfc6f9ca67694005e6908b39f74df79 --- /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 4c081ccc9269d9530b25a813f82950697918e739..1b9a1d6c2463106cd5970904014876b4cd3b7a59 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.