1
1
package com .macasaet ;
2
2
3
- import org .junit .jupiter .api .Disabled ;
4
3
import org .junit .jupiter .api .Test ;
5
4
5
+ import java .math .BigInteger ;
6
6
import java .util .ArrayList ;
7
+ import java .util .Collections ;
8
+ import java .util .Comparator ;
9
+ import java .util .Iterator ;
7
10
import java .util .List ;
8
11
import java .util .stream .StreamSupport ;
9
12
10
13
/**
11
- * --- Day 1: ---
14
+ * --- Day 1: Calorie Counting ---
12
15
*/
13
16
public class Day01 {
14
17
15
- /**
16
- *
17
- *
18
- * @return
19
- */
20
- protected List <String > getInput () {
18
+ protected Iterator <String > getInput () {
21
19
return StreamSupport
22
20
.stream (new LineSpliterator ("day-01.txt" ),
23
21
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 );
25
41
}
26
42
27
- @ Disabled
28
43
@ Test
29
44
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 ();
31
49
32
- System .out .println ("Part 1: " + null );
50
+ System .out .println ("Part 1: " + elf . totalCaloriesCarried () );
33
51
}
34
52
35
- @ Disabled
36
53
@ Test
37
54
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 ();
39
59
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
+ }
41
74
}
42
75
43
76
}
0 commit comments