-
Notifications
You must be signed in to change notification settings - Fork 0
/
PartOne.java
41 lines (36 loc) · 1.23 KB
/
PartOne.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
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
/**
* --- Day 1: Historian Hysteria ---
*
*
* https://adventofcode.com/2024/day/1
**/
public class PartOne {
public static void main(String[] args) throws Exception {
Path path = Path.of("2024/days/01/src/input.txt");
List<Integer> leftList = new ArrayList<>();
List<Integer> rightList = new ArrayList<>();
int difference = 0;
try (var lines = Files.lines(path)) {
lines.forEach(line -> {
String[] parts = line.split("\\s+");
int leftNumber = Integer.parseInt(parts[0]);
int rightNumber = Integer.parseInt(parts[1]);
leftList.add(leftNumber);
rightList.add(rightNumber);
});
}
leftList.sort(Integer::compareTo);
rightList.sort(Integer::compareTo);
int listSize = leftList.size();
for (int index = 0; index < listSize; index++) {
int leftNumber = leftList.get(index);
int rightNumber = rightList.get(index);
difference += Math.abs(leftNumber - rightNumber);
}
System.out.println(difference);
}
}