From d5aa1ddc7482f0586e7877cd0fa2f04fbede02fb Mon Sep 17 00:00:00 2001 From: Johannes Date: Sat, 22 Dec 2018 15:07:46 +0100 Subject: [PATCH] day22 part1 --- src/main.rs | 2 +- src/tasks/day22.rs | 32 ++++++++++++++++++++++++++++++++ src/tasks/mod.rs | 1 + 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/tasks/day22.rs diff --git a/src/main.rs b/src/main.rs index 8824b36..1a88b12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ fn main() { - aoc_2018::tasks::day20::task1(); + aoc_2018::tasks::day22::task1(); // aoc_2018::tasks::day15::task2(); } diff --git a/src/tasks/day22.rs b/src/tasks/day22.rs new file mode 100644 index 0000000..03a88fe --- /dev/null +++ b/src/tasks/day22.rs @@ -0,0 +1,32 @@ +pub fn task1() { + let depth: usize = 3879; + //let target: (usize, usize) = (8, 713); + const TARGET_X: usize = 8; + const TARGET_Y: usize = 713; + + let mut map: Vec> = Vec::with_capacity(TARGET_X + 1); + let mut inner_vec = Vec::with_capacity(TARGET_Y + 1); + inner_vec.resize(TARGET_Y + 1, 0); + map.resize(TARGET_X + 1, inner_vec); + + for x in 0..=TARGET_X { + for y in 0..=TARGET_Y { + let geo_index = match (x, y) { + (0, 0) => 0, + (TARGET_X, TARGET_Y) => 0, + (x, 0) => x * 16807, + (0, y) => y * 48271, + (x, y) => map[x - 1][y] * map[x][y - 1], + }; + let erosion_index = (geo_index + depth) % 20183; + map[x][y] = erosion_index; + } + } + + // println!("{:?}", map); + + let result: usize = (0..=TARGET_X) + .map(|x| (0..=TARGET_Y).map(|y| map[x][y] % 3).sum::()) + .sum(); + println!("Sum of erosion indexes: {}", result); +} diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index f69e2c5..713556c 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -14,3 +14,4 @@ pub mod day13; pub mod day14; pub mod day15; pub mod day20; +pub mod day22; \ No newline at end of file