From 25d33a1bb819f749c421e76bb36e694fa2830162 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 24 Dec 2017 07:55:51 +0200 Subject: Day 24: I will build the strongest lego-bridge in the galaxy! --- src/bin/day_24.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src') diff --git a/src/bin/day_24.rs b/src/bin/day_24.rs index 1fc3eca..eb7fddd 100644 --- a/src/bin/day_24.rs +++ b/src/bin/day_24.rs @@ -3,4 +3,58 @@ use advent_of_code_2017::*; fn main() { let args = AdventArgs::init(); + let components: Vec = args.input.iter() + .map(|line| { + let mut split = line.split('/'); + Component { + a: split.next().unwrap().parse().unwrap(), + b: split.next().unwrap().parse().unwrap() + } + }) + .collect(); + + if args.part == 1 { + let strongest = build_strongest(0, components); + println!("{}", strongest); + } else { + let (strongest, longest) = build_longest(0, components); + println!("length: {}, strength: {}", longest, strongest); + } +} + +fn build_strongest(start: u32, components: Vec) -> u32 { + components.iter().enumerate() + .filter(|&(_, c)| c.a == start || c.b == start) + .map(|(i, c)| { + let end = if c.a == start { c.b } else { c.a }; + let mut subset = components.clone(); + subset.remove(i); + c.strength() + build_strongest(end, subset) + }).max().unwrap_or(0) +} + +fn build_longest(start: u32, components: Vec) -> (u32, u32) { + components.iter().enumerate() + .filter(|&(_, c)| c.a == start || c.b == start) + .map(|(i, c)| { + let end = if c.a == start { c.b } else { c.a }; + let mut subset = components.clone(); + subset.remove(i); + let (s, l) = build_longest(end, subset); + (c.strength() + s, 1 + l) + }).max_by(|&(s1, l1), &(s2, l2)| { + l1.cmp(&l2).then(s1.cmp(&s2)) + }).unwrap_or((0, 0)) +} + +#[derive(Debug, Clone)] +struct Component { + a: u32, + b: u32 +} + +impl Component { + fn strength(&self) -> u32 { + self.a + self.b + } } -- cgit v1.2.3