Day 6 in Rust

This commit is contained in:
dece 2021-12-06 14:11:01 +01:00
parent 06eb7c1f08
commit e344804319
4 changed files with 43 additions and 0 deletions

1
2021/rust/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

7
2021/rust/Cargo.lock generated Normal file
View file

@ -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"

8
2021/rust/Cargo.toml Normal file
View file

@ -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]

27
2021/rust/src/bin/day6.rs Normal file
View file

@ -0,0 +1,27 @@
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::<usize>(c).unwrap()] += 1;
}
for _ in 0..80 {
step(&mut data);
}
println!("{}", data.iter().sum::<u64>());
for _ in 80..256 {
step(&mut data);
}
println!("{}", data.iter().sum::<u64>());
}
fn step(data: &mut Vec<u64>) {
let d0 = data[0];
for d in 0..=7 {
data[d] = data[d + 1];
}
data[6] += d0;
data[8] = d0;
}