Skip to content

Instantly share code, notes, and snippets.

@paulhoadley
Last active January 1, 2019 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulhoadley/51267f9b5914d7d746e0ef32abeeea43 to your computer and use it in GitHub Desktop.
Save paulhoadley/51267f9b5914d7d746e0ef32abeeea43 to your computer and use it in GitHub Desktop.
Advent of Code 2018: Day 1
package net.logicsquad.advent.y2018;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Day1 {
private static final String INPUT_FILENAME = "etc/2018/day1.input";
public static void main(String args[]) throws IOException {
try (Stream<String> values = Files.lines(Paths.get(INPUT_FILENAME))) {
List<Integer> ints = values.map(s -> Integer.valueOf(s)).collect(Collectors.toList());
Integer current = Integer.valueOf(0);
List<Integer> freqs = new ArrayList<>();
Integer result = null;
while (result == null) {
for (Integer i : ints) {
current = current + i;
if (freqs.contains(current)) {
result = current;
break;
} else {
freqs.add(current);
}
}
}
System.out.println("Day1.main: result = " + result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment