From e8358006ad414c144a6260166f8d3322ca24eb20 Mon Sep 17 00:00:00 2001 From: jacopograndi Date: Wed, 3 Aug 2022 23:21:45 +0200 Subject: day03 --- 2020/.gitignore | 1 + 2020/day03/Cargo.lock | 7 ++ 2020/day03/Cargo.toml | 8 ++ 2020/day03/input0.txt | 323 +++++++++++++++++++++++++++++++++++++++++++++++++ 2020/day03/src/main.rs | 74 +++++++++++ 2020/day03/test0.txt | 11 ++ 6 files changed, 424 insertions(+) create mode 100644 2020/.gitignore create mode 100644 2020/day03/Cargo.lock create mode 100644 2020/day03/Cargo.toml create mode 100644 2020/day03/input0.txt create mode 100644 2020/day03/src/main.rs create mode 100644 2020/day03/test0.txt diff --git a/2020/.gitignore b/2020/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/2020/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/2020/day03/Cargo.lock b/2020/day03/Cargo.lock new file mode 100644 index 0000000..21c74cb --- /dev/null +++ b/2020/day03/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day03" +version = "0.1.0" diff --git a/2020/day03/Cargo.toml b/2020/day03/Cargo.toml new file mode 100644 index 0000000..2804bab --- /dev/null +++ b/2020/day03/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day03" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2020/day03/input0.txt b/2020/day03/input0.txt new file mode 100644 index 0000000..077fb5f --- /dev/null +++ b/2020/day03/input0.txt @@ -0,0 +1,323 @@ +...#.....#.......##......#..... +...#..................#........ +....##....#.......#............ +.........#.......#.......#..... +..#..............#.........#..# +.....#.........#....#....#....# +....##..........#.#.##......... +...#....##...#...#...#.#..#.... +...#.......###..........#...... +.........#.....#....#...#.#.... +.#...###..#..##..#.........###. +#.#...#..........###...#....#.. +#....#.#..#..........#.......#. +.#..#........##.#.............. +............#..#.#............# +.............#..........#...... +...#.......#...............#... +.#...#..#..#............#..#... +....##.##..................#.## +#......#...#..##....#.....#...# +#..#..........##....#...###.... +##......#.##.#......#..#......# +....#...#.......##.##...#.#..#. +##.#...#....#...#...#........#. +........#..#.....#....#.......# +.#......#......#..............# +.#.....#..#..#..#..#..#....#..# +.......#.....#................. +.#......#...#..#..#...#...#.... +.........#..#..#.........#..... +.....#.........#.#..........#.. +#......#....#....##....#.#.#... +................##.#........... +.....##.....#............#.#... +...........#...#.#..##...#..... +.......#....##.......#..#....#. +.##......##....#....####.##..#. +.....#.##.....#...#....##...... +.............#....#......#....# +#.#.#.###........#.#....#.#.... +.##...........#................ +#..#..#...##..##.##...#..#.#... +..#......##..#.#......#..#.#.#. +.....#..............#......#... +#.#..##.##...#............##... +.#......#.............#........ +........##....#......#..#...... +##.........##....#..........#.. +..#..#....#.........##....#..#. +........#..#..#........#...#... +#.........#......##.#...#.##... +.##.............#..###....#.##. +.##.#....#.......#............. +#..##.#.........#..##.#......## +....#..#....................... +.#.#.........#...............#. +....#......#.#..##..#...#.#..#. +#....##...##..#.......##.....## +....##...##...#....#.....#..#.. +.#......#.#.#.#......##..#..#.. +.....##..#..#.....#.....#...##. +....###................#..#.#.. +.....#..#..#.#..........#..#... +...#.....#............#........ +#.............#...#..#.....#..# +#........#.....#.#..#......#... +...#.##.....#.#..#.........#..# +.......##...#..#.#....##....... +..................#..##..#.#.#. +..#......#..#..#.....#...#.#... +.#.......#.....#.#....#.#...... +##..#.#....#.###..#...#.......# +.......................#....... +..###..........#..##.##.#...##. +.....#...#....###.........#..#. +..#.....#....###............... +....#.......#........#....#..#. +......#................#.#...## +#.....#.......#..#..........##. +#.#....##.........#.....#.#.... +#.#.#...#............####.##..# +.....#....####........#...#..#. +....##........#.#.............. +.#......#..#..##......#....#.## +..#....#.#........#..#....#.... +.#...#.##...#.#.....#.....#...# +..........#................###. +.....#..........##..#.......... +.....#..................#...#.. +#......##....#.#...#..#.......# +..#......##....#......#.#...#.. +###.#..###.#.#..#...#....#..... +#.....#.#...#.##...#........#.. +#..........................#... +.#.#.....#.#.#.......##.#.#.... +.#....#..##......#....#........ +.#.......#.##......#.#..#...... +............#.....#....##.##... +....##........##......#........ +....#......##....##.....#...... +..#.#.....#......#...#.#....... +.###.........#...#........#.... +......#.#...##.....##..##..#... +...#...#.#......#..##..#....... +.##....#.#........#.#.......... +#....#.#......#......#.#.#..... +#.....#.....#................## +...........#....#...#...#...... +..........##..##..#...##....... +.##......#.......#..#.#..##.... +..........##....#....#..#.#.... +...............#......#.....##. +.#...#....................#..#. +.............###............... +.####..............#...#....... +....#...#.#...#...#....#....... +.......#.#..................... +............................... +#..#.........##.......#.#.#.... +....##...#...........#......#.. +........##...#......#.......... +....#.#.....#..#......#........ +#..#................#..#.##.... +.#........#.......#.........##. +#...........#...#...#......#.#. +..#.#.#..........##.##...#...#. +..#...#.##...#.#...........#... +##...........##...##...##...... +....#....##...#......#..#.....# +#..#.#.#..#...#...#....#....... +............#.....#....#....#.# +....##.....#.........#......#.. +.....##.......#...#...#.###.... +...##......##..###.#.#....#.... +#.#.#.#..#.#.........#...#...## +..#..........#................. +....##....#.................... +###.#...............##...##.#.. +....#.......##.#..#.#.......... +............##..#.......##..... +#...#.........#..#..#..#...#... +..#......##..#.#...##.#.......# +......#................#...#... +......#..###............#.#.... +..#.#...###...#..#...#......##. +...#.##...##............#...... +#...##........#.#..#.......#... +#..#.....#..#.##............... +..#.....#.#....#.........#..... +.............#....#..#...#.##.. +..#.#.....................##.#. +........#.......#..#.#......... +##..............#.....#.......# +.#.##...###....#.....#..##.#... +#..#...#..#......#..........### +#...........#..#...#....#....#. +....#..#.......##......#......# +#...#.#...............##...#... +...##.#..##.......##..#........ +...........##..........#....... +..#....#..##...#......#.#...... +.#.#....#.#.#...#.#............ +.#.#..#...##.......#.#......... +...#...#.............#.######.. +##.#........###.......#....#.#. +.#....#.....#.#........#......# +..#.#.........#..........##.#.. +.#....#.#..............#......# +.....#..##.........#..##..#.... +........#..#....#.......#.....# +#.#.......#.....#.##.#...#....# +...#...##...#....#.....#....#.# +#..##....#..........#..#....... +.......#.#.....#...#.#.#.....## +#...#...#..#......##.#..#...... +...#.......#....#...........#.# +##.......#####.#.........#..#.. +....#.#...................##... +......#..##............#....... +#.........#....#####.#.#..#.#.. +..#......#.#.##............#... +..#...#.....#.#....#......#.... +.#...#....#....#.#.#......#.#.. +..#.##.....#..........#...#.#.. +.......#...#.............#...#. +.#.........#.....#.#........##. +#....#..#..........##.......##. +...#....#.#.........#.......### +......#....#.#......#.......#.. +.....#...#...#.#...##..#.#..... +#.........##..#...##..#.#....#. +...#......#.#......##.....#.... +.#####.....#.#.#.#...###.##.... +..#................#.#...#.#... +#.......##...#.........##..#... +..#.....#....##............#... +#............##...............# +..#..#.................#....... +...............#..#.......##... +..##..#....#...##..........#..# +#...###....##.#.......#.....#.. +..........#.........#..#......# +##....#.....#...##.......#..... +..#..#.......#................. +..#..##......#.........#......# +...........##.#..#......#.#..#. +..#...##...##......#...#...#.#. +.#..#.....#.........#.......... +#..##...#............#..#.#.... +..#...#...##.#........#....#.#. +......##..###.#....#........#.. +.....#..#....##...##..........# +................#.#.#.....#..#. +#.##...#......#.#..#.......###. +.......#.#..#..#......#..##..#. +.##...#...#....#....#.......#.. +......#..#....#.#.###.....#.#.# +#....#.#...#......#.#.....#..#. +.......#.#...#.#.#............# +#.....#..#...#................. +.....#..........#..#.#..#.#.... +.........#......#.#.........### +..#.###........#....##.#....... +.#.......#.#......#........#..# +............#........#.....#... +......#......#....#.#....#..... +.#.......#.....#.##.#..#...#..# +##.....#...#..........##..#...# +.#........#....#...#....##.#... +...#.#.......#.#....#.#...#...# +........#.#.....#.##...#.#.#... +...........#....#..#.........#. +......#.#..#..##...#.......#... +...#....#..#..#.##...........## +.#..#.#.#......#....##...#..... +......#..#........#...##....... +.............#...##.#.....#...# +....#...............##......#.# +.#...........#.........###.##.# +....##........##...#.##.....#.. +#......##........#...........#. +###.#.................#.....#.. +.....##..#.........#......##.#. +#.#.......##.#..#...#...#...... +...#.#..##.....#....##.....#.#. +...##..#...#........#.#..#..#.. +...........#....#...#...##..... +##.......#...#.#.##...##..#.#.. +#....#.#..##................... +.#...................#.#..#.... +#.....#..........#..#...#...#.. +...#..#............#.#......... +............#..##.....##......# +#....#.........#.#..#.......... +...#.#................#....#.#. +..#..#...#...#.#.#...#.#.#..... +..#.......#.............##..#.# +#........#.#.###.#...#..#.###.. +.......#......#..#.....####...# +..##....#..#................... +....##.#....#......#.#..#..#..# +#...........##...#.#.##..###... +##.##......#...........#....#.. +.#....#....#..#..#...##...#.... +...##.#.#......#............... +.....##.##...#...........#..... +....#...#.#.........##.#....#.# +#..#...........#......#........ +..#..#.....#....#.##.......#..# +..#.......##.....##.......#...# +.#.##.#..#...............#....# +.........#...........#......... +..........##......#.#.......... +..#........###....#..#...#...#. +....#.#...#.....#..#....#...... +..##...##...#..#..##......##..# +..#..#......#....#....#...#..## +...#...............#..#........ +....##..#...#......#........#.. +###.....##.......#............. +.#.#.##........##..#...#....... +.....###............#..#..#.... +.#....##.#...####........#..... +............#.#.....##....#.#.. +....#..........#...#........... +........#.#...#..##...........# +#.......#..#.......###...#....# +#....#..#......#.....#...##.#.. +..#.............#.#.###...##..# +.#.#....#...#.....#...#.......# +.##.#..#.........#..#......#... +#....#...#......#.....#.....#.. +...........#....#.......##...#. +#.#..##....#....#.#.......#.#.. +..............#.#..##.##....... +....#........#......#....#.#... +......#.....................#.. +#..##...##.....#.........#..... +#.....#.....#....#...#.....#... +........###...........#...#.... +............#.....#...##....#.. +.......#.......#...#.#...##.... +..#.#..#....#...#...#....#..... +..........#.#....#....###....#. +.##...#......###..#............ +...#...#........#....#....##... +##.....#.##...#.#...........#.# +..........#.#....#...##.#...#.. +..#....#.#...#...#....#.###.... +......#.##..#..#.........#.###. +#.#.#.....#.....##.......#..... +...#..#..#....#.#....#....#.... +##..#.#................#......# +.....#...#..#......#..####..... +.....##.....#....####......#... +..........##..###.#....#.....## +###...#.......#......##...#.... +.......##...#...#..#.##.#....## +.....##.....##...##.....#..#..# +......#.#.....#...#....#...#... +..##........#...#.............. +..#........#.##.........#...#.. +#....#....#................#... diff --git a/2020/day03/src/main.rs b/2020/day03/src/main.rs new file mode 100644 index 0000000..6e74f74 --- /dev/null +++ b/2020/day03/src/main.rs @@ -0,0 +1,74 @@ +fn slide(lines: Vec>, vx: i32, vy: i32) -> i32 { + let mut x: i32 = 0; + let mut y: i32 = 0; + let mut hits: i32 = 0; + loop { + if lines[y as usize][x as usize] == '#' { + hits += 1; + } + x += vx; + y += vy; + if y as usize >= lines.len() { + break; + } + if x as usize >= lines[y as usize].len() { + x -= lines[y as usize].len() as i32; + } + } + hits +} + +fn slide_everywhere(lines: Vec>) -> i32 { + let mut hits: i32 = 1; + for (vx, vy) in vec![(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] { + hits *= slide(lines.clone(), vx, vy); + } + hits +} + +fn main() { + let lines: Vec> = include_str!("../input0.txt") + .trim() + .split("\n") + .map(|s| s.chars().collect()) + .collect(); + let hits = slide(lines.clone(), 3, 1); + println!("hits: {}", hits); + let hits_everything = slide_everywhere(lines.clone()); + println!("hits on the five paths: {}", hits_everything); +} + +#[cfg(test)] +mod tests { + use crate::*; + + #[test] + fn small_input() { + let lines: Vec> = include_str!("../test0.txt") + .trim() + .split("\n") + .map(|s| s.chars().collect()) + .collect(); + assert_eq!(slide(lines, 3, 1), 7); + } + + #[test] + fn big_input() { + let lines: Vec> = include_str!("../input0.txt") + .trim() + .split("\n") + .map(|s| s.chars().collect()) + .collect(); + assert_eq!(slide(lines, 3, 1), 220); + } + + #[test] + fn small_input_all_slopes() { + let lines: Vec> = include_str!("../test0.txt") + .trim() + .split("\n") + .map(|s| s.chars().collect()) + .collect(); + assert_eq!(slide_everywhere(lines), 336); + } +} diff --git a/2020/day03/test0.txt b/2020/day03/test0.txt new file mode 100644 index 0000000..7e88cdc --- /dev/null +++ b/2020/day03/test0.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# -- cgit v1.2.3-54-g00ecf