80 lines
2.2 KiB
Rust
80 lines
2.2 KiB
Rust
|
|
struct PatternWithOutcome {
|
|
patterns: Vec<bool>,
|
|
outcome: bool
|
|
}
|
|
|
|
impl PatternWithOutcome {
|
|
pub fn new(patterns: Vec<bool>, pattern: bool) -> Self {
|
|
PatternWithOutcome {
|
|
patterns,
|
|
outcome: pattern
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let init_state: Vec<bool> = "#.#.#..##.#....#.#.##..##.##..#..#...##....###..#......###.#..#.....#.###.#...#####.####...#####.#.#".chars().map(|c| c == '#').collect();
|
|
|
|
let lines: Vec<&str> = include_str!("../input").lines().collect();
|
|
|
|
let mut patterns_and_outcomes: Vec<PatternWithOutcome> = Vec::new();
|
|
|
|
for line in lines {
|
|
let temp: Vec<&str>= line.split(" => ").collect();
|
|
|
|
let temp = PatternWithOutcome::new(temp[0].chars().map(|c| c == '#').collect(), temp[1].chars().collect::<Vec<char>>()[0] == '#');
|
|
patterns_and_outcomes.push(temp)
|
|
}
|
|
|
|
let mut laststate= init_state.clone();
|
|
|
|
for i in 1..201 {
|
|
laststate = advance_generation(laststate, &patterns_and_outcomes);
|
|
|
|
let mut result = 0;
|
|
|
|
for j in (i*2)..laststate.len() {
|
|
if laststate[j] { result += (j-(i*2)) }
|
|
}
|
|
|
|
//Part 1
|
|
if i == 20 {
|
|
println!("{}", result);
|
|
}
|
|
|
|
//Really dumb solution for part 2
|
|
if i == 200 {
|
|
println!("{}", result as u64 + 25 as u64 *49999999800 as u64);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn advance_generation(mut state: Vec<bool>, patterns: &Vec<PatternWithOutcome>) -> Vec<bool>{
|
|
let mut new_state: Vec<bool> = Vec::new();
|
|
let mut position = 2;
|
|
|
|
//Pad
|
|
state.insert(0, false);
|
|
state.insert(0, false);
|
|
state.insert(0, false);
|
|
state.insert(0, false);
|
|
state.push(false);
|
|
state.push(false);
|
|
state.push(false);
|
|
state.push(false);
|
|
|
|
while position < (state.len()-2) {
|
|
for pattern in patterns {
|
|
if (pattern.patterns[0] == state[position-2]) && (pattern.patterns[1] == state[position-1]) && (pattern.patterns[2] == state[position]) && (pattern.patterns[3] == state[position+1]) && (pattern.patterns[4] == state[position+2]) {
|
|
new_state.push(pattern.outcome);
|
|
break;
|
|
}
|
|
}
|
|
|
|
position+=1;
|
|
}
|
|
|
|
new_state
|
|
} |