From 2599a5686198ed6af783c7a9a1a67ab71750f156 Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 6 Dec 2021 14:11:01 +0100 Subject: [PATCH] Day 6 in Rust --- 2021/rust/.gitignore | 1 + 2021/rust/Cargo.lock | 7 +++++++ 2021/rust/Cargo.toml | 8 ++++++++ 2021/rust/src/bin/day6.rs | 30 ++++++++++++++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 2021/rust/.gitignore create mode 100644 2021/rust/Cargo.lock create mode 100644 2021/rust/Cargo.toml create mode 100644 2021/rust/src/bin/day6.rs diff --git a/2021/rust/.gitignore b/2021/rust/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/2021/rust/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2021/rust/Cargo.lock b/2021/rust/Cargo.lock new file mode 100644 index 0000000..b21cc6a --- /dev/null +++ b/2021/rust/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "rust" +version = "0.1.0" diff --git a/2021/rust/Cargo.toml b/2021/rust/Cargo.toml new file mode 100644 index 0000000..1ec6963 --- /dev/null +++ b/2021/rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2021/rust/src/bin/day6.rs b/2021/rust/src/bin/day6.rs new file mode 100644 index 0000000..bb48799 --- /dev/null +++ b/2021/rust/src/bin/day6.rs @@ -0,0 +1,30 @@ +use std::io; + +fn main() { + let mut text = String::new(); + io::stdin().read_line(&mut text).unwrap(); + let mut data = vec![0u64; 9]; + for c in text.strip_suffix("\n").unwrap().split(",") { + data[str::parse::(c).unwrap()] += 1; + } + for _ in 0..80 { + step(&mut data); + } + println!("{}", data.iter().sum::()); + for _ in 80..256 { + step(&mut data); + } + println!("{}", data.iter().sum::()); +} + +fn step(data: &mut Vec) { + let d0 = data[0]; + let mut prev = data[8]; + for d in (0..=7).rev() { + let temp = data[d]; + data[d] = prev; + prev = temp; + } + data[6] += d0; + data[8] = d0; +}