day12 part 1

This commit is contained in:
Johannes Schaefer
2018-12-14 14:09:57 +01:00
parent 54103e8fa6
commit 2fa6c40f33
4 changed files with 91 additions and 2 deletions

34
input/day12.txt Normal file
View File

@@ -0,0 +1,34 @@
initial state: ..##.#######...##.###...#..#.#.#..#.##.#.##....####..........#..#.######..####.#.#..###.##..##..#..#
#..#. => .
..#.. => .
..#.# => #
##.#. => .
.#... => #
#.... => .
##### => #
.#.## => .
#.#.. => .
#.### => #
.##.. => #
##... => .
#...# => #
####. => #
#.#.# => .
#..## => .
.#### => .
...## => .
..### => #
.#..# => .
##..# => #
.#.#. => .
..##. => .
###.. => .
###.# => #
#.##. => #
..... => .
.##.# => #
....# => .
##.## => #
...#. => #
.###. => .

View File

@@ -1,4 +1,4 @@
fn main() {
// aoc_2018::tasks::day11::task1();
aoc_2018::tasks::day11::task2_fast();
aoc_2018::tasks::day12::task1();
// aoc_2018::tasks::day11::task2_fast();
}

54
src/tasks/day12.rs Normal file
View File

@@ -0,0 +1,54 @@
use crate::utils;
use std::collections::HashMap;
use std::ops::Add;
pub fn task1() {
let input = utils::read_file("input/day12.txt");
let mut input = input.lines();
let num_generations = 20;
let mut state: Vec<char> = "."
.repeat(num_generations)
.add(&input.next().unwrap()[15..])
.add(&".".repeat(num_generations + 2))
.chars()
.collect();
let mut transformations: HashMap<String, char> = HashMap::new();
input.next();
for line in input {
let key = line.split(" => ").nth(0).unwrap();
transformations.insert(key.to_string(), line.chars().last().unwrap());
}
for _ in 0..num_generations {
println!("{}", state.iter().collect::<String>());
let mut new_state = state.clone();
for (i, c) in new_state[2..state.len() - 2].iter_mut().enumerate() {
let next = transformations.get(&state[i..i + 5].iter().collect::<String>());
if let Some(cc) = next {
*c = *cc;
} else {
*c = '.';
}
}
state = new_state;
}
println!("{}", state.iter().collect::<String>());
let sum: isize = state
.iter()
.enumerate()
.map(|(i, c)| {
if *c == '#' {
i as isize - (num_generations as isize)
} else {
0
}
})
.sum();
println!("Result: {}", sum);
}

View File

@@ -9,3 +9,4 @@ pub mod day08;
pub mod day09;
pub mod day10;
pub mod day11;
pub mod day12;