Skip to content

Commit dcecd1a

Browse files
committed
2024 Day 2 part 1 start
1 parent d30af06 commit dcecd1a

File tree

4 files changed

+98
-7
lines changed

4 files changed

+98
-7
lines changed

Diff for: src/main/java/net/leibi/adventofcode2024/day1/Day1.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Integer getSumOfDistances(List<Integer> left, List<Integer> right) {
3030
left.sort(Integer::compareTo);
3131
right.sort(Integer::compareTo);
3232

33-
Integer sum = 0;
33+
int sum = 0;
3434
for (var i = 0; i < left.size(); i++) {
3535
sum += Math.abs(left.get(i) - right.get(i));
3636
}
@@ -41,19 +41,28 @@ public Integer getSumOfDistances(List<Integer> left, List<Integer> right) {
4141
public long part2(String input) {
4242

4343
var lists = getListOfListsFromInput(input);
44-
var scores = getSimilarityScores(lists.get(0), lists.get(1));
44+
int[] rightArray = lists.get(1).stream().mapToInt(Integer::intValue).toArray();
45+
var scores = getSimilarityScores(lists.get(0), rightArray);
4546

4647
return scores.stream().mapToLong(i -> i).sum();
4748
}
4849

49-
private List<Long> getSimilarityScores(List<Integer> left, List<Integer> right) {
50+
private List<Long> getSimilarityScores(List<Integer> left, int[] right) {
5051

5152
var scores = new ArrayList<Long>();
52-
for (var i : left) {
53-
final var count = right.stream().filter(v -> v.equals(i)).count();
54-
var score = count * i;
55-
scores.add(score);
53+
54+
55+
for (var currentNumber : left) {
56+
scores.add(getCountOfOccurancesOfNumberInList(right, currentNumber) * currentNumber);
5657
}
5758
return scores;
5859
}
60+
61+
private static long getCountOfOccurancesOfNumberInList(int[] right, int i) {
62+
long cnt = 0;
63+
for (int integer : right) {
64+
if (i == integer) cnt++;
65+
}
66+
return cnt;
67+
}
5968
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.leibi.adventofcode2024.day2;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
6+
public class Day2 {
7+
public long part1(String input) {
8+
final var list = input.lines().map(s -> s.split("\\s+")).toList();
9+
return list.stream().filter(a -> isSafe(Arrays.asList(a))).count();
10+
}
11+
12+
boolean isSafe(List<Integer> report) {
13+
14+
int minSafe = 1;
15+
int maxSafe = 3;
16+
17+
Direction direction = report.get(1) - report.get(0) > 0 ? Direction.INC : Direction.DEC;
18+
for (var i = 1; i < report.size(); i++) {
19+
var diff = report.get(i) - report.get(i - 1);
20+
if (Math.abs(diff) > maxSafe || Math.abs(diff) < minSafe)
21+
return false;
22+
23+
Direction localDir = diff > 0 ? Direction.INC : Direction.DEC;
24+
if (localDir != direction)
25+
return false;
26+
}
27+
return true;
28+
}
29+
30+
enum Direction {
31+
INC,
32+
DEC,
33+
BOTH
34+
}
35+
36+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package net.leibi.adventofcode2024.day2;
2+
3+
public class Input {
4+
5+
public static final String SMALL = """
6+
7 6 4 2 1
7+
1 2 7 8 9
8+
9 7 6 2 1
9+
1 3 2 4 5
10+
8 6 4 4 1
11+
1 3 6 7 9
12+
""";
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package net.leibi.adventofcode2024.day2;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static org.assertj.core.api.Assertions.assertThat;
8+
9+
class Day2Test {
10+
11+
12+
private final Day2 day2 = new Day2();
13+
14+
@Test
15+
void part1Small(){
16+
assertThat((day2.part1(Input.SMALL))).isEqualTo(2L);
17+
}
18+
19+
20+
@Test
21+
void isSafe(){
22+
23+
assertThat(day2.isSafe(List.of(7,6,4,2,1))).isTrue();
24+
assertThat(day2.isSafe(List.of(1,2,7,8,9))).isFalse();
25+
assertThat(day2.isSafe(List.of(9,7,6,2,1))).isFalse();
26+
assertThat(day2.isSafe(List.of(1,3,2,4,5))).isFalse();
27+
assertThat(day2.isSafe(List.of(8,6,4,4,1))).isFalse();
28+
assertThat(day2.isSafe(List.of(1,3,6,7,9))).isTrue();
29+
30+
31+
}
32+
33+
}

0 commit comments

Comments
 (0)