From 99ffb910798d46b8c8a6d91aa8ff2b06fc4313f8 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 9 Dec 2020 22:32:05 -0800 Subject: 10 --- 10/src/main.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 10/src/main.rs (limited to '10/src') diff --git a/10/src/main.rs b/10/src/main.rs new file mode 100644 index 0000000..d504711 --- /dev/null +++ b/10/src/main.rs @@ -0,0 +1,67 @@ +use advent_lib::prelude::*; + +use std::convert::TryFrom; + + +fn main() -> Result<()> { + let mut args = std::env::args(); + if args.len() != 2 { + eprintln!("Usage: advent input"); + } + let _ = args.next(); + let filename = args.next().unwrap(); + + let input = advent_lib::read_lines_file(&filename)?; + + let mut adaptors: Vec = Vec::new(); + for line in &input { + adaptors.push(line.parse::().unwrap()); + } + adaptors.sort(); + + let mut buckets = Vec::new(); + buckets.push(0); + buckets.push(0); + buckets.push(0); + buckets.push(0); + + for i in 0 .. adaptors.len() { + let mut prev_joltage = 0; + if i > 0 { + prev_joltage = adaptors[i - 1]; + } + let difference = adaptors[i] - prev_joltage; + buckets[usize::try_from(difference).unwrap()] += 1; + } + + buckets[3] += 1; + + println!("{}", buckets[1]*buckets[3]); + + let mut reversed_adaptors = Vec::new(); + for i in 0 .. adaptors.len() { + reversed_adaptors.push(adaptors[adaptors.len() - i - 1]); + } + reversed_adaptors.push(0); + + let mut memoized_counts: Vec = Vec::new(); + memoized_counts.push(1); + + for i in 1 .. reversed_adaptors.len() { + let mut count = 0; + for j in 1 .. i+1 { + let difference = reversed_adaptors[i - j] - reversed_adaptors[i]; + if difference <= 3 { + count += memoized_counts[i - j]; + } else { + break; + } + } + memoized_counts.push(count); + } + + println!("{}", memoized_counts[memoized_counts.len()-1]); + + Ok(()) +} + -- cgit 1.4.1