2022 Day 6
Changes to be committed: new file: 2022/day06/Day06A.class new file: 2022/day06/Day06A.java new file: 2022/day06/day06_algo.txt
This commit is contained in:
parent
50165e756d
commit
f5f2f6e64a
3 changed files with 52 additions and 0 deletions
BIN
2022/day06/Day06A.class
Normal file
BIN
2022/day06/Day06A.class
Normal file
Binary file not shown.
29
2022/day06/Day06A.java
Normal file
29
2022/day06/Day06A.java
Normal file
|
@ -0,0 +1,29 @@
|
|||
import java.util.Scanner;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Day06A {
|
||||
public static void main (String[] args) {
|
||||
try (Scanner inputFile = new Scanner(Paths.get("input.txt"))) {
|
||||
char[] signal = inputFile.nextLine().toCharArray();
|
||||
for (int i = 0; i + 3 < signal.length; i++) {
|
||||
if (distinctValues(Arrays.copyOfRange(signal, i, i + 4))) {
|
||||
System.out.printf("Characters processed: %d%n", i + 4);
|
||||
System.exit(0);
|
||||
}
|
||||
}
|
||||
} catch (IOException e ) {}
|
||||
}
|
||||
|
||||
public static boolean distinctValues(char[] arr){
|
||||
for (int i = 0; i < arr.length-1; i++) {
|
||||
for (int j = i + 1; j < arr.length; j++) {
|
||||
if (arr[i] == arr[j]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
23
2022/day06/day06_algo.txt
Normal file
23
2022/day06/day06_algo.txt
Normal file
|
@ -0,0 +1,23 @@
|
|||
=== Main ===
|
||||
Open file
|
||||
initialize counter p
|
||||
Read full string into a char array. BEEG array
|
||||
Loop - for i = 0, i + 3 < array length, i++
|
||||
feed rolling 4-char snapshot into check function index range i to i+3
|
||||
if check true (no duplicates)
|
||||
print p to screen
|
||||
if check false (has duplicates)
|
||||
increment p
|
||||
End Loop
|
||||
|
||||
=== Check function === Reads char array
|
||||
Outer Loop - for i = 0, i < array length - 1, i++
|
||||
Inner Loop - for j = i+1, j < array length, j++
|
||||
if array[i] == array[j]
|
||||
return false
|
||||
End Inner Loop
|
||||
End Outer Loop
|
||||
return true
|
||||
|
||||
For Part B, change i+4 in lines 11 and 12 to i+14 (or size of range needed)
|
||||
|
Loading…
Reference in a new issue