Skip to content

Commit b72390d

Browse files
committed
Advent of Code 2021 - Day 2
1 parent e0ee151 commit b72390d

File tree

8 files changed

+1128
-15
lines changed

8 files changed

+1128
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ mvn clean test
1919
Last year I ran a [cruise log for my 25 days competing in the Advent of Code](https://www.codingnagger.com/tag/advent-of-code-2020/) and decided I would at least try to do the same this year.
2020

2121
- [The Advent of Code 2021 Day 1 log, starting slow](https://www.codingnagger.com/2021/12/01/the-advent-of-code-2021-day-1-log-starting-slow/)
22+
- [The Advent of Code 2021 Day 2 log, sneaky release](https://www.codingnagger.com/2021/12/02/the-advent-of-code-2021-day-2-log-sneaky-release/)
2223

2324

src/main/java/com/codingnagger/App.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.codingnagger.days.Day;
44
import com.codingnagger.days.Day1;
5+
import com.codingnagger.days.Day2;
56
import com.codingnagger.utils.InputLoader;
67

78
import java.io.IOException;
@@ -14,9 +15,9 @@ public class App {
1415
public static void main(String[] args) throws IOException {
1516
System.out.println("Advent of Code 2021");
1617

17-
List<String> input = new InputLoader().Load("day1.txt");
18+
List<String> input = InputLoader.Load("day2.txt");
1819

19-
Day day = new Day1();
20+
Day day = new Day2();
2021

2122
System.out.println("Part 1:");
2223
System.out.println(day.partOne(input));
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.codingnagger.days;
2+
3+
import java.util.List;
4+
5+
import static com.codingnagger.utils.InputConverter.convertToIntegers;
6+
7+
public class Day2 implements Day {
8+
9+
@Override
10+
public String partOne(List<String> input) {
11+
Submarine s = new Submarine();
12+
input.forEach(i -> s.move(i));
13+
return "" + s.depth * s.horizontalPosition;
14+
}
15+
16+
@Override
17+
public String partTwo(List<String> input) {
18+
Submarine2 s = new Submarine2();
19+
input.forEach(i -> s.move(i));
20+
return "" + s.depth * s.horizontalPosition;
21+
}
22+
23+
public class Submarine2 {
24+
int depth = 0;
25+
int horizontalPosition = 0;
26+
int aim = 0;
27+
28+
void move(String action) {
29+
String[] actionDetails = action.split(" ");
30+
String command = actionDetails[0];
31+
int value = Integer.parseInt(actionDetails[1]);
32+
33+
switch (command) {
34+
case "forward":
35+
horizontalPosition += value;
36+
depth += aim * value;
37+
break;
38+
39+
case "down":
40+
aim += value;
41+
break;
42+
43+
case "up":
44+
aim -= value;
45+
break;
46+
}
47+
}
48+
}
49+
50+
public class Submarine {
51+
int depth = 0;
52+
int horizontalPosition = 0;
53+
54+
void move(String action) {
55+
String[] actionDetails = action.split(" ");
56+
String command = actionDetails[0];
57+
int value = Integer.parseInt(actionDetails[1]);
58+
59+
switch (command) {
60+
case "forward":
61+
horizontalPosition += value;
62+
break;
63+
64+
case "down":
65+
depth += value;
66+
break;
67+
68+
case "up":
69+
depth -= value;
70+
break;
71+
}
72+
}
73+
}
74+
}

src/main/java/com/codingnagger/utils/InputLoader.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,19 @@
66
import java.util.List;
77

88
public class InputLoader {
9-
public static List<String> Load(String filename) throws IOException {
9+
public static List<String> Load(String filename) {
1010
return Load("main", filename);
1111
}
1212

13-
public static List<String> LoadTest(String filename) throws IOException {
13+
public static List<String> LoadTest(String filename) {
1414
return Load("test", filename);
1515
}
1616

17-
public static List<String> Load(String resourcesParent, String filename) throws IOException {
18-
return Files.readAllLines(Paths.get("src", resourcesParent, "resources", filename));
17+
private static List<String> Load(String resourcesParent, String filename) {
18+
try {
19+
return Files.readAllLines(Paths.get("src", resourcesParent, "resources", filename));
20+
} catch (IOException e) {
21+
throw new RuntimeException(e);
22+
}
1923
}
2024
}

0 commit comments

Comments
 (0)