1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
/* * Copyright (c) 2020 Erik Nordstrøm <erik@nordstroem.no> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ use std::collections::HashMap; solution_printer!(6, print_solution, input_generator, INPUT, solve_part_1, solve_part_2); pub const INPUT: &str = include_str!("../input/2019/day6.txt"); /// ### Day 6: Universal Orbit Map /// /// [https://adventofcode.com/2019/day/6](https://adventofcode.com/2019/day/6) /// /// You've landed at the Universal Orbit Map facility on Mercury. Because /// navigation in space often involves transferring between orbits, the orbit /// maps here are useful for finding efficient routes between, for example, you /// and Santa. You download a map of the local orbits (your puzzle input). /// /// Except for the universal Center of Mass (`COM`), every object in space is in /// orbit around exactly one other object. An [orbit](https://en.wikipedia.org/wiki/Orbit) looks roughly like this: /// /// ```text /// \ /// \ /// | /// | /// AAA--> o o <--BBB /// | /// | /// / /// / /// ``` /// /// In this diagram, the object `BBB` is in orbit around `AAA`. The path that `BBB` /// takes around `AAA` (drawn with lines) is only partly shown. In the map data, /// this orbital relationship is written `AAA)BBB`, which means "`BBB` is in orbit /// around `AAA`". /// /// Before you use your map data to plot a course, you need to make sure it /// wasn't corrupted during the download. To verify maps, the Universal Orbit /// Map facility uses *orbit count checksums* - the total number of *direct orbits* /// (like the one shown above) and *indirect orbits*. /// /// Whenever `A` orbits `B` and `B` orbits `C`, then `A` *indirectly orbits* `C`. This chain /// can be any number of objects long: if `A` orbits `B`, `B` orbits `C`, and `C` orbits /// `D`, then `A` indirectly orbits `D`. /// /// For example, suppose you have the following map: /// /// ```text /// COM)B /// B)C /// C)D /// D)E /// E)F /// B)G /// G)H /// D)I /// E)J /// J)K /// K)L /// ``` /// /// Visually, the above map of orbits looks like this: /// /// ```text /// G - H J - K - L /// / / /// COM - B - C - D - E - F /// \ /// I /// ``` /// /// In this visual representation, when two objects are connected by a line, /// the one on the right directly orbits the one on the left. /// /// Here, we can count the total number of orbits as follows: /// /// - `D` directly orbits `C` and indirectly orbits `B` and `COM`, a total of `3` /// orbits. /// - `L` directly orbits `K` and indirectly orbits `J`, `E`, `D`, `C`, `B`, and `COM`, a /// total of `7` orbits. /// - `COM` orbits nothing. /// /// The total number of direct and indirect orbits in this example is `42`. /// /// *What is the total number of direct and indirect orbits* in your map data? /// /// ### Examples /// /// ``` /// use codetrotter_aoc_2019_solutions::day_06::{input_generator, solve_part_1}; /// /// const EXINPUT: &str = /// "COM)B\n\ /// B)C\n\ /// C)D\n\ /// D)E\n\ /// E)F\n\ /// B)G\n\ /// G)H\n\ /// D)I\n\ /// E)J\n\ /// J)K\n\ /// K)L"; /// /// assert_eq!(solve_part_1(&mut input_generator(EXINPUT)), 42); /// ``` /// /// ### Solution /// /// ⚠️ SPOILER ALERT ⚠️ /// /// ``` /// use codetrotter_aoc_2019_solutions::day_06::{INPUT, input_generator, solve_part_1}; /// assert_eq!(solve_part_1(&mut input_generator(INPUT)), 333679); /// ``` pub fn solve_part_1<'a, T> (input_orbit_pairs: &mut T) -> u32 where T: Iterator<Item = OrbitPair<'a>> { let orbits = orbits_from_orbit_pairs(input_orbit_pairs); get_total_number_of_orbits_direct_and_indirect(&orbits, "COM", 0) } fn get_total_number_of_orbits_direct_and_indirect (orbits: &Orbits, body: &str, num_others_orbited_by_body: u32) -> u32 { match orbits.get(body) { None => num_others_orbited_by_body, Some(orbiters) => { num_others_orbited_by_body + orbiters.iter().map(|&orbiter| get_total_number_of_orbits_direct_and_indirect(orbits, orbiter, num_others_orbited_by_body + 1)).sum::<u32>() }, } } pub type OrbitPair<'a> = (&'a str, &'a str); pub type Orbits<'a> = HashMap<&'a str, Vec<&'a str>>; pub type Orbiting<'a> = HashMap<&'a str, &'a str>; pub fn orbits_from_orbit_pairs<'a, T> (orbit_pairs: &mut T) -> Orbits<'a> where T: Iterator<Item = OrbitPair<'a>> { let mut orbits = Orbits::new(); for (orbited, orbiter) in orbit_pairs { let orbiters = orbits.entry(orbited).or_insert(Default::default()); orbiters.push(orbiter); } orbits } pub fn input_generator (input: &'static str) -> impl Iterator<Item = OrbitPair> { input.lines().map(|orbit_pair_str| { let (orbited, remain) = orbit_pair_str.split_at(orbit_pair_str.find(")").unwrap()); let orbiter = &remain[1..]; (orbited, orbiter) }) } /// ### Day 6, Part Two /// /// [https://adventofcode.com/2019/day/6#part2](https://adventofcode.com/2019/day/6#part2) /// /// Now, you just need to figure out how many *orbital transfers* you (`YOU`) need /// to take to get to Santa (`SAN`). /// /// You start at the object `YOU` are orbiting; your destination is the object /// `SAN` is orbiting. An orbital transfer lets you move from any object to an /// object orbiting or orbited by that object. /// /// For example, suppose you have the following map: /// /// ```text /// COM)B /// B)C /// C)D /// D)E /// E)F /// B)G /// G)H /// D)I /// E)J /// J)K /// K)L /// K)YOU /// I)SAN /// ``` /// /// Visually, the above map of orbits looks like this: /// /// ```text /// YOU /// / /// G - H J - K - L /// / / /// COM - B - C - D - E - F /// \ /// I - SAN /// ``` /// /// In this example, `YOU` are in orbit around `K`, and `SAN` is in orbit around `I`. /// To move from `K` to `I`, a minimum of `4` orbital transfers are required: /// /// - `K` to `J` /// - `J` to `E` /// - `E` to `D` /// - `D` to `I` /// /// Afterward, the map of orbits looks like this: /// /// ```text /// G - H J - K - L /// / / /// COM - B - C - D - E - F /// \ /// I - SAN /// \ /// YOU /// ``` /// /// *What is the minimum number of orbital transfers required* to move from the /// object `YOU` are orbiting to the object `SAN` is orbiting? (Between the objects /// they are orbiting - *not* between `YOU` and `SAN`.) /// /// ### Examples /// /// ``` /// use codetrotter_aoc_2019_solutions::day_06::{input_generator, solve_part_2}; /// /// const EXINPUT: &str = /// "COM)B\n\ /// B)C\n\ /// C)D\n\ /// D)E\n\ /// E)F\n\ /// B)G\n\ /// G)H\n\ /// D)I\n\ /// E)J\n\ /// J)K\n\ /// K)L\n\ /// K)YOU\n\ /// I)SAN"; /// /// assert_eq!(solve_part_2(&mut input_generator(EXINPUT)), 4); /// ``` /// /// ### Solution /// /// ⚠️ SPOILER ALERT ⚠️ /// /// ``` /// use codetrotter_aoc_2019_solutions::day_06::{INPUT, input_generator, solve_part_2}; /// assert_eq!(solve_part_2(&mut input_generator(INPUT)), 370); /// ``` pub fn solve_part_2<'a, T> (input_orbit_pairs: &mut T) -> usize where T: Iterator<Item = OrbitPair<'a>> { let orbiting = orbiting_from_orbit_pairs(input_orbit_pairs); let mut you_orbit = *orbiting.get("YOU").unwrap(); let mut santa_orbits = *orbiting.get("SAN").unwrap(); let mut you_orbits_from_com = vec![]; while you_orbit != "COM" { you_orbits_from_com.push(you_orbit); you_orbit = *orbiting.get(you_orbit).unwrap(); } drop(you_orbit); let mut santa_orbits_from_com = vec![]; while santa_orbits != "COM" { santa_orbits_from_com.push(santa_orbits); santa_orbits = *orbiting.get(santa_orbits).unwrap(); } drop(santa_orbits); let mut distance_between_you_and_santa = 0; let mut you_orbits_from_com: &[&str] = &*you_orbits_from_com; if you_orbits_from_com.len() > santa_orbits_from_com.len() { let num_orbits_from_com_diff = you_orbits_from_com.len() - santa_orbits_from_com.len(); you_orbits_from_com = &you_orbits_from_com[num_orbits_from_com_diff..]; distance_between_you_and_santa += num_orbits_from_com_diff; } let mut santa_orbits_from_com: &[&str] = &*santa_orbits_from_com; if santa_orbits_from_com.len() > santa_orbits_from_com.len() { let num_orbits_from_com_diff = santa_orbits_from_com.len() - you_orbits_from_com.len(); santa_orbits_from_com = &santa_orbits_from_com[num_orbits_from_com_diff..]; distance_between_you_and_santa += num_orbits_from_com_diff; } if you_orbits_from_com.len() > 0 { while you_orbits_from_com[0] != santa_orbits_from_com[0] { you_orbits_from_com = &you_orbits_from_com[1..]; santa_orbits_from_com = &santa_orbits_from_com[1..]; distance_between_you_and_santa += 2; } } distance_between_you_and_santa } pub fn orbiting_from_orbit_pairs<'a, T> (orbit_pairs: &mut T) -> Orbiting<'a> where T: Iterator<Item = OrbitPair<'a>> { orbit_pairs.map(|(orbited, orbiter)| (orbiter, orbited)).collect() }