3
3
import java .util .ArrayList ;
4
4
import java .util .HashMap ;
5
5
import java .util .List ;
6
- import java .util .Map ;
7
6
import java .util .stream .IntStream ;
8
7
9
8
import org .apache .commons .lang3 .StringUtils ;
10
9
11
10
import com .github .pareronia .aocd .Aocd ;
12
11
import com .github .pareronia .aocd .Puzzle ;
13
12
14
- import lombok .Builder ;
15
- import lombok .RequiredArgsConstructor ;
16
- import lombok .Singular ;
17
- import lombok .ToString ;
18
-
19
13
public class AoC2020_14 extends AoCBase {
20
14
21
15
private final List <Group > groups ;
22
16
23
17
private AoC2020_14 (final List <String > input , final boolean debug ) {
24
18
super (debug );
25
19
this .groups = new ArrayList <>();
26
- Group . GroupBuilder groupBuilder = Group .builder ();
20
+ var groupBuilder = Group .builder ();
27
21
groupBuilder .mask (input .get (0 ).substring ("mask = " .length ()));
28
22
for (int i = 1 ; i <= input .size (); i ++) {
29
23
if (i == input .size ()) {
@@ -33,7 +27,7 @@ private AoC2020_14(final List<String> input, final boolean debug) {
33
27
groupBuilder = Group .builder ();
34
28
groupBuilder .mask (input .get (i ).substring ("mask = " .length ()));
35
29
} else {
36
- final String [] splits = input .get (i ).split ("] = " );
30
+ final var splits = input .get (i ).split ("] = " );
37
31
final int address = Integer .parseInt (splits [0 ].substring ("mem[" .length ()));
38
32
final int value = Integer .parseInt (splits [1 ]);
39
33
groupBuilder .write (new Write (address , value ));
@@ -89,10 +83,10 @@ private Result applyMask2(final long address, final long value, final String mas
89
83
}
90
84
91
85
private Long solve (final Strategy strategy ) {
92
- final Map < String , Long > memory = new HashMap <>();
93
- for (final Group group : this .groups ) {
94
- for (final Write write : group .writes ) {
95
- final Result result = strategy .execute (write .address , write .value , group .mask );
86
+ final var memory = new HashMap <String , Long >();
87
+ for (final var group : this .groups ) {
88
+ for (final var write : group .writes ) {
89
+ final var result = strategy .execute (write .address , write .value , group .mask );
96
90
for (final String address : result .addresses ) {
97
91
memory .put (address , result .value );
98
92
}
@@ -123,40 +117,46 @@ public static void main(final String[] args) throws Exception {
123
117
);
124
118
}
125
119
126
- private static final List <String > TEST1 = splitLines (
127
- " mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X\r \n " +
128
- " mem[8] = 11\r \n " +
129
- " mem[7] = 101\r \n " +
130
- " mem[8] = 0"
131
- );
132
- private static final List <String > TEST2 = splitLines (
133
- " mask = 000000000000000000000000000000X1001X\r \n " +
134
- " mem[42] = 100\r \n " +
135
- " mask = 00000000000000000000000000000000X0XX\r \n " +
136
- " mem[26] = 1"
137
- );
120
+ private static final List <String > TEST1 = splitLines ("""
121
+ mask = XXXXXXXXXXXXXXXXXXXXXXXXXXXXX1XXXX0X
122
+ mem[8] = 11
123
+ mem[7] = 101
124
+ mem[8] = 0
125
+ """ );
126
+ private static final List <String > TEST2 = splitLines ("""
127
+ mask = 000000000000000000000000000000X1001X
128
+ mem[42] = 100
129
+ mask = 00000000000000000000000000000000X0XX
130
+ mem[26] = 1
131
+ """ );
138
132
139
- @ RequiredArgsConstructor
140
- @ ToString
141
- private static final class Write {
142
- private final int address ;
143
- private final int value ;
144
- }
133
+ private static final record Write (int address , int value ) { }
145
134
146
- @ RequiredArgsConstructor
147
- @ Builder
148
- @ ToString
149
- private static final class Group {
150
- private final String mask ;
151
- @ Singular
152
- private final List <Write > writes ;
135
+ private static final record Group (String mask , List <Write > writes ) {
136
+ public static GroupBuilder builder () {
137
+ return new GroupBuilder ();
138
+ }
139
+ private static final class GroupBuilder {
140
+ private String mask ;
141
+ private final List <Write > writes = new ArrayList <>();
142
+
143
+ public Group build () {
144
+ return new Group (this .mask , this .writes );
145
+ }
146
+
147
+ public GroupBuilder mask (final String mask ) {
148
+ this .mask = mask ;
149
+ return this ;
150
+ }
151
+
152
+ public GroupBuilder write (final Write write ) {
153
+ this .writes .add (write );
154
+ return this ;
155
+ }
156
+ }
153
157
}
154
158
155
- @ RequiredArgsConstructor
156
- private static final class Result {
157
- private final List <String > addresses ;
158
- private final long value ;
159
- }
159
+ private static final record Result (List <String > addresses , long value ) { }
160
160
161
161
@ FunctionalInterface
162
162
private interface Strategy {
0 commit comments