Skip to content

Commit e0ee151

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

File tree

14 files changed

+2136
-62
lines changed

14 files changed

+2136
-62
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
target/
1+
target/
2+
.idea
3+
*.iml

.vscode/launch.json

Lines changed: 0 additions & 26 deletions
This file was deleted.

.vscode/settings.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ mvn clean test
1818

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

21-
21+
- [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/)
2222

2323

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
<version>4.11</version>
2626
<scope>test</scope>
2727
</dependency>
28+
<dependency>
29+
<groupId>org.assertj</groupId>
30+
<artifactId>assertj-core</artifactId>
31+
<version>3.21.0</version>
32+
<scope>test</scope>
33+
</dependency>
2834
</dependencies>
2935

3036
<build>
Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
package com.codingnagger;
22

3-
import java.io.IOException;
4-
3+
import com.codingnagger.days.Day;
4+
import com.codingnagger.days.Day1;
55
import com.codingnagger.utils.InputLoader;
66

7+
import java.io.IOException;
8+
import java.util.List;
9+
710
/**
811
* Hello world!
9-
*
1012
*/
11-
public class App
12-
{
13-
public static void main( String[] args ) throws IOException
14-
{
15-
System.out.println( "Advent of Code 2021" );
13+
public class App {
14+
public static void main(String[] args) throws IOException {
15+
System.out.println("Advent of Code 2021");
1616

17-
Iterable<String> input = new InputLoader().Load("day1.txt");
17+
List<String> input = new InputLoader().Load("day1.txt");
1818

19-
// Day day = new Day1();
19+
Day day = new Day1();
2020

21-
System.out.println( "Part 1:" );
22-
// System.out.println(day1.partOne(input));
21+
System.out.println("Part 1:");
22+
System.out.println(day.partOne(input));
2323

24-
System.out.println( "Part 2:" );
25-
// System.out.println(day1.partTwo(input));
24+
System.out.println("Part 2:");
25+
System.out.println(day.partTwo(input));
2626
}
2727
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.codingnagger.days;
22

3+
import java.util.List;
4+
35
public interface Day {
4-
public String Part1(Iterable<String> input);
5-
public String Part2(Iterable<String> input);
6+
String partOne(List<String> input);
7+
8+
String partTwo(List<String> input);
69
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.codingnagger.days;
2+
3+
import java.util.List;
4+
5+
import static com.codingnagger.utils.InputConverter.convertToIntegers;
6+
7+
public class Day1 implements Day {
8+
9+
@Override
10+
public String partOne(List<String> input) {
11+
return computeIncreases(input, 1);
12+
}
13+
14+
@Override
15+
public String partTwo(List<String> input) {
16+
return computeIncreases(input, 3);
17+
}
18+
19+
private String computeIncreases(List<String> input, int slidingWindowSize) {
20+
List<Integer> integerInput = convertToIntegers(input);
21+
int increaseCount = 0;
22+
int previous = slidingWindowSum(integerInput, 0, slidingWindowSize);
23+
24+
for (int i = 1; i < integerInput.size() - slidingWindowSize + 1; i++) {
25+
int value = slidingWindowSum(integerInput, i, slidingWindowSize);
26+
27+
if (value > previous) {
28+
increaseCount++;
29+
}
30+
previous = value;
31+
}
32+
return "" + increaseCount;
33+
}
34+
35+
private int slidingWindowSum(List<Integer> integerInput, int index, int slidingWindowSize) {
36+
int sum = 0;
37+
for (int i = 0; i < slidingWindowSize; i++) {
38+
sum += integerInput.get(i + index);
39+
}
40+
return sum;
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.codingnagger.utils;
2+
3+
import java.util.List;
4+
import java.util.stream.Collectors;
5+
6+
public class InputConverter {
7+
public static List<Integer> convertToIntegers(List<String> input) {
8+
return input.stream().map(Integer::parseInt).collect(Collectors.toList());
9+
}
10+
}

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

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

88
public class InputLoader {
9-
public List<String> Load(String filename) throws IOException {
10-
return Files.readAllLines(Paths.get("src","main","resources", filename));
11-
}
9+
public static List<String> Load(String filename) throws IOException {
10+
return Load("main", filename);
11+
}
12+
13+
public static List<String> LoadTest(String filename) throws IOException {
14+
return Load("test", filename);
15+
}
16+
17+
public static List<String> Load(String resourcesParent, String filename) throws IOException {
18+
return Files.readAllLines(Paths.get("src", resourcesParent, "resources", filename));
19+
}
1220
}

0 commit comments

Comments
 (0)