Created
December 8, 2020 23:11
-
-
Save missionarydev/d54ed59bc3eff3cbedeacaf4df8b750c to your computer and use it in GitHub Desktop.
Advent of Code Day 8 - Part 1
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 javafx.util.Pair; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.*; | |
public class Day08 { | |
public static void main(String[] args) { | |
File file = new File("input.txt"); | |
if (!file.exists()) { | |
System.err.println("'input.txt' does not exist"); | |
return; | |
} | |
List<Pair<Main.Instruction, Integer>> data = new ArrayList<>(); | |
try { | |
Scanner scanner = new Scanner(file); | |
while (scanner.hasNextLine()) { | |
String line = scanner.nextLine(); | |
String[] rawData = line.split(" "); | |
data.add(new Pair<>(Main.Instruction.valueOf(rawData[0].toUpperCase()), Integer.valueOf(rawData[1]))); | |
} | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
// part 1 | |
int accumulated = 0; | |
int index = 0; | |
Set<Integer> alreadySeen = new HashSet<>(); | |
// we don't want to re-run w/ same index number | |
while (!alreadySeen.contains(index)) { | |
alreadySeen.add(index); | |
Pair<Main.Instruction, Integer> instructionStep = data.get(index); | |
switch (instructionStep.getKey()) { | |
case NOP: | |
index++; | |
break; | |
case ACC: | |
index++; | |
accumulated += instructionStep.getValue(); | |
break; | |
case JMP: | |
index += instructionStep.getValue(); | |
break; | |
} | |
} | |
System.out.println(accumulated); | |
} | |
enum Instruction { | |
ACC, | |
JMP, | |
NOP; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment