Last active
December 9, 2020 19:21
-
-
Save gitaficionado/901a7023ea0fa1eb16f349e8691c583d to your computer and use it in GitHub Desktop.
Fetches file from res folder. Thanks to TurkeyDev for the code.
This file contains 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
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.List; | |
public abstract class AOCPuzzle | |
{ | |
private long timerStart; | |
public AOCPuzzle(String day) | |
{ | |
File file = new File("res/day" + day + ".txt"); | |
if(!file.exists()) | |
{ | |
solve(new ArrayList<>()); | |
return; | |
} | |
BufferedReader reader; | |
try | |
{ | |
reader = new BufferedReader(new FileReader(file)); | |
} catch(FileNotFoundException e) | |
{ | |
System.err.println("File not found!!"); | |
solve(new ArrayList<>()); | |
return; | |
} | |
List<String> inputLines = new ArrayList<>(); | |
try | |
{ | |
String line; | |
while((line = reader.readLine()) != null) | |
inputLines.add(line); | |
reader.close(); | |
} catch(IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
timerStart = System.nanoTime(); | |
solve(inputLines); | |
lap(); | |
} | |
abstract void solve(List<String> input); | |
public void lap() | |
{ | |
long timeSpent = (System.nanoTime() - timerStart) / 1000; | |
if(timeSpent < 1000) | |
System.out.println("Duration: " + timeSpent + "µs"); | |
else | |
System.out.println("Duration: " + (timeSpent / 1000.0) + "ms"); | |
timerStart = System.nanoTime(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment