This commit is contained in:
parent
2c413315ad
commit
773fdbf91f
7
rust/day6/Cargo.lock
generated
Normal file
7
rust/day6/Cargo.lock
generated
Normal file
@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
8
rust/day6/Cargo.toml
Normal file
8
rust/day6/Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day6"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
57
rust/day6/src/main.rs
Normal file
57
rust/day6/src/main.rs
Normal file
@ -0,0 +1,57 @@
|
||||
use std::fs;
|
||||
use std::io::BufRead;
|
||||
|
||||
static PATH: &'static str = "../../inputs/input-day6";
|
||||
|
||||
fn remove_whitespace(s: &str) -> String {
|
||||
return s.split_whitespace().collect();
|
||||
}
|
||||
|
||||
fn load_file() -> Result<Vec<i64>, std::io::Error> {
|
||||
let file = fs::File::open(PATH)?;
|
||||
let reader = std::io::BufReader::new(file);
|
||||
let res: Vec<i64> = reader.split(b',').map(|p| {
|
||||
let p = p.unwrap();
|
||||
let v = String::from_utf8_lossy(&p);
|
||||
return remove_whitespace(&v).parse::<i64>().unwrap();
|
||||
}).collect();
|
||||
return Ok(res);
|
||||
}
|
||||
|
||||
fn run_day(v: &mut Vec<i64>) {
|
||||
let len = v.len();
|
||||
|
||||
for i in 0..len {
|
||||
if v[i] == 0 {
|
||||
v[i] = 6;
|
||||
v.push(8);
|
||||
} else {
|
||||
v[i] -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn part1() {
|
||||
let mut x = load_file().unwrap();
|
||||
|
||||
for _ in 0..80 {
|
||||
run_day(&mut x);
|
||||
}
|
||||
|
||||
println!("total: {}", x.len());
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let mut x = load_file().unwrap();
|
||||
|
||||
for _ in 0..256 {
|
||||
run_day(&mut x);
|
||||
}
|
||||
|
||||
println!("total: {}", x.len());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
part1();
|
||||
part2();
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user