From 6fe31e953618ab0803d10eef5b5a043603394975 Mon Sep 17 00:00:00 2001 From: inga-lovinde <52715130+inga-lovinde@users.noreply.github.com> Date: Sat, 12 Dec 2020 20:44:09 +0100 Subject: [PATCH] Solution for day 10 --- day10/Cargo.toml | 9 +++++++++ day10/src/main.rs | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 day10/Cargo.toml create mode 100644 day10/src/main.rs diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000..7058d2f --- /dev/null +++ b/day10/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day10" +version = "0.1.0" +authors = ["inga-lovinde <52715130+inga-lovinde@users.noreply.github.com>"] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day10/src/main.rs b/day10/src/main.rs new file mode 100644 index 0000000..69b26c0 --- /dev/null +++ b/day10/src/main.rs @@ -0,0 +1,37 @@ +use std::collections::HashMap; +use std::io::{self, BufRead}; + +fn main() { + let stdin = io::stdin(); + let mut joltages: Vec = stdin.lock().lines() + .map(|line| line.unwrap().parse().unwrap()) + .collect(); + joltages.push(0); + joltages.push(joltages.iter().max().unwrap() + 3); + + joltages.sort(); + + let mut differences = HashMap::new(); + for i in 1..joltages.len() { + let count = differences.entry(joltages[i] - joltages[i-1]).or_insert(0); + *count += 1; + } + + for (key, value) in differences.iter() { + println!("{}: {}", key, value); + } + + println!("{}", differences.get(&1).unwrap_or(&0) * differences.get(&3).unwrap_or(&0)); + + let mut arrangements = vec![0u128; joltages.len()]; + arrangements[0] = 1; + for i in 1..joltages.len() { + for j in 0..i { + if (joltages[j] + 3) >= joltages[i] { + arrangements[i] += arrangements[j]; + } + } + } + + println!("{}", arrangements[joltages.len() - 1]); +}