Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e6d94df

Browse filesBrowse files
committed
Day 1
1 parent c6cad88 commit e6d94df
Copy full SHA for e6d94df

File tree

3 files changed

+63
-16
lines changed
Filter options

3 files changed

+63
-16
lines changed

‎.github/workflows/ci.yml

Copy file name to clipboardExpand all lines: .github/workflows/ci.yml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
distribution: 'temurin'
1717
java-version: '19'
1818
check-latest: true
19-
- uses: actions/cache@v2
19+
- uses: actions/cache@v3
2020
with:
2121
path: ~/.m2
2222
key: m2-${{ runner.os }}-19-${{ hashFiles('**/pom.xml') }}

‎src/test/java/com/macasaet/Day01.java

Copy file name to clipboard
+48-15Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,76 @@
11
package com.macasaet;
22

3-
import org.junit.jupiter.api.Disabled;
43
import org.junit.jupiter.api.Test;
54

5+
import java.math.BigInteger;
66
import java.util.ArrayList;
7+
import java.util.Collections;
8+
import java.util.Comparator;
9+
import java.util.Iterator;
710
import java.util.List;
811
import java.util.stream.StreamSupport;
912

1013
/**
11-
* --- Day 1: ---
14+
* --- Day 1: Calorie Counting ---
1215
*/
1316
public class Day01 {
1417

15-
/**
16-
*
17-
*
18-
* @return
19-
*/
20-
protected List<String> getInput() {
18+
protected Iterator<String> getInput() {
2119
return StreamSupport
2220
.stream(new LineSpliterator("day-01.txt"),
2321
false)
24-
.collect(ArrayList::new, List::add, List::addAll);
22+
.iterator();
23+
}
24+
25+
protected List<Elf> getElves() {
26+
var calories = new ArrayList<BigInteger>();
27+
final var elves = new ArrayList<Elf>();
28+
for (final var i = getInput(); i.hasNext(); ) {
29+
final var line = i.next();
30+
if (line.isBlank()) {
31+
elves.add(new Elf(Collections.unmodifiableList(calories)));
32+
calories = new ArrayList<>();
33+
} else {
34+
calories.add(new BigInteger(line.strip()));
35+
}
36+
}
37+
if (!calories.isEmpty()) {
38+
elves.add(new Elf(Collections.unmodifiableList(calories)));
39+
}
40+
return Collections.unmodifiableList(elves);
2541
}
2642

27-
@Disabled
2843
@Test
2944
public final void part1() {
30-
final var list = getInput();
45+
final var elves = getElves();
46+
final var elf = elves.stream()
47+
.max(Comparator.comparing(Elf::totalCaloriesCarried))
48+
.get();
3149

32-
System.out.println("Part 1: " + null);
50+
System.out.println("Part 1: " + elf.totalCaloriesCarried());
3351
}
3452

35-
@Disabled
3653
@Test
3754
public final void part2() {
38-
final var list = getInput();
55+
final var elves = getElves();
56+
final var list = elves.stream()
57+
.sorted(Comparator.comparing(Elf::totalCaloriesCarried).reversed())
58+
.toList();
3959

40-
System.out.println("Part 2: " + null);
60+
System.out.println("Part 2: " + (list.get(0).totalCaloriesCarried().add(list.get(1).totalCaloriesCarried()).add(list.get(2).totalCaloriesCarried())));
61+
}
62+
63+
/**
64+
* An elf who collects food for the reindeer.
65+
*
66+
* @param itemCalories The number of calories of each item carried by the elf
67+
*/
68+
public record Elf(List<BigInteger> itemCalories) {
69+
public BigInteger totalCaloriesCarried() {
70+
return itemCalories().stream()
71+
.reduce(BigInteger::add)
72+
.get();
73+
}
4174
}
4275

4376
}

‎src/test/resources/sample/day-01.txt

Copy file name to clipboard
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1000
2+
2000
3+
3000
4+
5+
4000
6+
7+
5000
8+
6000
9+
10+
7000
11+
8000
12+
9000
13+
14+
10000

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.