|
| 1 | +use regex::Regex; |
| 2 | +use std::collections::HashMap; |
| 3 | +use std::collections::HashSet; |
| 4 | + |
| 5 | +pub fn run(input: Vec<String>) { |
| 6 | + let part1 = solve(&input, false); |
| 7 | + println!("part 1: {}", part1); |
| 8 | + assert_eq!(part1, 664); |
| 9 | + |
| 10 | + let part2 = solve(&input, true); |
| 11 | + println!("part 2: {}", part2); |
| 12 | + assert_eq!(part2, 640); |
| 13 | +} |
| 14 | + |
| 15 | +fn solve(input: &Vec<String>, extra: bool) -> i64 { |
| 16 | + // parse the input |
| 17 | + let mut happiness: HashMap<(&str, &str), i64> = HashMap::new(); |
| 18 | + let mut people: HashSet<&str> = HashSet::new(); |
| 19 | + |
| 20 | + let re = |
| 21 | + Regex::new(r"^(.+?) would (gain|lose) (\d+?) happiness units by sitting next to (.+?)\.$") |
| 22 | + .unwrap(); |
| 23 | + for line in input.iter() { |
| 24 | + match re.captures(line) { |
| 25 | + Some(cap) => { |
| 26 | + let p1 = cap.get(1).unwrap().as_str(); |
| 27 | + let p2 = cap.get(4).unwrap().as_str(); |
| 28 | + people.insert(p1); |
| 29 | + people.insert(p2); |
| 30 | + let sign = if cap.get(2).unwrap().as_str() == "gain" { |
| 31 | + 1 |
| 32 | + } else { |
| 33 | + -1 |
| 34 | + }; |
| 35 | + let units: i64 = cap.get(3).unwrap().as_str().parse().unwrap(); |
| 36 | + happiness.insert((p1, p2), units * sign); |
| 37 | + } |
| 38 | + None => { |
| 39 | + panic!("invalid input: {}", line); |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // handle part2 |
| 45 | + if extra { |
| 46 | + let you = "you"; |
| 47 | + for p in people.iter() { |
| 48 | + happiness.insert((p, you), 0); |
| 49 | + happiness.insert((you, p), 0); |
| 50 | + } |
| 51 | + people.insert(you); |
| 52 | + } |
| 53 | + |
| 54 | + // we "anchor" one person, because the table is round. |
| 55 | + let p = people.iter().next().unwrap().clone(); |
| 56 | + let mut seen = HashSet::new(); |
| 57 | + seen.insert(p); |
| 58 | + |
| 59 | + // recursively explore the search space. |
| 60 | + let max = find_max(&happiness, p, p, &people, &mut seen); |
| 61 | + return max; |
| 62 | +} |
| 63 | + |
| 64 | +fn find_max<'a>(happiness: &HashMap<(&'a str, &'a str), i64>, |
| 65 | + first: &'a str, |
| 66 | + prev: &'a str, |
| 67 | + people: &HashSet<&'a str>, |
| 68 | + seen: &mut HashSet<&'a str>) |
| 69 | + -> i64 { |
| 70 | + if seen.len() == people.len() { |
| 71 | + // we are done. We have to adjust for first and last people sitting next to each other |
| 72 | + return happiness.get(&(first, prev)).unwrap() + happiness.get(&(prev, first)).unwrap(); |
| 73 | + } |
| 74 | + let mut max = None; |
| 75 | + for p in people.iter() { |
| 76 | + if seen.contains(p) { |
| 77 | + continue; |
| 78 | + } |
| 79 | + let p = p.clone(); |
| 80 | + seen.insert(p); |
| 81 | + let mut v = find_max(happiness, first, p, people, seen); |
| 82 | + seen.remove(p); |
| 83 | + v += *happiness.get(&(prev, p)).unwrap(); |
| 84 | + v += *happiness.get(&(p, prev)).unwrap(); |
| 85 | + max = match max { |
| 86 | + None => Some(v), |
| 87 | + Some(max) => if v > max { Some(v) } else { Some(max) }, |
| 88 | + } |
| 89 | + } |
| 90 | + return max.unwrap(); |
| 91 | +} |
0 commit comments