mirror of
https://github.com/Zormm/Advent-Of-Code-2022.git
synced 2026-02-26 07:26:51 +01:00
Added the Entrys for the Days 1 - 7
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import java.io.IOException;
|
||||
import java.io.RandomAccessFile;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
try {
|
||||
RandomAccessFile file = new RandomAccessFile("input.txt", "r");
|
||||
String str;
|
||||
|
||||
while ((str = file.readLine()) != null) {
|
||||
//Part 1
|
||||
System.out.print("Part 1: ");
|
||||
System.out.println(findStartOfSequence(str,4));
|
||||
|
||||
//Part 2
|
||||
System.out.print("Part 2: ");
|
||||
System.out.println(findStartOfSequence(str,14));
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static int findStartOfSequence(String input, int lengthOfSequence) {
|
||||
char[] sequence = new char[lengthOfSequence];
|
||||
boolean matchFound = false;
|
||||
|
||||
for (int i = lengthOfSequence; i <= input.length(); i++) {
|
||||
matchFound = false;
|
||||
for (int j = 0; j < lengthOfSequence; j++) {
|
||||
sequence[j] = input.charAt(i-lengthOfSequence+j);
|
||||
}
|
||||
|
||||
for (int j = 0; j < lengthOfSequence; j++) {
|
||||
for (int k = 0; k < lengthOfSequence; k++) {
|
||||
if (j == k) {
|
||||
continue;
|
||||
}
|
||||
if (sequence[j] == sequence[k]) {
|
||||
matchFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!matchFound) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user