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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
/* Copyright 2023 Mario Finelli
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//! Advent of Code 2015 Day 16: <https://adventofcode.com/2015/day/16>
//!
//! Today's challenge is pretty straightforward. We don't have all of the
//! information for the Aunt Sues but presumably, there is only one correct
//! answer in the list of Aunt Sues, and so each incorrect Sue will have some
//! disqualifying characteristic. The solution is therefore simple, check the
//! information that we have about each Sue, if we can't find anything that
//! disqualifies here then she's the one that we're looking for!
use regex::Regex;
/// How many children Aunt Sue has (provided by the prompt).
const CHILDREN: u32 = 3;
/// How many cats Aunt Sue has (provided by the prompt).
const CATS: u32 = 7;
/// How many samoyeds Aunt Sue has (provided by the prompt).
const SAMOYEDS: u32 = 2;
/// How many pomeranians Aunt Sue has (provided by the prompt).
const POMERANIANS: u32 = 3;
/// How many akitas Aunt Sue has (provided by the prompt).
const AKITAS: u32 = 0;
/// How many vizslas Aunt Sue has (provided by the prompt).
const VIZSLAS: u32 = 0;
/// How many goldfish Aunt Sue has (provided by the prompt).
const GOLDFISH: u32 = 5;
/// How many trees Aunt Sue has (provided by the prompt).
const TREES: u32 = 3;
/// How many cars Aunt Sue has (provided by the prompt).
const CARS: u32 = 2;
/// How many perfumes Aunt Sue has (provided by the prompt).
const PERFUMES: u32 = 1;
/// AuntSue represents one of the Aunt Sues from the input list. She has an
/// identifying number, and then optional information that we remember about
/// her.
#[derive(Debug, PartialEq)]
struct AuntSue {
number: u32,
children: Option<u32>,
cats: Option<u32>,
samoyeds: Option<u32>,
pomeranians: Option<u32>,
akitas: Option<u32>,
vizslas: Option<u32>,
goldfish: Option<u32>,
trees: Option<u32>,
cars: Option<u32>,
perfumes: Option<u32>,
}
impl AuntSue {
/// Creates a new `AuntSue` with the given identifying number and no
/// information remembered about her (to be filled in later).
fn new(number: u32) -> AuntSue {
AuntSue {
number,
children: None,
cats: None,
samoyeds: None,
pomeranians: None,
akitas: None,
vizslas: None,
goldfish: None,
trees: None,
cars: None,
perfumes: None,
}
}
}
/// The solution for the day sixteen challenge.
///
/// Starting with the input as a string and the part we're solving (which only
/// affects some of the comparisons that we do to find the real aunt sue) we
/// parse the input into `AuntSue` objects and then try to find out if we have
/// the one we're looking for. In theory we could optimize this further by not
/// creating objects but running the comparison as we try to check each field,
/// but I don't think that it's worth it. Leaving it as-is creates a cleaner,
/// easier-to-reason-about solution. If we get `0` back (which is not a valid
/// Aunt Sue) then it means that we exhausted the input list of Sues and were
/// unable to find the one that we were looking for.
///
/// # Example
/// ```rust
/// # use aoc::y15d16::y15d16;
/// // probably read this from the input file...
/// let input = concat!(
/// "Sue 1: goldfish: 5, cars: 2, samoyeds: 2\n",
/// "Sue 2: goldfish: 4, cars: 2, samoyeds: 2",
/// );
/// assert_eq!(y15d16(input, 1), 1);
/// assert_eq!(y15d16(input, 2), 2);
/// ```
pub fn y15d16(input: &str, part: u32) -> u32 {
let lines: Vec<_> = input.lines().collect();
let number_regex = Regex::new(r"^Sue (\d+):").unwrap();
let children_regex = Regex::new(r"children: (\d+)").unwrap();
let cats_regex = Regex::new(r"cats: (\d+)").unwrap();
let samoyeds_regex = Regex::new(r"samoyeds: (\d+)").unwrap();
let pomeranians_regex = Regex::new(r"pomeranians: (\d+)").unwrap();
let akitas_regex = Regex::new(r"akitas: (\d+)").unwrap();
let vizslas_regex = Regex::new(r"vizslas: (\d+)").unwrap();
let goldfish_regex = Regex::new(r"goldfish: (\d+)").unwrap();
let trees_regex = Regex::new(r"trees: (\d+)").unwrap();
let cars_regex = Regex::new(r"cars: (\d+)").unwrap();
let perfumes_regex = Regex::new(r"perfumes: (\d+)").unwrap();
for line in lines {
let number = number_regex.captures(line).unwrap();
let mut sue = AuntSue::new(number[1].parse().unwrap());
if let Some(c) = children_regex.captures(line) {
sue.children = Some(c[1].parse().unwrap());
}
if let Some(c) = cats_regex.captures(line) {
sue.cats = Some(c[1].parse().unwrap());
}
if let Some(s) = samoyeds_regex.captures(line) {
sue.samoyeds = Some(s[1].parse().unwrap());
}
if let Some(p) = pomeranians_regex.captures(line) {
sue.pomeranians = Some(p[1].parse().unwrap());
}
if let Some(a) = akitas_regex.captures(line) {
sue.akitas = Some(a[1].parse().unwrap());
}
if let Some(v) = vizslas_regex.captures(line) {
sue.vizslas = Some(v[1].parse().unwrap());
}
if let Some(g) = goldfish_regex.captures(line) {
sue.goldfish = Some(g[1].parse().unwrap());
}
if let Some(t) = trees_regex.captures(line) {
sue.trees = Some(t[1].parse().unwrap());
}
if let Some(c) = cars_regex.captures(line) {
sue.cars = Some(c[1].parse().unwrap());
}
if let Some(p) = perfumes_regex.captures(line) {
sue.perfumes = Some(p[1].parse().unwrap());
}
if the_real_aunt_sue(&sue, part) {
return sue.number;
}
}
0
}
/// This function determines if the passed Aunt Sue is the _real_ Aunt Sue. We
/// check each of her characteristics and if we find one that doesn't match
/// the input we were given then we disqualify her. If we can't disqualify her
/// then we have found the one we're looking for.
fn the_real_aunt_sue(sue: &AuntSue, part: u32) -> bool {
if let Some(c) = sue.children {
if c != CHILDREN {
return false;
}
}
if let Some(c) = sue.cats {
if (part == 2 && c <= CATS) || (part == 1 && c != CATS) {
return false;
}
}
if let Some(s) = sue.samoyeds {
if s != SAMOYEDS {
return false;
}
}
if let Some(p) = sue.pomeranians {
if (part == 2 && p >= POMERANIANS) || (part == 1 && p != POMERANIANS) {
return false;
}
}
if let Some(a) = sue.akitas {
if a != AKITAS {
return false;
}
}
if let Some(v) = sue.vizslas {
if v != VIZSLAS {
return false;
}
}
if let Some(g) = sue.goldfish {
if (part == 2 && g >= GOLDFISH) || (part == 1 && g != GOLDFISH) {
return false;
}
}
if let Some(t) = sue.trees {
if (part == 2 && t <= TREES) || (part == 1 && t != TREES) {
return false;
}
}
if let Some(c) = sue.cars {
if c != CARS {
return false;
}
}
if let Some(p) = sue.perfumes {
if p != PERFUMES {
return false;
}
}
true
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
#[test]
fn test_auntsue_new() {
assert_eq!(
AuntSue::new(10),
AuntSue {
number: 10,
children: None,
cats: None,
samoyeds: None,
pomeranians: None,
akitas: None,
vizslas: None,
goldfish: None,
trees: None,
cars: None,
perfumes: None,
}
);
}
#[test]
fn test_the_real_aunt_sue() {
let mut input = AuntSue::new(10);
input.children = Some(CHILDREN);
input.cats = Some(CATS);
input.samoyeds = Some(SAMOYEDS);
input.pomeranians = Some(POMERANIANS);
input.akitas = Some(AKITAS);
input.vizslas = Some(VIZSLAS);
input.goldfish = Some(GOLDFISH);
input.trees = Some(TREES);
input.cars = Some(CARS);
input.perfumes = Some(PERFUMES);
assert!(the_real_aunt_sue(&input, 1));
input.children = Some(CHILDREN + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.children = Some(CHILDREN);
input.cats = Some(CATS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.cats = Some(CATS);
input.samoyeds = Some(SAMOYEDS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.samoyeds = Some(SAMOYEDS);
input.pomeranians = Some(POMERANIANS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.pomeranians = Some(POMERANIANS);
input.akitas = Some(AKITAS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.akitas = Some(AKITAS);
input.vizslas = Some(VIZSLAS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.vizslas = Some(VIZSLAS);
input.goldfish = Some(GOLDFISH + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.goldfish = Some(GOLDFISH);
input.trees = Some(TREES + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.trees = Some(TREES);
input.cars = Some(CARS + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.cars = Some(CARS);
input.perfumes = Some(PERFUMES + 1);
assert!(!the_real_aunt_sue(&input, 1));
input.perfumes = Some(PERFUMES);
input.cats = Some(CATS + 1);
input.pomeranians = Some(POMERANIANS - 1);
input.goldfish = Some(GOLDFISH - 1);
input.trees = Some(TREES + 1);
assert!(the_real_aunt_sue(&input, 2));
input.cats = Some(CATS);
assert!(!the_real_aunt_sue(&input, 2));
input.cats = Some(CATS + 1);
input.pomeranians = Some(POMERANIANS);
assert!(!the_real_aunt_sue(&input, 2));
input.pomeranians = Some(POMERANIANS - 1);
input.goldfish = Some(GOLDFISH);
assert!(!the_real_aunt_sue(&input, 2));
input.goldfish = Some(GOLDFISH - 1);
input.trees = Some(TREES);
assert!(!the_real_aunt_sue(&input, 2));
input.trees = Some(TREES + 1);
}
#[test]
fn the_solution() {
let contents = fs::read_to_string("input/2015/day16.txt").unwrap();
assert_eq!(y15d16(&contents, 1), 40);
assert_eq!(y15d16(&contents, 2), 241);
}
}