Big update: Adding all advent of codes to same repo
This commit is contained in:
BIN
AdventOfCode2020/day5/day5.class
Normal file
BIN
AdventOfCode2020/day5/day5.class
Normal file
Binary file not shown.
63
AdventOfCode2020/day5/day5.java
Normal file
63
AdventOfCode2020/day5/day5.java
Normal file
@@ -0,0 +1,63 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
class day5 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
File inputs = new File("input.txt");
|
||||
try {
|
||||
|
||||
Scanner in = new Scanner(inputs);
|
||||
String[] input = new String[884];
|
||||
|
||||
for (int i = 0; i < 884; i++) input[i] = in.nextLine();
|
||||
|
||||
int highest = 0;
|
||||
int lowest = 99999999;
|
||||
|
||||
ArrayList<Integer> seatIDs = new ArrayList<Integer>();
|
||||
|
||||
for (String line : input) {
|
||||
|
||||
int row = binaryConvertion(line.substring(0, line.length() - 3), 'B');
|
||||
int collumn = binaryConvertion(line.substring(line.length() - 3), 'R');
|
||||
|
||||
int calculation = row * 8 + collumn;
|
||||
if (calculation > highest) highest = calculation;
|
||||
else if (calculation < lowest) lowest = calculation;
|
||||
|
||||
seatIDs.add(calculation);
|
||||
|
||||
}
|
||||
|
||||
for (int i = lowest; i <= highest; i++) {
|
||||
|
||||
if (seatIDs.contains(i) == false) {
|
||||
System.out.println(i);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static int binaryConvertion(String givenInputs, char one) {
|
||||
int counter = 0;
|
||||
for (int i = givenInputs.length() - 1; i >= 0; i--) {
|
||||
if (givenInputs.charAt(i) == one) {
|
||||
counter += Math.pow(2, givenInputs.length() - i - 1);
|
||||
}
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user