Skip to content

Commit db781d6

Browse files
committed
Day 1
1 parent 361742a commit db781d6

File tree

4 files changed

+77
-12
lines changed

4 files changed

+77
-12
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
.settings
55
advent-of-code.iml
66
target/
7+
src/test/resources/config.properties
8+
src/test/resources/input
9+
src/test/resources/2020

src/test/java/com/macasaet/Day01.java

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,65 @@
11
package com.macasaet;
22

3-
import java.util.stream.Stream;
3+
import java.util.ArrayList;
4+
import java.util.LinkedList;
5+
import java.util.List;
46
import java.util.stream.StreamSupport;
57

68
import org.junit.jupiter.api.Test;
79

8-
10+
/**
11+
* --- Day 1: Sonar Sweep ---
12+
*/
913
public class Day01 {
1014

11-
protected Stream<String> getInput() {
12-
return StreamSupport.stream(new LineSpliterator(getClass().getResourceAsStream("/day-1-input.txt")),
13-
false);
15+
/**
16+
* Perform a sonar sweep of the nearby sea floor.
17+
*
18+
* @return measurements of the sea floor depth further and further away from the submarine
19+
*/
20+
protected List<Integer> getInput() {
21+
return StreamSupport
22+
.stream(new LineSpliterator("day-01.txt"),
23+
false)
24+
.mapToInt(Integer::parseInt)
25+
.collect(ArrayList::new, List::add, List::addAll);
1426
}
1527

1628
@Test
1729
public final void part1() {
18-
30+
final var list = getInput();
31+
int increases = countIncreases(list);
32+
System.out.println("Part 1: " + increases);
1933
}
2034

2135
@Test
2236
public final void part2() {
37+
final var list = getInput();
38+
final var windows = new LinkedList<Integer>();
39+
for (int i = 2; i < list.size(); i++) {
40+
windows.add(list.get(i) + list.get(i - 1) + list.get(i - 2));
41+
}
42+
final int increases = countIncreases(windows);
43+
System.out.println("Part 2: " + increases);
44+
}
2345

46+
/**
47+
* Determine how quickly the depth increases.
48+
*
49+
* @param list progressively further measurements of the sea floor depth
50+
* @return the number of times a depth measurement increase from the previous measurement
51+
*/
52+
protected int countIncreases(final List<? extends Integer> list) {
53+
int previous = list.get(0);
54+
int increases = 0;
55+
for (int i = 1; i < list.size(); i++) {
56+
final var current = list.get(i);
57+
if (current > previous) {
58+
increases++;
59+
}
60+
previous = current;
61+
}
62+
return increases;
2463
}
25-
}
64+
65+
}

src/test/java/com/macasaet/LineSpliterator.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package com.macasaet;
22

3-
import java.io.BufferedReader;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.io.InputStreamReader;
7-
import java.io.Reader;
3+
import java.io.*;
84
import java.util.Objects;
5+
import java.util.Properties;
96
import java.util.Spliterator;
107
import java.util.function.Consumer;
118

129
public class LineSpliterator implements Spliterator<String>, AutoCloseable {
1310

11+
private static final String prefix;
1412
private final BufferedReader reader;
1513

14+
static {
15+
final var properties = new Properties();
16+
try {
17+
final var config = LineSpliterator.class.getResourceAsStream("/config.properties");
18+
if (config != null) properties.load(config);
19+
} catch (final IOException ignored) {
20+
}
21+
prefix = properties.getProperty("prefix", "/sample");
22+
}
23+
1624
public LineSpliterator(final BufferedReader reader) {
1725
Objects.requireNonNull(reader);
1826
this.reader = reader;
@@ -26,6 +34,10 @@ public LineSpliterator(final InputStream stream) {
2634
this(new InputStreamReader(stream));
2735
}
2836

37+
public LineSpliterator(final String fileName) {
38+
this(LineSpliterator.class.getResourceAsStream(prefix + "/" + fileName));
39+
}
40+
2941
public boolean tryAdvance(final Consumer<? super String> action) {
3042
try {
3143
final var line = reader.readLine();

src/test/resources/sample/day-01.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
199
2+
200
3+
208
4+
210
5+
200
6+
207
7+
240
8+
269
9+
260
10+
263

0 commit comments

Comments
 (0)