add initial data
This commit is contained in:
commit
c4e841b4bc
43 files changed
+5284
No files matched your search
Generated
+7
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aoc06"
|
||||
version = "0.1.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "aoc06"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1 @@
|
||||
1,3,4,1,1,1,1,1,1,1,1,2,2,1,4,2,4,1,1,1,1,1,5,4,1,1,2,1,1,1,1,4,1,1,1,4,4,1,1,1,1,1,1,1,2,4,1,3,1,1,2,1,2,1,1,4,1,1,1,4,3,1,3,1,5,1,1,3,4,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,1,1,5,2,5,5,3,2,1,5,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,1,1,1,1,5,1,1,1,1,1,4,1,1,1,1,1,3,1,1,1,1,1,1,1,1,1,1,1,3,1,2,4,1,5,5,1,1,5,3,4,4,4,1,1,1,2,1,1,1,1,1,1,2,1,1,1,1,1,1,5,3,1,4,1,1,2,2,1,2,2,5,1,1,1,2,1,1,1,1,3,4,5,1,2,1,1,1,1,1,5,2,1,1,1,1,1,1,5,1,1,1,1,1,1,1,5,1,4,1,5,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,5,4,5,1,1,1,1,1,1,1,5,1,1,3,1,1,1,3,1,4,2,1,5,1,3,5,5,2,1,3,1,1,1,1,1,3,1,3,1,1,2,4,3,1,4,2,2,1,1,1,1,1,1,1,5,2,1,1,1,2
|
||||
@@ -0,0 +1,58 @@
|
||||
const SIZE: usize = 10;
|
||||
const REPRODUCTIVE_CYCLE: usize = 6;
|
||||
const SPAWN_GENERATION: usize = 8;
|
||||
const OVERFLOW: usize = 9;
|
||||
|
||||
struct Population {
|
||||
data: [i128; SIZE]
|
||||
|
||||
}
|
||||
|
||||
impl Population {
|
||||
fn get_new_spawn(&self) -> i128 {
|
||||
self.data[OVERFLOW]
|
||||
}
|
||||
|
||||
fn reset_new_spawn(&mut self) {
|
||||
self.data[OVERFLOW] = 0
|
||||
}
|
||||
|
||||
fn iterate(&mut self, generations: i32) {
|
||||
for _i in 0..generations {
|
||||
self.data.rotate_left(1);
|
||||
self.data[REPRODUCTIVE_CYCLE] += self.get_new_spawn();
|
||||
self.data[SPAWN_GENERATION] = self.get_new_spawn();
|
||||
self.reset_new_spawn();
|
||||
}
|
||||
}
|
||||
|
||||
fn calc_sum(&self) -> i128 {
|
||||
let mut result = 0;
|
||||
for item in self.data {
|
||||
result += item;
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut data = load_data("data.txt");
|
||||
data.iterate(256);
|
||||
let sum = data.calc_sum();
|
||||
println!("lanternfish: {}", sum);
|
||||
}
|
||||
|
||||
fn load_data(filename: &str) -> Population {
|
||||
let content = std::fs::read_to_string(filename).unwrap();
|
||||
let mut data: [i128; SIZE] = [0; SIZE];
|
||||
for elements in content.split(",") {
|
||||
println!("{:?}", elements);
|
||||
let value = elements.parse::<i32>();
|
||||
let x = match value {
|
||||
Ok(x) => x as usize,
|
||||
Err(_e) => break
|
||||
};
|
||||
data[x] += 1;
|
||||
}
|
||||
Population{data}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
3,4,3,1,2
|
||||
Reference in new issue
Block a user