1
+ import static com .github .pareronia .aoc .IterTools .enumerate ;
2
+ import static com .github .pareronia .aoc .StringOps .splitLines ;
3
+ import static java .util .stream .Collectors .toMap ;
4
+
5
+ import java .util .Arrays ;
1
6
import java .util .List ;
7
+ import java .util .Map ;
2
8
9
+ import com .github .pareronia .aoc .IterTools .Enumerated ;
3
10
import com .github .pareronia .aoc .solution .SolutionBase ;
4
11
5
- public class AoC2015_10 extends SolutionBase <String , Integer , Integer > {
12
+ public class AoC2015_10 extends SolutionBase <AoC2015_10 . Input , Integer , Integer > {
6
13
7
14
private AoC2015_10 (final boolean debug ) {
8
15
super (debug );
@@ -16,49 +23,154 @@ public static final AoC2015_10 createDebug() {
16
23
return new AoC2015_10 (true );
17
24
}
18
25
19
- private String lookAndSay (final String string ) {
20
- final StringBuilder result = new StringBuilder ();
21
- int i = 0 ;
22
- while (i < string .length ()) {
23
- final char digit = string .charAt (i );
24
- int j = 0 ;
25
- while (i + j < string .length () && string .charAt (i + j ) == digit ) {
26
- j ++;
27
- }
28
- result .append (j ).append (digit );
29
- i += j ;
30
- }
31
- return result .toString ();
32
- }
33
-
34
26
@ Override
35
- protected String parseInput (final List <String > inputs ) {
36
- assert inputs .size () == 1 ;
37
- return inputs .get (0 );
27
+ protected Input parseInput (final List <String > inputs ) {
28
+ final List <String []> elements = splitLines (ELEMENTS ).stream ()
29
+ .map (s -> s .split (" " ))
30
+ .toList ();
31
+ final Map <String , Integer > indices = enumerate (elements .stream ())
32
+ .collect (toMap (e -> e .value ()[2 ], Enumerated ::index ));
33
+ final String [] sequence = new String [N ];
34
+ final int [][] decays = new int [N ][];
35
+ enumerate (elements .stream ()).forEach (e -> {
36
+ sequence [e .index ()] = e .value ()[0 ];
37
+ decays [e .index ()] = Arrays .stream (e .value ())
38
+ .skip (4 ).mapToInt (indices ::get ).toArray ();
39
+ });
40
+ return new Input (Arrays .asList (sequence ), decays , inputs .get (0 ));
38
41
}
39
42
40
- private String solve (final String input , final int iterations ) {
41
- String string = input ;
43
+ private int solve (final Input input , final int iterations ) {
44
+ int [] current = new int [N ];
45
+ current [input .sequence .indexOf (input .start )] = 1 ;
42
46
for (int i = 0 ; i < iterations ; i ++) {
43
- string = lookAndSay (string );
44
- log (i + ": " + string .length ());
47
+ final int [] nxt = new int [N ];
48
+ for (int j = 0 ; j < N ; j ++) {
49
+ final int c = current [j ];
50
+ if (c > 0 ) {
51
+ for (int k = 0 ; k < input .decays [j ].length ; k ++) {
52
+ nxt [input .decays [j ][k ]] += c ;
53
+ }
54
+ }
55
+ }
56
+ current = nxt ;
57
+ }
58
+ int ans = 0 ;
59
+ for (int i = 0 ; i < N ; i ++) {
60
+ ans += current [i ] * input .sequence .get (i ).length ();
45
61
}
46
- return string ;
62
+ return ans ;
47
63
}
48
64
49
65
@ Override
50
- public Integer solvePart1 (final String input ) {
51
- return solve (input , 40 ). length () ;
66
+ public Integer solvePart1 (final Input input ) {
67
+ return solve (input , 40 );
52
68
}
53
69
54
70
@ Override
55
- public Integer solvePart2 (final String input ) {
56
- return solve (input , 50 ). length () ;
71
+ public Integer solvePart2 (final Input input ) {
72
+ return solve (input , 50 );
57
73
}
58
74
59
75
public static void main (final String [] args ) throws Exception {
60
- assert AoC2015_10 .createDebug ().solve ("1" , 5 ).equals ("312211" );
61
-
62
76
AoC2015_10 .create ().run ();
63
77
}
78
+
79
+ record Input (List <String > sequence , int [][] decays , String start ) {}
80
+
81
+ private final int N = 92 ;
82
+ private final String ELEMENTS = """
83
+ 22 -> H -> H
84
+ 13112221133211322112211213322112 -> He -> Hf Pa H Ca Li
85
+ 312211322212221121123222112 -> Li -> He
86
+ 111312211312113221133211322112211213322112 -> Be -> Ge Ca Li
87
+ 1321132122211322212221121123222112 -> B -> Be
88
+ 3113112211322112211213322112 -> C -> B
89
+ 111312212221121123222112 -> N -> C
90
+ 132112211213322112 -> O -> N
91
+ 31121123222112 -> F -> O
92
+ 111213322112 -> Ne -> F
93
+ 123222112 -> Na -> Ne
94
+ 3113322112 -> Mg -> Pm Na
95
+ 1113222112 -> Al -> Mg
96
+ 1322112 -> Si -> Al
97
+ 311311222112 -> P -> Ho Si
98
+ 1113122112 -> S -> P
99
+ 132112 -> Cl -> S
100
+ 3112 -> Ar -> Cl
101
+ 1112 -> K -> Ar
102
+ 12 -> Ca -> K
103
+ 3113112221133112 -> Sc -> Ho Pa H Ca Co
104
+ 11131221131112 -> Ti -> Sc
105
+ 13211312 -> V -> Ti
106
+ 31132 -> Cr -> V
107
+ 111311222112 -> Mn -> Cr Si
108
+ 13122112 -> Fe -> Mn
109
+ 32112 -> Co -> Fe
110
+ 11133112 -> Ni -> Zn Co
111
+ 131112 -> Cu -> Ni
112
+ 312 -> Zn -> Cu
113
+ 13221133122211332 -> Ga -> Eu Ca Ac H Ca Zn
114
+ 31131122211311122113222 -> Ge -> Ho Ga
115
+ 11131221131211322113322112 -> As -> Ge Na
116
+ 13211321222113222112 -> Se -> As
117
+ 3113112211322112 -> Br -> Se
118
+ 11131221222112 -> Kr -> Br
119
+ 1321122112 -> Rb -> Kr
120
+ 3112112 -> Sr -> Rb
121
+ 1112133 -> Y -> Sr U
122
+ 12322211331222113112211 -> Zr -> Y H Ca Tc
123
+ 1113122113322113111221131221 -> Nb -> Er Zr
124
+ 13211322211312113211 -> Mo -> Nb
125
+ 311322113212221 -> Tc -> Mo
126
+ 132211331222113112211 -> Ru -> Eu Ca Tc
127
+ 311311222113111221131221 -> Rh -> Ho Ru
128
+ 111312211312113211 -> Pd -> Rh
129
+ 132113212221 -> Ag -> Pd
130
+ 3113112211 -> Cd -> Ag
131
+ 11131221 -> In -> Cd
132
+ 13211 -> Sn -> In
133
+ 3112221 -> Sb -> Pm Sn
134
+ 1322113312211 -> Te -> Eu Ca Sb
135
+ 311311222113111221 -> I -> Ho Te
136
+ 11131221131211 -> Xe -> I
137
+ 13211321 -> Cs -> Xe
138
+ 311311 -> Ba -> Cs
139
+ 11131 -> La -> Ba
140
+ 1321133112 -> Ce -> La H Ca Co
141
+ 31131112 -> Pr -> Ce
142
+ 111312 -> Nd -> Pr
143
+ 132 -> Pm -> Nd
144
+ 311332 -> Sm -> Pm Ca Zn
145
+ 1113222 -> Eu -> Sm
146
+ 13221133112 -> Gd -> Eu Ca Co
147
+ 3113112221131112 -> Tb -> Ho Gd
148
+ 111312211312 -> Dy -> Tb
149
+ 1321132 -> Ho -> Dy
150
+ 311311222 -> Er -> Ho Pm
151
+ 11131221133112 -> Tm -> Er Ca Co
152
+ 1321131112 -> Yb -> Tm
153
+ 311312 -> Lu -> Yb
154
+ 11132 -> Hf -> Lu
155
+ 13112221133211322112211213322113 -> Ta -> Hf Pa H Ca W
156
+ 312211322212221121123222113 -> W -> Ta
157
+ 111312211312113221133211322112211213322113 -> Re -> Ge Ca W
158
+ 1321132122211322212221121123222113 -> Os -> Re
159
+ 3113112211322112211213322113 -> Ir -> Os
160
+ 111312212221121123222113 -> Pt -> Ir
161
+ 132112211213322113 -> Au -> Pt
162
+ 31121123222113 -> Hg -> Au
163
+ 111213322113 -> Tl -> Hg
164
+ 123222113 -> Pb -> Tl
165
+ 3113322113 -> Bi -> Pm Pb
166
+ 1113222113 -> Po -> Bi
167
+ 1322113 -> At -> Po
168
+ 311311222113 -> Rn -> Ho At
169
+ 1113122113 -> Fr -> Rn
170
+ 132113 -> Ra -> Fr
171
+ 3113 -> Ac -> Ra
172
+ 1113 -> Th -> Ac
173
+ 13 -> Pa -> Th
174
+ 3 -> U -> Pa
175
+ """ ;
64
176
}
0 commit comments