Terrana Ninetailed 8 months ago
commit
39c6579618
  1. 1
      .gitignore
  2. 2000
      resources/input/day01/input
  3. 50
      src/net/ninetailed/adventofcode2021/Day01.java
  4. 8
      src/net/ninetailed/adventofcode2021/Solution.java
  5. 18
      src/net/ninetailed/adventofcode2021/Util.java
  6. 46
      test/net/ninetailed/adventofcode2021/TestDay01.java

1
.gitignore

@ -0,0 +1 @@
/out/

2000
resources/input/day01/input

File diff suppressed because it is too large

50
src/net/ninetailed/adventofcode2021/Day01.java

@ -0,0 +1,50 @@
package net.ninetailed.adventofcode2021;
import java.util.Iterator;
import java.util.List;
public class Day01 implements Solution {
public static void main(String[] args) throws Exception {
List<String> input = Util.loadInput("resources/input/day01/input");
Day01 solution = new Day01();
System.out.println("Part 1");
System.out.println(solution.part1(input));
System.out.println();
System.out.println("Part 2");
System.out.println(solution.part2(input));
}
private static int[] parseInput(List<String> input) {
int[] parsed = new int[input.size()];
Iterator<String> iter = input.iterator();
for (int i = 0; i < parsed.length; i++) {
parsed[i] = Integer.parseInt(iter.next());
}
return parsed;
}
public String part1(List<String> rawInput) {
return String.valueOf(countIncreases(parseInput(rawInput)));
}
public String part2(List<String> rawInput) {
return String.valueOf(countIncreases(generateTripleSums(rawInput)));
}
int[] generateTripleSums(List<String> rawInput) {
int[] input = parseInput(rawInput);
int[] sums = new int[input.length - 2];
for (int i = 0; i < sums.length; i++) {
sums[i] = input[i] + input[i+1] + input[i+2];
}
return sums;
}
private int countIncreases(int[] input) {
int increases = 0;
for (int i = 1; i < input.length; i++) {
if (input[i-1] < input[i]) increases++;
}
return increases;
}
}

8
src/net/ninetailed/adventofcode2021/Solution.java

@ -0,0 +1,8 @@
package net.ninetailed.adventofcode2021;
import java.util.List;
public interface Solution {
String part1(List<String> input);
String part2(List<String> input);
}

18
src/net/ninetailed/adventofcode2021/Util.java

@ -0,0 +1,18 @@
package net.ninetailed.adventofcode2021;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Util {
public static List<String> loadInput(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
List<String> buffer = new ArrayList<>();
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
buffer.add(line);
}
return buffer;
}
}

46
test/net/ninetailed/adventofcode2021/TestDay01.java

@ -0,0 +1,46 @@
package net.ninetailed.adventofcode2021;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
public class TestDay01 {
private static final List<String> TEST_INPUT = Arrays.asList(
("199\n" +
"200\n" +
"208\n" +
"210\n" +
"200\n" +
"207\n" +
"240\n" +
"269\n" +
"260\n" +
"263")
.split("\n"));
Day01 solution;
@Before
public void setup() {
solution = new Day01();
}
@Test
public void testPart1() {
Assert.assertEquals("7", solution.part1(TEST_INPUT));
}
@Test
public void testPart2TripleSums() {
int[] sums = solution.generateTripleSums(TEST_INPUT);
Assert.assertArrayEquals(new int[] {607, 618, 618, 617, 647, 716, 769, 792}, sums);
}
@Test
public void testPart2() {
Assert.assertEquals("5", solution.part2(TEST_INPUT));
}
}
Loading…
Cancel
Save