From 772a3db1831d4ccfed172e659b92db47cba7ee70 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Wed, 8 Dec 2021 20:11:07 +0100 Subject: Add day6, slowly --- day6/src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 day6/src/main.rs (limited to 'day6/src') diff --git a/day6/src/main.rs b/day6/src/main.rs new file mode 100644 index 0000000..46a148c --- /dev/null +++ b/day6/src/main.rs @@ -0,0 +1,36 @@ +use std::fs::File; +use std::io::{BufRead, BufReader}; + +fn main() -> std::io::Result<()> { + let file = File::open("input.txt")?; + let mut reader = BufReader::new(file); + let mut buffer = String::new(); + + reader.read_line(&mut buffer)?; + + let mut lanternfishes: Vec<_> = buffer + .trim() + .split(',') + .map(|x| x.parse::().unwrap()) + .collect::>(); + + for _ in 0..80 { + let mut n = 0; + for lanternfish in lanternfishes.iter_mut() { + if *lanternfish == 0 { + *lanternfish = 6; + n += 1; + } else { + *lanternfish -= 1; + } + } + + for _ in 0..n { + lanternfishes.push(8); + } + } + + println!("{}", lanternfishes.len()); + + Ok(()) +} -- cgit v1.2.3-18-g5258