From 6d230570a9d96444cb93c2840915c060c0302902 Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 13 Jan 2024 19:36:51 +0100 Subject: [PATCH] Day 13 part 1. --- src/day13.rs | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 83 insertions(+) create mode 100644 src/day13.rs diff --git a/src/day13.rs b/src/day13.rs new file mode 100644 index 0000000..c4e777a --- /dev/null +++ b/src/day13.rs @@ -0,0 +1,82 @@ +use std::collections::{HashMap, HashSet}; + +use aoc_runner_derive::{aoc, aoc_generator}; + +#[aoc_generator(day13)] +fn parse(input: &str) -> HashMap<(String, String), i32> { + input.lines() + .map(|line| { + let v: Vec<&str> = line[0..line.len() - 1].split(" ").collect(); + let from = v[0]; + let to = v[10]; + let mut value = v[3].parse::().unwrap(); + if v[2] == "lose" { + value *= -1; + } + ((from.into(), to.into()), value) + }).collect() +} + +#[aoc(day13, part1)] +fn part1(deltas: &HashMap<(String, String), i32>) -> i32 { + let names: HashSet<_> = deltas.keys().map(|s| s.0.clone()).collect(); + permutations(names).iter().map(|arrangement| { + let mut happiness = 0; + for i in 0..arrangement.len() { + let left = if i == 0 { arrangement.len() - 1 } else { i - 1 }; + let right = (i + 1) % arrangement.len(); + happiness += deltas[&(arrangement[left].clone(), arrangement[i].clone())] + + deltas[&(arrangement[i].clone(), arrangement[left].clone())]; + } + happiness + }).max().unwrap() +} + +fn permutations(values: HashSet) -> Vec> { + let base: Vec = values.into_iter().collect(); + p(base) +} + +fn p(mut input: Vec) -> Vec> { + if input.len() == 1 { + return vec![input]; + } + let mut results = Vec::new(); + for _ in 0..input.len() { + p(input.iter().skip(1).map(|t| t.clone()).collect()).into_iter().for_each(|mut v| { + v.insert(0, input[0].clone()); + results.push(v); + }); + let v = input.remove(0); + input.push(v); + } + results +} + + +// #[aoc(day13, part2)] +// fn part2(input: &str) -> String { +// todo!() +// } + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_part1() { + let input = "Alice would gain 54 happiness units by sitting next to Bob. +Alice would lose 79 happiness units by sitting next to Carol. +Alice would lose 2 happiness units by sitting next to David. +Bob would gain 83 happiness units by sitting next to Alice. +Bob would lose 7 happiness units by sitting next to Carol. +Bob would lose 63 happiness units by sitting next to David. +Carol would lose 62 happiness units by sitting next to Alice. +Carol would gain 60 happiness units by sitting next to Bob. +Carol would gain 55 happiness units by sitting next to David. +David would gain 46 happiness units by sitting next to Alice. +David would lose 7 happiness units by sitting next to Bob. +David would gain 41 happiness units by sitting next to Carol."; + assert_eq!(part1(&parse(input)), 330); + } +} diff --git a/src/lib.rs b/src/lib.rs index 50dc1b5..32f9452 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,5 +5,6 @@ extern crate aoc_runner_derive; mod day10; mod day11; mod day12; +mod day13; aoc_lib! { year = 2015 }