From ca275c703e4d73718537f16f0117bdd12f6d3ab7 Mon Sep 17 00:00:00 2001 From: henine Date: Thu, 10 Dec 2020 09:36:37 +0100 Subject: [PATCH] day10 --- day10/Cargo.toml | 9 ++++ day10/input | 106 ++++++++++++++++++++++++++++++++++++++++++++++ day10/src/main.rs | 52 +++++++++++++++++++++++ day10/test | 31 ++++++++++++++ 4 files changed, 198 insertions(+) create mode 100644 day10/Cargo.toml create mode 100644 day10/input create mode 100644 day10/src/main.rs create mode 100644 day10/test diff --git a/day10/Cargo.toml b/day10/Cargo.toml new file mode 100644 index 0000000..476bec7 --- /dev/null +++ b/day10/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day10" +version = "0.1.0" +authors = ["henine "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day10/input b/day10/input new file mode 100644 index 0000000..89adc49 --- /dev/null +++ b/day10/input @@ -0,0 +1,106 @@ +66 +7 +73 +162 +62 +165 +157 +158 +137 +125 +138 +59 +36 +40 +94 +95 +13 +35 +136 +96 +156 +155 +24 +84 +42 +171 +142 +3 +104 +149 +83 +129 +19 +122 +68 +103 +74 +118 +20 +110 +54 +127 +88 +31 +135 +26 +126 +2 +51 +91 +16 +65 +128 +119 +67 +48 +111 +29 +49 +12 +132 +17 +41 +166 +75 +146 +50 +30 +1 +164 +112 +34 +18 +72 +97 +145 +11 +117 +58 +78 +152 +90 +172 +163 +89 +107 +45 +37 +79 +159 +141 +105 +10 +115 +69 +170 +25 +100 +80 +4 +85 +169 +106 +57 +116 +23 diff --git a/day10/src/main.rs b/day10/src/main.rs new file mode 100644 index 0000000..f0e909c --- /dev/null +++ b/day10/src/main.rs @@ -0,0 +1,52 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::string::String; + +fn main() { + let file = match File::open("input") { + Ok(file) => file, + Err(e) => panic!(e) + }; + + let input_buffer = BufReader::new(&file); + let lines = input_buffer.lines(); + + let mut adapters: Vec = lines + .map(Result::unwrap) + .map(|x: String| x.trim().to_string()) + .map(|x| x.parse::().unwrap()) + .collect(); + + adapters.sort(); + + let mut hist = [0, 0, 1]; + let mut prev = 0; + for adapter in adapters.iter() { + hist[(adapter - prev - 1) as usize] += 1; + prev = *adapter; + } + println!("{:?}", hist); + + println!("{}", hist[0] * hist[2]); + + adapters.reverse(); + + let mut path_counts = Vec::with_capacity(adapters.len()); + path_counts.push(1 as u64); + for _i in 1..adapters.len() { path_counts.push(0 as u64) }; + + for adapter_i in 1..adapters.len() { + let adapter = adapters[adapter_i]; + path_counts[adapter_i] = + if adapter_i >= 1 && adapters[adapter_i - 1] - adapter <= 3 { path_counts[adapter_i - 1] } else { 0 } + + if adapter_i >= 2 && adapters[adapter_i - 2] - adapter <= 3 { path_counts[adapter_i - 2] } else { 0 } + + if adapter_i >= 3 && adapters[adapter_i - 3] - adapter <= 3 { path_counts[adapter_i - 3] } else { 0 } + } + + let count = + if adapters[adapters.len() - 1] <= 3 { path_counts[adapters.len() - 1] } else { 0 } + + if adapters[adapters.len() - 2] <= 3 { path_counts[adapters.len() - 2] } else { 0 } + + if adapters[adapters.len() - 3] <= 3 { path_counts[adapters.len() - 3] } else { 0 }; + + println!("{}", count); +} diff --git a/day10/test b/day10/test new file mode 100644 index 0000000..be5c492 --- /dev/null +++ b/day10/test @@ -0,0 +1,31 @@ +28 +33 +18 +42 +31 +14 +46 +20 +48 +47 +24 +23 +49 +45 +19 +38 +39 +11 +1 +32 +25 +35 +8 +17 +7 +9 +4 +2 +34 +10 +3 \ No newline at end of file