blob: f1723e1ee8a6c76fea86f29d4880afa8d4a87627 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
use std::io::{BufRead};
/*
* This is an embarassingly naïve solution based on poor reading of
* the problem and thinking that the actual example printout had to
* be reproduced. Part 2 is solved in a much saner way.
*/
pub fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() != 2 { panic!("Need exactly 1 argument"); }
let num_days: usize = args[1].parse().expect("Need number of days as argument");
let mut stdin = std::io::stdin();
let mut handle = stdin.lock();
let input: String = {
let mut buf = String::new();
handle.read_line(&mut buf).unwrap();
String::from(buf.trim_end())
};
let mut fishes: Vec<u64> = input.split(',').map(|w| w.parse().expect("Malformed input")).collect();
for i in 0..num_days {
let mut new_fish_count: usize = 0;
for fish in (&mut fishes).into_iter() {
if *fish == 0 {
new_fish_count += 1;
*fish = 6;
}
else { *fish -= 1; }
}
fishes.extend(std::iter::repeat(8).take(new_fish_count));
/*
print!("After day {:>width$}: ", i, width = format!("{}", num_days).len());
for fish in (& fishes).into_iter() {
print!("{} ", *fish);
}
println!("");
*/
}
println!("After {} days there are {} fishies", num_days, fishes.len());
}
|