Skip to main content

aoc/year2015/
day10.rs

1//! # Elves Look, Elves Say
2//!
3//! There is a trick to solve this problem in constant time and space. While this is not possible
4//! for any arbitrary sequence, in Advent of Code *we only need to solve for our given input*.
5//!
6//! Examining the input shows that it consists of one of Conway's
7//! [atomic elements](https://en.wikipedia.org/wiki/Look-and-say_sequence#Cosmological_decay).
8//! Each element breaks down into other elements that do not interact with each other. This means
9//! that we only need to track the *count* of each element, rather than deal with the sequence
10//! as a whole. Each step we replace the count of each element with its decay products. For example
11//! if we had five `Ni` then next step we would decay to five `Zn` and five `Co`.
12//!
13//! Computing the result is simply multiplying the number of each element by its length. There are
14//! 92 elements total so we can use a fixed-size array to store the decay chain information.
15//!
16//! It would be possible (but less fun) to precompute all possible 92 answers into a
17//! lookup table.
18use crate::util::hash::*;
19use crate::util::iter::*;
20
21const ELEMENTS: &str = "\
2222 -> H -> H
2313112221133211322112211213322112 -> He -> Hf Pa H Ca Li
24312211322212221121123222112 -> Li -> He
25111312211312113221133211322112211213322112 -> Be -> Ge Ca Li
261321132122211322212221121123222112 -> B -> Be
273113112211322112211213322112 -> C -> B
28111312212221121123222112 -> N -> C
29132112211213322112 -> O -> N
3031121123222112 -> F -> O
31111213322112 -> Ne -> F
32123222112 -> Na -> Ne
333113322112 -> Mg -> Pm Na
341113222112 -> Al -> Mg
351322112 -> Si -> Al
36311311222112 -> P -> Ho Si
371113122112 -> S -> P
38132112 -> Cl -> S
393112 -> Ar -> Cl
401112 -> K -> Ar
4112 -> Ca -> K
423113112221133112 -> Sc -> Ho Pa H Ca Co
4311131221131112 -> Ti -> Sc
4413211312 -> V -> Ti
4531132 -> Cr -> V
46111311222112 -> Mn -> Cr Si
4713122112 -> Fe -> Mn
4832112 -> Co -> Fe
4911133112 -> Ni -> Zn Co
50131112 -> Cu -> Ni
51312 -> Zn -> Cu
5213221133122211332 -> Ga -> Eu Ca Ac H Ca Zn
5331131122211311122113222 -> Ge -> Ho Ga
5411131221131211322113322112 -> As -> Ge Na
5513211321222113222112 -> Se -> As
563113112211322112 -> Br -> Se
5711131221222112 -> Kr -> Br
581321122112 -> Rb -> Kr
593112112 -> Sr -> Rb
601112133 -> Y -> Sr U
6112322211331222113112211 -> Zr -> Y H Ca Tc
621113122113322113111221131221 -> Nb -> Er Zr
6313211322211312113211 -> Mo -> Nb
64311322113212221 -> Tc -> Mo
65132211331222113112211 -> Ru -> Eu Ca Tc
66311311222113111221131221 -> Rh -> Ho Ru
67111312211312113211 -> Pd -> Rh
68132113212221 -> Ag -> Pd
693113112211 -> Cd -> Ag
7011131221 -> In -> Cd
7113211 -> Sn -> In
723112221 -> Sb -> Pm Sn
731322113312211 -> Te -> Eu Ca Sb
74311311222113111221 -> I -> Ho Te
7511131221131211 -> Xe -> I
7613211321 -> Cs -> Xe
77311311 -> Ba -> Cs
7811131 -> La -> Ba
791321133112 -> Ce -> La H Ca Co
8031131112 -> Pr -> Ce
81111312 -> Nd -> Pr
82132 -> Pm -> Nd
83311332 -> Sm -> Pm Ca Zn
841113222 -> Eu -> Sm
8513221133112 -> Gd -> Eu Ca Co
863113112221131112 -> Tb -> Ho Gd
87111312211312 -> Dy -> Tb
881321132 -> Ho -> Dy
89311311222 -> Er -> Ho Pm
9011131221133112 -> Tm -> Er Ca Co
911321131112 -> Yb -> Tm
92311312 -> Lu -> Yb
9311132 -> Hf -> Lu
9413112221133211322112211213322113 -> Ta -> Hf Pa H Ca W
95312211322212221121123222113 -> W -> Ta
96111312211312113221133211322112211213322113 -> Re -> Ge Ca W
971321132122211322212221121123222113 -> Os -> Re
983113112211322112211213322113 -> Ir -> Os
99111312212221121123222113 -> Pt -> Ir
100132112211213322113 -> Au -> Pt
10131121123222113 -> Hg -> Au
102111213322113 -> Tl -> Hg
103123222113 -> Pb -> Tl
1043113322113 -> Bi -> Pm Pb
1051113222113 -> Po -> Bi
1061322113 -> At -> Po
107311311222113 -> Rn -> Ho At
1081113122113 -> Fr -> Rn
109132113 -> Ra -> Fr
1103113 -> Ac -> Ra
1111113 -> Th -> Ac
11213 -> Pa -> Th
1133 -> U -> Pa";
114
115type Result = (usize, usize);
116
117pub fn parse(input: &str) -> Result {
118    let elements: Vec<_> =
119        ELEMENTS.lines().flat_map(|line| line.split(" -> ")).chunk::<3>().collect();
120
121    // Map both sequence and element name to indices.
122    let indices: FastMap<_, _> = elements
123        .iter()
124        .enumerate()
125        .flat_map(|(i, &[sequence, name, _])| [(sequence, i), (name, i)])
126        .collect();
127
128    // Build list of decay chains.
129    let sizes: Vec<_> = elements.iter().map(|[sequence, ..]| sequence.len()).collect();
130    let decays: Vec<_> = elements
131        .iter()
132        .map(|&[.., decay]| decay.split_ascii_whitespace().map(|name| indices[name]).collect())
133        .collect();
134
135    // Each input is a single element.
136    let mut current = [0; 92];
137    current[indices[input.trim()]] = 1;
138
139    for _ in 0..40 {
140        current = step(&current, &decays);
141    }
142    let part1 = length(&current, &sizes);
143
144    for _ in 0..10 {
145        current = step(&current, &decays);
146    }
147    let part2 = length(&current, &sizes);
148
149    (part1, part2)
150}
151
152pub fn part1(input: &Result) -> usize {
153    input.0
154}
155
156pub fn part2(input: &Result) -> usize {
157    input.1
158}
159
160fn step(current: &[usize], decays: &[Vec<usize>]) -> [usize; 92] {
161    let mut next = [0; 92];
162
163    for (&count, decay) in current.iter().zip(decays) {
164        if count > 0 {
165            for &element in decay {
166                next[element] += count;
167            }
168        }
169    }
170
171    next
172}
173
174fn length(current: &[usize], sizes: &[usize]) -> usize {
175    current.iter().zip(sizes).map(|(c, s)| c * s).sum()
176}