Skip to content

Instantly share code, notes, and snippets.

@desrtfx
Created December 14, 2015 11:20
Show Gist options
  • Save desrtfx/4dad37564305ced0c0f8 to your computer and use it in GitHub Desktop.
Save desrtfx/4dad37564305ced0c0f8 to your computer and use it in GitHub Desktop.
Advent of Code Day14 Version 2
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Day14v2 {
private static final String FILENAME = "D:\\Dokumente\\Java Workspace\\AdventOfCodeDay14\\InputDay14.txt";
private static final Pattern PATTERN = Pattern
.compile("(.+) can fly (\\d+) km/s for (\\d+) seconds, but then must rest for (\\d+) seconds.");
private static final int TARGET_TIME = 2503; // 2503 Seconds
enum State {
FLY, REST
};
List<Reindeer> racers = new ArrayList<>();
class Reindeer {
String name;
int speed;
int flyTime;
int restTime;
int distance;
int points;
State state;
int currentFlyTime;
int currentRestTime;
public Reindeer(String name, int speed, int flyTime, int restTime) {
this.name = name;
this.speed = speed;
this.flyTime = flyTime;
this.restTime = restTime;
distance = 0;
points = 0;
currentFlyTime = 0;
currentRestTime = 0;
fly(); // Every Reindeer starts flying
}
void fly() { // start flying
state = State.FLY;
currentFlyTime = 0;
}
void rest() { // start resting
state = State.REST;
currentRestTime = 0;
}
void tick() { // process one tick race time
if (state == State.FLY) { // Reindeer is flying
distance += speed;
if (currentFlyTime == flyTime - 1) {
rest();
}
currentFlyTime++;
} else { // Reindeer is resting
if (currentRestTime == restTime - 1) {
fly();
}
currentRestTime++;
}
}
}
public Day14v2() {
// initialize data
List<String> data = FileIO.getFileAsList(FILENAME);
for (String s : data) {
Matcher m = PATTERN.matcher(s);
if (m.matches()) {
Reindeer r = new Reindeer(m.group(1), Integer.parseInt(m
.group(2)), Integer.parseInt(m.group(3)),
Integer.parseInt(m.group(4)));
add(r);
}
}
}
void race() { // run the race
for (int second = 0; second < TARGET_TIME; second++) {
tick();
}
}
void tick() { // one race round
racers.forEach(Reindeer::tick);
checkCurrentLead();
}
void checkCurrentLead() { // find the current leader of the race
int max = winner();
racers.stream().filter(r -> r.distance == max).forEach(r -> r.points++);
}
int winner() { // get the max distance at the current state of the race
return racers.stream().mapToInt(r -> r.distance).max().getAsInt();
}
void add(Reindeer r) {
racers.add(r); // add the racers
}
int pointsWinner() { // get the max points at the current state of the race
return racers.stream().mapToInt(p -> p.points).max().getAsInt();
}
public static void main(String[] args) {
Day14v2 day14 = new Day14v2();
day14.race(); // run the Reindeer race
System.out.println("Part 1: Max Distance = " + day14.winner());
System.out.println("Part 2: Max Points = " + day14.pointsWinner());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment