-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartTwo.java
75 lines (62 loc) · 2.04 KB
/
PartTwo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import java.nio.file.Files;
import java.nio.file.Path;
/**
* --- Day 2: Red-Nosed Reports --- --- Part Two ---
*
*
* https://adventofcode.com/2024/day/2
**/
public class PartTwo {
static void main(String[] args) throws Exception {
Path path = Path.of("2024/days/02/src/input.txt");
int amountOfSafeValues = 0;
try (var lines = Files.lines(path)) {
for (String line : lines.toList()) {
String[] numbers = line.split("\\s+");
if (isSafe(numbers)) {
amountOfSafeValues++;
}
}
}
System.out.println(amountOfSafeValues);
}
private static boolean isSafe(String[] numbers) {
int size = numbers.length;
int[] nums = new int[size];
for (int i = 0; i < size; i++) {
nums[i] = Integer.parseInt(numbers[i]);
}
for (int removeIndex = 0; removeIndex < nums.length; removeIndex++) {
if (isValidSequence(nums, removeIndex)) {
return true;
}
}
return false;
}
private static boolean isValidSequence(int[] nums, int removeIndex) {
boolean isAscending = true, isDescending = true;
int size = nums.length;
int[] newNums = new int[size - 1];
for (int index = 0, newIndex = 0; index < size; index++) {
if (index != removeIndex) {
newNums[newIndex++] = nums[index];
}
}
nums = newNums;
for (int index = 0; index < nums.length - 1; index++) {
int currentNumber = nums[index];
int nextNumber = nums[index + 1];
int difference = Math.abs(currentNumber - nextNumber);
if (currentNumber >= nextNumber) {
isAscending = false;
}
if (currentNumber <= nextNumber) {
isDescending = false;
}
if (difference > 3) {
return false;
}
}
return isAscending || isDescending;
}
}