Skip to content

Instantly share code, notes, and snippets.

@snarkbait
Last active December 14, 2015 07:21
Show Gist options
  • Save snarkbait/30cadc5e24eeda44325f to your computer and use it in GitHub Desktop.
Save snarkbait/30cadc5e24eeda44325f to your computer and use it in GitHub Desktop.
Advent Day 14 AdventOfCode
import java.util.ArrayList;
import java.util.List;
/**
* @author /u/Philboyd_Studge on 12/13/2015.
*/
public class Advent14 {
List<Reindeer> deer = new ArrayList<>();
public void add(Reindeer r) {
deer.add(r);
}
public void tick() {
deer.forEach(Reindeer::tick);
checkCurrentWinner();
}
public void checkCurrentWinner() {
int max = winner();
deer.stream()
.filter(x -> x.distance==max)
.forEach(x -> x.points++);
}
public int winner() {
return deer.stream()
.mapToInt(x -> x.distance)
.max()
.getAsInt();
}
public int pointsWinner() {
return deer.stream()
.mapToInt(x -> x.points)
.max()
.getAsInt();
}
public static void main(String[] args) {
Advent14 race = new Advent14();
List<String[]> input = FileIO.getFileLinesSplit("advent14.txt", " ");
input.stream()
.forEach(x -> race.add(new Reindeer(x[0], Integer.parseInt(x[3]),
Integer.parseInt(x[6]), Integer.parseInt(x[13]))));
for (int i = 0; i < 2503; i++) {
race.tick();
}
System.out.println("Winner :" + race.winner());
System.out.println("Points winner :" + race.pointsWinner());
}
}
/**
* @author /u/Philboyd_Studge on 12/13/2015.
*/
public class Reindeer {
String name;
int speed;
int maxTime;
int restTime;
int flyCounter;
int restCounter;
int distance;
int points;
boolean flying;
public Reindeer(String name, int speed, int maxTime, int restTime) {
this.name = name;
this.speed = speed;
this.maxTime = maxTime;
this.restTime = restTime;
fly();
}
private void fly() {
flying = true;
flyCounter = 0;
}
private void rest() {
flying = false;
restCounter = 0;
}
public void tick() {
if (flying) {
distance += speed;
if (flyCounter++ == maxTime - 1) {
rest();
}
} else {
if (restCounter++ == restTime - 1) {
fly();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment