Skip to content

Commit 646bc2f

Browse files
committed
feat: solve day 1
1 parent 1b1f944 commit 646bc2f

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.adventofcode.flashk.day01;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
public class HistorianHysteria {
9+
10+
private List<Long> left = new ArrayList<>();
11+
private List<Long> right = new ArrayList<>();
12+
13+
public HistorianHysteria(List<String> input) {
14+
for(String line : input) {
15+
String[] split = line.split(" ");
16+
left.add(Long.valueOf(split[0]));
17+
right.add(Long.valueOf(split[1]));
18+
}
19+
}
20+
21+
public Long solveA() {
22+
List<Long> leftSorted = left.stream().sorted().toList();
23+
List<Long> rightSorted = right.stream().sorted().toList();
24+
25+
Long result = 0L;
26+
27+
for(int i = 0; i < leftSorted.size(); i++) {
28+
result += Math.abs(leftSorted.get(i)-rightSorted.get(i));
29+
}
30+
31+
return result;
32+
}
33+
34+
public Long solveB() {
35+
36+
Long result = 0L;
37+
Map<Long,Long> numberOccurrences = new HashMap<>();
38+
39+
for(Long number : left) {
40+
Long occurrences = 0L;
41+
if(numberOccurrences.containsKey(number)) {
42+
occurrences = numberOccurrences.get(number);
43+
result += number * numberOccurrences.get(number);
44+
} else {
45+
46+
for(Long numberRight : right) {
47+
if(number.equals(numberRight)) {
48+
occurrences++;
49+
}
50+
}
51+
52+
numberOccurrences.put(number, occurrences);
53+
}
54+
result += number * occurrences;
55+
56+
}
57+
58+
return result;
59+
}
60+
61+
62+
}

src/test/java/com/adventofcode/flashk/day01/Day01Test.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import com.adventofcode.flashk.common.test.utils.Timer;
2020
import com.adventofcode.flashk.common.test.utils.Input;
2121

22+
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
2224
@DisplayName(TestDisplayName.DAY_01)
2325
@TestMethodOrder(OrderAnnotation.class)
2426
public class Day01Test extends PuzzleTest {
@@ -42,7 +44,10 @@ public void testSolvePart1Sample() {
4244

4345
// Read input file
4446
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
45-
47+
48+
HistorianHysteria historianHysteria = new HistorianHysteria(inputs);
49+
50+
assertEquals(11, historianHysteria.solveA());
4651
}
4752

4853
@Test
@@ -56,7 +61,10 @@ public void testSolvePart1Input() {
5661

5762
// Read input file
5863
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
59-
64+
HistorianHysteria historianHysteria = new HistorianHysteria(inputs);
65+
66+
assertEquals(3574690, historianHysteria.solveA());
67+
6068
}
6169

6270
@Test
@@ -70,7 +78,10 @@ public void testSolvePart2Sample() {
7078

7179
// Read input file
7280
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE_SAMPLE);
73-
81+
82+
HistorianHysteria historianHysteria = new HistorianHysteria(inputs);
83+
84+
assertEquals(31, historianHysteria.solveB());
7485
}
7586

7687
@Test
@@ -84,6 +95,10 @@ public void testSolvePart2Input() {
8495

8596
// Read input file
8697
List<String> inputs = Input.readStringLines(INPUT_FOLDER, TestFilename.INPUT_FILE);
98+
99+
HistorianHysteria historianHysteria = new HistorianHysteria(inputs);
100+
101+
assertEquals(22565391, historianHysteria.solveB());
87102

88103
}
89104

0 commit comments

Comments
 (0)