1type Input = Vec<Vec<usize>>;
13
14pub fn parse(input: &str) -> Input {
16 let mut graph = vec![vec![]; 26 * 26 * 26];
17
18 for line in input.lines() {
19 let mut edges = line.split_ascii_whitespace();
20 let from = edges.next().unwrap();
21 graph[to_index(from)].extend(edges.map(to_index));
22 }
23
24 graph
25}
26
27pub fn part1(input: &Input) -> u64 {
28 paths(input, "you", "out")
29}
30
31pub fn part2(input: &Input) -> u64 {
35 let one = paths(input, "svr", "fft") * paths(input, "fft", "dac") * paths(input, "dac", "out");
36 let two = paths(input, "svr", "dac") * paths(input, "dac", "fft") * paths(input, "fft", "out");
37 one + two
38}
39
40fn paths(input: &Input, from: &str, to: &str) -> u64 {
41 let mut cache = vec![u64::MAX; input.len()];
42 dfs(input, &mut cache, to_index(from), to_index(to))
43}
44
45fn dfs(input: &Input, cache: &mut [u64], node: usize, end: usize) -> u64 {
46 if node == end {
47 1
48 } else if cache[node] == u64::MAX {
49 let result = input[node].iter().map(|&next| dfs(input, cache, next, end)).sum();
50 cache[node] = result;
51 result
52 } else {
53 cache[node]
54 }
55}
56
57fn to_index(s: &str) -> usize {
59 s.bytes().take(3).fold(0, |acc, b| 26 * acc + usize::from(b - b'a'))
60}