diff --git a/day12/Cargo.lock b/day12/Cargo.lock new file mode 100644 index 0000000..411c289 --- /dev/null +++ b/day12/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day12" +version = "0.1.0" + diff --git a/day12/Cargo.toml b/day12/Cargo.toml new file mode 100644 index 0000000..a048ddc --- /dev/null +++ b/day12/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day12" +version = "0.1.0" +authors = ["attila "] + +[dependencies] diff --git a/day12/input b/day12/input new file mode 100644 index 0000000..ec77fa3 --- /dev/null +++ b/day12/input @@ -0,0 +1,32 @@ +..#.. => . +#...# => . +.#... => # +#.##. => . +..#.# => # +#.#.# => . +###.. => # +###.# => # +..... => . +....# => . +.##.. => # +##### => . +####. => . +..##. => . +##.#. => # +.#..# => # +##..# => . +.##.# => . +.#### => # +..### => . +...## => # +#..## => # +#.... => . +##.## => . +#.#.. => . +##... => . +.#.## => # +.###. => # +...#. => . +#.### => . +#..#. => # +.#.#. => . \ No newline at end of file diff --git a/day12/src/main.rs b/day12/src/main.rs new file mode 100644 index 0000000..90104a7 --- /dev/null +++ b/day12/src/main.rs @@ -0,0 +1,80 @@ + +struct PatternWithOutcome { + patterns: Vec, + outcome: bool +} + +impl PatternWithOutcome { + pub fn new(patterns: Vec, pattern: bool) -> Self { + PatternWithOutcome { + patterns, + outcome: pattern + } + } +} + + +fn main() { + let init_state: Vec = "#.#.#..##.#....#.#.##..##.##..#..#...##....###..#......###.#..#.....#.###.#...#####.####...#####.#.#".chars().map(|c| c == '#').collect(); + + let lines: Vec<&str> = include_str!("../input").lines().collect(); + + let mut patterns_and_outcomes: Vec = 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::>()[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, patterns: &Vec) -> Vec{ + let mut new_state: Vec = 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 +} \ No newline at end of file