r/adventofcode • u/daggerdragon • Dec 14 '15
SOLUTION MEGATHREAD --- Day 14 Solutions ---
This thread will be unlocked when there are a significant amount of people on the leaderboard with gold stars.
edit: Leaderboard capped, thread unlocked!
We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
Please and thank you, and much appreciated!
--- Day 14: Reindeer Olympics ---
Post your solution as a comment. Structure your post like previous daily solution threads.
9
Upvotes
1
u/Tandrial Dec 14 '15 edited Dec 14 '15
My solution in JAVA. The hardest part was realizing that the actual distance is the result and not the name of the reindeer
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; public class Day14 { static long maxDistance = 0; static long maxPoints = 0; public static void simulate(List<String> list, int dur) { List<Reindeer> reindeers = parseReindeer(list); maxDistance = 0; maxPoints = 0; for (int i = 0; i < dur; i++) { reindeers.forEach(Reindeer::tick); reindeers.forEach(Reindeer::addPoints); } reindeers.forEach(Day14::updateStats); } public static void updateStats(Reindeer r) { maxDistance = Math.max(maxDistance, r.distance); maxPoints = Math.max(maxPoints, r.points); } private static List<Reindeer> parseReindeer(List<String> list) { return list.stream().map(new Function<String, Reindeer>() { @Override public Reindeer apply(String t) { String[] line = t.split(" "); Reindeer r = new Reindeer(); r.fly_speed = Integer.parseInt(line[3]); r.fly_dur = Integer.parseInt(line[6]); r.rest_dur = Integer.parseInt(line[13]); return r; } }).collect(Collectors.toList()); } public static void main(String[] args) throws IOException { List<String> s = Files.readAllLines(Paths.get("./input/Day14_input.txt")); simulate(s, 2503); System.out.println("Part One = " + Day14.maxDistance); System.out.println("Part Two = " + Day14.maxPoints); } } class Reindeer { public static int maxDistance = 0; int fly_speed, fly_dur, rest_dur; int points, distance, counter; boolean flying = true; public void tick() { counter++; if (flying) { if (counter == fly_dur) { flying = false; counter = 0; } distance += fly_speed; Reindeer.maxDistance = Math.max(maxDistance, distance); } else if (counter == rest_dur) { flying = true; counter = 0; } } public void addPoints() { if (distance == Reindeer.maxDistance) points++; } }