Created
December 10, 2020 06:55
-
-
Save erikaderstedt/14c3b427d41d280f7578066655e471a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::common::Solution; | |
use crate::common::parsed_from_each_line; | |
#[derive(Debug)] | |
struct Data { | |
level: i64, | |
consecutive_ones: usize, | |
seqs: [usize;4], | |
} | |
impl Data { | |
fn new() -> Data { Data { level: 0, consecutive_ones: 0, seqs: [0;4]} } | |
fn num_one_diffs(&self) -> usize { self.seqs.iter().enumerate().map(|(i, n)| n*(i+1)).sum() } | |
} | |
pub fn solve(input: &String) -> Solution { | |
let mut nums:Vec<i64> = parsed_from_each_line(input); | |
let device = nums.iter().max().unwrap() + 3; | |
nums.sort(); | |
nums.push(device); | |
let r = nums.iter().fold(Data::new(), |data,adapter| { | |
match adapter-data.level { | |
1 => Data { level: *adapter, consecutive_ones: data.consecutive_ones + 1, seqs: data.seqs }, | |
3 => { | |
let mut n = data.seqs.clone(); | |
match data.consecutive_ones { | |
1..=4 => { n[data.consecutive_ones-1] += 1; Data { level: *adapter, consecutive_ones: 0, seqs: n } }, | |
_ => Data { level: *adapter, consecutive_ones: 0, seqs: n }, | |
}}, | |
_ => panic!("I didn't think this was in the data.") | |
} | |
}); | |
let num_one_diffs = r.num_one_diffs(); | |
let num_three_diffs = nums.len() - num_one_diffs; | |
let p1 = num_one_diffs * num_three_diffs; | |
let p2 = (7 as i64).pow(r.seqs[3] as u32) * (4 as i64).pow(r.seqs[2] as u32) * (2 as i64).pow(r.seqs[1] as u32); | |
Solution { part_1: p1.to_string(), part_2: p2.to_string() } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment