Created
December 16, 2017 07:29
-
-
Save snarkbait/6d4d1c54923f3f63cb9dabe665a69184 to your computer and use it in GitHub Desktop.
Advent of Code 2017 - Abstract Challenge Class, Loader, Runner, and more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import java.util.List; | |
public abstract class AdventOfCode { | |
protected List<String> input; | |
public AdventOfCode(List<String> input) { | |
this.input = input; | |
parse(); | |
} | |
public abstract Object part1(); | |
public abstract Object part2(); | |
public abstract void parse(); | |
public void run() { | |
System.out.println("Part 1:" + part1().toString()); | |
System.out.println("Part 2:" + part2().toString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
import java.lang.reflect.Constructor; | |
import java.lang.reflect.InvocationTargetException; | |
import java.util.List; | |
public class DayLoader { | |
private DayLoader() {} | |
public static AdventOfCode getClassForDay(int year, int day) { | |
if (year < 2015 || year > 2017 || day < 1 || day > 25) { | |
throw new IllegalArgumentException("Invalid date for Advent of Code."); | |
} | |
try { | |
String name = "Advent" + year + ".Day" + day; | |
Constructor constructor = Class.forName(name).getConstructor(List.class); | |
return (AdventOfCode)constructor.newInstance(FileIO.getAOCInputForDay(year, day, FileIO.SESSION_ID)); | |
} catch (ClassNotFoundException | InstantiationException | | |
IllegalAccessException | InvocationTargetException | | |
NoSuchMethodException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Advent2017; | |
import util.FileIO; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
public class Make { | |
private static void makeDay(int year, int day) { | |
String className = "Day" + day; | |
String file = "src/Advent" + year + "/" + className + ".java"; | |
if (!Files.exists(Paths.get(file))) { | |
List<String> template = FileIO.getFileAsList("advent_template.txt"); | |
template = template.stream() | |
.map(x -> x.replace("Template", className)) | |
.collect(Collectors.toList()); | |
FileIO.writeListToFile(template, file); | |
System.out.println("Writing file: " + file); | |
} else { | |
System.out.println("File: " + file + " already exists."); | |
} | |
} | |
private static void makeAll(int year) { | |
for (int i = 0; i < 26; i++) { | |
makeDay(year, i); | |
} | |
} | |
public static void main(String[] args) { | |
makeAll(2017); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util; | |
public class Runner { | |
public static void runAll(int year, int upToDay) { | |
for (int i = 1; i <= upToDay; i++) { | |
run(year, i); | |
} | |
} | |
public static void run(int year, int day) { | |
AdventOfCode challenge = DayLoader.getClassForDay(year, day); | |
if (challenge == null) throw new RuntimeException("Unable to load class/input"); | |
Timer.startTimer(); | |
System.out.println("<===== Advent of Code challenge output for Year: " + year + " - Day " + day + " =====>"); | |
challenge.run(); | |
System.out.println(Timer.endTimer()); | |
} | |
public static void main(String[] args) { | |
runAll(2017, 16); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment