1
+ package com .macasaet ;
2
+
3
+ import java .util .ArrayList ;
4
+ import java .util .Arrays ;
5
+ import java .util .List ;
6
+ import java .util .Optional ;
7
+ import java .util .stream .Collectors ;
8
+ import java .util .stream .Stream ;
9
+ import java .util .stream .StreamSupport ;
10
+
11
+ import org .junit .jupiter .api .Test ;
12
+
13
+ /**
14
+ * --- Day 6: Lanternfish ---
15
+ */
16
+ public class Day06 {
17
+
18
+ /**
19
+ * A glowing fish that spawns very quickly. Their population grows exponentially.
20
+ */
21
+ public static class Lanternfish {
22
+
23
+ private int daysToSpawn ;
24
+
25
+ /**
26
+ * @param daysToSpawn the number of days until it creates a new {@link Lanternfish}
27
+ */
28
+ public Lanternfish (final int daysToSpawn ) {
29
+ setDaysToSpawn (daysToSpawn );
30
+ }
31
+
32
+ /**
33
+ * Simulate the passage of one day
34
+ *
35
+ * @return either a new Lanternfish or nothing depending on whether the fish spawned
36
+ */
37
+ public Optional <Lanternfish > tick () {
38
+ final var timer = getDaysToSpawn () - 1 ;
39
+ if (timer < 0 ) {
40
+ setDaysToSpawn (6 );
41
+ return Optional .of (new Lanternfish (8 ));
42
+ } else {
43
+ setDaysToSpawn (timer );
44
+ return Optional .empty ();
45
+ }
46
+ }
47
+
48
+ /**
49
+ * @return the number of days until the fish spawns
50
+ */
51
+ public int getDaysToSpawn () {
52
+ return this .daysToSpawn ;
53
+ }
54
+
55
+ /**
56
+ * Update this fish's days to spawn
57
+ *
58
+ * @param daysToSpawn the number of days until the fish spawns, must be non-negative
59
+ */
60
+ protected void setDaysToSpawn (final int daysToSpawn ) {
61
+ if (daysToSpawn < 0 ) {
62
+ throw new IllegalArgumentException ("\" days to spawn\" must be non-negative" );
63
+ }
64
+ this .daysToSpawn = daysToSpawn ;
65
+ }
66
+ }
67
+
68
+ protected Stream <String > getInput () {
69
+ return StreamSupport
70
+ .stream (new LineSpliterator ("day-06.txt" ),
71
+ false );
72
+ }
73
+
74
+ public List <Lanternfish > parseInput () {
75
+ final var list = getInput ().toList ();
76
+ final var line = list .get (0 );
77
+ final var components = line .split ("," );
78
+ return Arrays .stream (components )
79
+ .mapToInt (Integer ::parseInt )
80
+ .mapToObj (Lanternfish ::new )
81
+ .collect (Collectors .toList ());
82
+ }
83
+
84
+ @ Test
85
+ public final void part1 () {
86
+ var population = parseInput ();
87
+ for (int _i = 80 ; --_i >= 0 ; ) {
88
+ final List <Lanternfish > list = new ArrayList <>(population );
89
+ for (final var fish : population ) {
90
+ final var result = fish .tick ();
91
+ result .ifPresent (list ::add );
92
+ }
93
+ population = list ;
94
+ }
95
+ System .out .println ("Part 1: " + population .size ());
96
+ }
97
+
98
+ @ Test
99
+ public final void part2 () {
100
+ final var initial = parseInput ();
101
+ var map = new long [9 ];
102
+ for (final var fish : initial ) {
103
+ map [fish .getDaysToSpawn ()]++;
104
+ }
105
+ for (int _i = 256 ; --_i >= 0 ; ) {
106
+ final var temp = new long [map .length ];
107
+ for (int daysToSpawn = map .length ; --daysToSpawn >= 0 ; ) {
108
+ final var count = map [daysToSpawn ];
109
+ final var prototype = new Lanternfish (daysToSpawn );
110
+ final var result = prototype .tick ();
111
+ temp [prototype .getDaysToSpawn ()] += count ;
112
+ result .ifPresent (spawn -> temp [spawn .getDaysToSpawn ()] = temp [spawn .getDaysToSpawn ()] + count );
113
+ }
114
+ map = temp ;
115
+ }
116
+ final var result = Arrays .stream (map ).reduce (0L , Long ::sum );
117
+ System .out .println ("Part 2: " + result );
118
+ }
119
+
120
+ }
0 commit comments