diff options
-rw-r--r-- | inputs/day_24.txt | 57 | ||||
-rw-r--r-- | src/bin/day_24.rs | 54 |
2 files changed, 111 insertions, 0 deletions
diff --git a/inputs/day_24.txt b/inputs/day_24.txt new file mode 100644 index 0000000..1fbfe25 --- /dev/null +++ b/inputs/day_24.txt @@ -0,0 +1,57 @@ +42/37 +28/28 +29/25 +45/8 +35/23 +49/20 +44/4 +15/33 +14/19 +31/44 +39/14 +25/17 +34/34 +38/42 +8/42 +15/28 +0/7 +49/12 +18/36 +45/45 +28/7 +30/43 +23/41 +0/35 +18/9 +3/31 +20/31 +10/40 +0/22 +1/23 +20/47 +38/36 +15/8 +34/32 +30/30 +30/44 +19/28 +46/15 +34/50 +40/20 +27/39 +3/14 +43/45 +50/42 +1/33 +6/39 +46/44 +22/35 +15/20 +43/31 +23/23 +19/27 +47/15 +43/43 +25/36 +26/38 +1/10 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<Component> = 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<Component>) -> 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<Component>) -> (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 + } } |