Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 1, 2017 06:03
Show Gist options
  • Save snarkbait/846cfa25801daad332b5583dc89ff755 to your computer and use it in GitHub Desktop.
Save snarkbait/846cfa25801daad332b5583dc89ff755 to your computer and use it in GitHub Desktop.
Advent of Code 2017 Day 1
package Advent2017;
import util.FileIO;
import util.Timer;
/**
* @author /u/Philboyd_Studge
*/
public class Day1 {
private static int part1(String s) {
return solve(s, 1);
}
private static int part2(String s) {
return solve(s, s.length() / 2);
}
private static int solve(String s, int dist) {
int sum = 0;
for (int i = 0; i < s.length(); i++) {
int next = (i + dist) % s.length();
if (s.charAt(i) == s.charAt(next)) {
sum += val(s.charAt(i));
}
}
return sum;
}
private static int val(char c) {
return c ^ 0x30;
}
public static void main(String[] args) {
Timer.startTimer();
int result = FileIO.performIntActionOnLine("advent2017_day1.txt", Day1::part1);
System.out.println("Part 1: " + result);
int result2 = FileIO.performIntActionOnLine("advent2017_day1.txt", Day1::part2);
System.out.println("Part 2: " + result2);
System.out.println(Timer.endTimer());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment