day12 done, but the solution for part2 is a bit hardcoded, probably should fix that at some point
This commit is contained in:
parent
c0b3f6dc47
commit
d9d25fc8fe
|
@ -0,0 +1,4 @@
|
||||||
|
[[package]]
|
||||||
|
name = "day12"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "day12"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["attila <daf276@gmx.de>"]
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,32 @@
|
||||||
|
..#.. => .
|
||||||
|
#...# => .
|
||||||
|
.#... => #
|
||||||
|
#.##. => .
|
||||||
|
..#.# => #
|
||||||
|
#.#.# => .
|
||||||
|
###.. => #
|
||||||
|
###.# => #
|
||||||
|
..... => .
|
||||||
|
....# => .
|
||||||
|
.##.. => #
|
||||||
|
##### => .
|
||||||
|
####. => .
|
||||||
|
..##. => .
|
||||||
|
##.#. => #
|
||||||
|
.#..# => #
|
||||||
|
##..# => .
|
||||||
|
.##.# => .
|
||||||
|
.#### => #
|
||||||
|
..### => .
|
||||||
|
...## => #
|
||||||
|
#..## => #
|
||||||
|
#.... => .
|
||||||
|
##.## => .
|
||||||
|
#.#.. => .
|
||||||
|
##... => .
|
||||||
|
.#.## => #
|
||||||
|
.###. => #
|
||||||
|
...#. => .
|
||||||
|
#.### => .
|
||||||
|
#..#. => #
|
||||||
|
.#.#. => .
|
|
@ -0,0 +1,80 @@
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user