From: Greg Burri Date: Tue, 13 Dec 2022 17:54:45 +0000 (+0100) Subject: Simplify X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=dd893f045c41419c890e580c565daae034f9b95e;p=advent_of_code_2022.git Simplify --- diff --git a/src/day13.rs b/src/day13.rs index ceced61..16e2859 100644 --- a/src/day13.rs +++ b/src/day13.rs @@ -1,6 +1,6 @@ use std::cmp::{Ord, Ordering, PartialOrd}; -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, PartialEq, Eq)] pub enum Signal { Value(i32), List(Vec), @@ -83,13 +83,22 @@ pub fn sum_indices_signals_in_the_right_order(signals: &[Signal]) -> usize { } pub fn product_indices_special_signals(signals: &[Signal]) -> usize { - let mut signals = Vec::from(signals); let s1 = Signal::parse("[[2]]"); let s2 = Signal::parse("[[6]]"); - signals.push(s1.clone()); - signals.push(s2.clone()); - signals.sort(); - (signals.binary_search(&s1).unwrap() + 1) * (signals.binary_search(&s2).unwrap() + 1) + + let mut pos_1 = 1; + let mut pos_2 = 2; + + for s in signals { + if s < &s1 { + pos_1 += 1; + pos_2 += 1; + } else if s < &s2 { + pos_2 += 1; + } + } + + pos_1 * pos_2 } #[cfg(test)]