|
| 1 | +//! # Keypad Conundrum |
| 2 | +use crate::util::hash::*; |
| 3 | +use crate::util::parse::*; |
| 4 | +use crate::util::point::*; |
| 5 | + |
| 6 | +type Cache = FastMap<(usize, usize), usize>; |
| 7 | + |
| 8 | +pub fn parse(input: &str) -> &str { |
| 9 | + input |
| 10 | +} |
| 11 | + |
| 12 | +pub fn part1(input: &str) -> usize { |
| 13 | + chain(input, 3) |
| 14 | +} |
| 15 | + |
| 16 | +pub fn part2(input: &str) -> usize { |
| 17 | + chain(input, 26) |
| 18 | +} |
| 19 | + |
| 20 | +fn chain(input: &str, limit: usize) -> usize { |
| 21 | + let cache = &mut FastMap::with_capacity(500); |
| 22 | + input |
| 23 | + .lines() |
| 24 | + .map(str::as_bytes) |
| 25 | + .zip(input.iter_unsigned::<usize>()) |
| 26 | + .map(|(code, numeric)| dfs(cache, code, 0, limit) * numeric) |
| 27 | + .sum() |
| 28 | +} |
| 29 | + |
| 30 | +fn dfs(cache: &mut Cache, slice: &[u8], depth: usize, limit: usize) -> usize { |
| 31 | + if depth == limit { |
| 32 | + return slice.len(); |
| 33 | + } |
| 34 | + |
| 35 | + let key = (to_usize(slice), depth); |
| 36 | + if let Some(&previous) = cache.get(&key) { |
| 37 | + return previous; |
| 38 | + } |
| 39 | + |
| 40 | + let keypad = if depth == 0 { NUMERIC } else { DIRECTIONAL }; |
| 41 | + let mut shortest = usize::MAX; |
| 42 | + |
| 43 | + for sequence in combinations(slice, &keypad) { |
| 44 | + let mut presses = 0; |
| 45 | + |
| 46 | + for chunk in sequence.split_inclusive(|&b| b == b'A') { |
| 47 | + presses += dfs(cache, chunk, depth + 1, limit); |
| 48 | + } |
| 49 | + |
| 50 | + shortest = shortest.min(presses); |
| 51 | + } |
| 52 | + |
| 53 | + cache.insert(key, shortest); |
| 54 | + shortest |
| 55 | +} |
| 56 | + |
| 57 | +fn combinations(current: &[u8], keypad: &Keypad) -> Vec<Vec<u8>> { |
| 58 | + let mut next = Vec::new(); |
| 59 | + pad_dfs(&mut next, &mut Vec::with_capacity(16), keypad, current, 0, keypad.start); |
| 60 | + next |
| 61 | +} |
| 62 | + |
| 63 | +fn pad_dfs( |
| 64 | + combinations: &mut Vec<Vec<u8>>, |
| 65 | + path: &mut Vec<u8>, |
| 66 | + keypad: &Keypad, |
| 67 | + sequence: &[u8], |
| 68 | + depth: usize, |
| 69 | + from: Point, |
| 70 | +) { |
| 71 | + // Success |
| 72 | + if depth == sequence.len() { |
| 73 | + combinations.push(path.clone()); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Failure |
| 78 | + if from == keypad.gap { |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + let to = keypad.lookup[sequence[depth] as usize]; |
| 83 | + |
| 84 | + if from == to { |
| 85 | + // Push button. |
| 86 | + path.push(b'A'); |
| 87 | + pad_dfs(combinations, path, keypad, sequence, depth + 1, from); |
| 88 | + path.pop(); |
| 89 | + } else { |
| 90 | + // Move towards button. |
| 91 | + let mut step = |next: u8, direction: Point| { |
| 92 | + path.push(next); |
| 93 | + pad_dfs(combinations, path, keypad, sequence, depth, from + direction); |
| 94 | + path.pop(); |
| 95 | + }; |
| 96 | + |
| 97 | + if to.x < from.x { |
| 98 | + step(b'<', LEFT); |
| 99 | + } |
| 100 | + if to.x > from.x { |
| 101 | + step(b'>', RIGHT); |
| 102 | + } |
| 103 | + if to.y < from.y { |
| 104 | + step(b'^', UP); |
| 105 | + } |
| 106 | + if to.y > from.y { |
| 107 | + step(b'v', DOWN); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +struct Keypad { |
| 113 | + start: Point, |
| 114 | + gap: Point, |
| 115 | + lookup: [Point; 128], |
| 116 | +} |
| 117 | + |
| 118 | +const NUMERIC: Keypad = { |
| 119 | + let start = Point::new(2, 3); |
| 120 | + let gap = Point::new(0, 3); |
| 121 | + let mut lookup = [ORIGIN; 128]; |
| 122 | + |
| 123 | + lookup[b'7' as usize] = Point::new(0, 0); |
| 124 | + lookup[b'8' as usize] = Point::new(1, 0); |
| 125 | + lookup[b'9' as usize] = Point::new(2, 0); |
| 126 | + lookup[b'4' as usize] = Point::new(0, 1); |
| 127 | + lookup[b'5' as usize] = Point::new(1, 1); |
| 128 | + lookup[b'6' as usize] = Point::new(2, 1); |
| 129 | + lookup[b'1' as usize] = Point::new(0, 2); |
| 130 | + lookup[b'2' as usize] = Point::new(1, 2); |
| 131 | + lookup[b'3' as usize] = Point::new(2, 2); |
| 132 | + lookup[b'0' as usize] = Point::new(1, 3); |
| 133 | + lookup[b'A' as usize] = Point::new(2, 3); |
| 134 | + |
| 135 | + Keypad { start, gap, lookup } |
| 136 | +}; |
| 137 | + |
| 138 | +const DIRECTIONAL: Keypad = { |
| 139 | + let start = Point::new(2, 0); |
| 140 | + let gap = Point::new(0, 0); |
| 141 | + let mut lookup = [ORIGIN; 128]; |
| 142 | + |
| 143 | + lookup[b'^' as usize] = Point::new(1, 0); |
| 144 | + lookup[b'A' as usize] = Point::new(2, 0); |
| 145 | + lookup[b'<' as usize] = Point::new(0, 1); |
| 146 | + lookup[b'v' as usize] = Point::new(1, 1); |
| 147 | + lookup[b'>' as usize] = Point::new(2, 1); |
| 148 | + |
| 149 | + Keypad { start, gap, lookup } |
| 150 | +}; |
| 151 | + |
| 152 | +// Max slice length is 5 so value is unique. |
| 153 | +fn to_usize(slice: &[u8]) -> usize { |
| 154 | + let mut array = [0; 8]; |
| 155 | + array[0..slice.len()].copy_from_slice(slice); |
| 156 | + usize::from_ne_bytes(array) |
| 157 | +} |
0 commit comments