diff --git a/rust/.vscode/settings.json b/rust/.vscode/settings.json new file mode 100644 index 0000000..875c867 --- /dev/null +++ b/rust/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rust-analyzer.showUnlinkedFileNotification": false +} \ No newline at end of file diff --git a/rust/Cargo.lock b/rust/Cargo.lock index a0cd482..d4001db 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "20.1.2" +version = "20.3.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index e85cf3a..568dada 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "20.1.2" +version = "20.3.2" edition = "2021" resolver = "2" @@ -30,3 +30,7 @@ path = "src/day01/main.rs" [[bin]] name = "day02" path = "src/day02/main.rs" + +[[bin]] +name = "day03" +path = "src/day03/main.rs" diff --git a/rust/src/day02/main.rs b/rust/src/day02/main.rs index 3fec5f9..5a1af1b 100644 --- a/rust/src/day02/main.rs +++ b/rust/src/day02/main.rs @@ -21,7 +21,7 @@ impl FromStr for Password { type Err = ParseError; fn from_str(line: &str) -> Result { - let parts = line.split(" ").collect::>(); + let parts = line.split(' ').collect::>(); if parts.len() != 3 { return Err(ParseError(line.into())); @@ -30,7 +30,7 @@ impl FromStr for Password { let first_num = nums_iter.next().unwrap(); let second_num = nums_iter.next().unwrap(); - let letter = parts[1].chars().nth(0).unwrap(); + let letter = parts[1].chars().next().unwrap(); let password = parts[2].into(); Ok(Password { diff --git a/rust/src/day03/input.txt b/rust/src/day03/input.txt new file mode 100644 index 0000000..08b6876 --- /dev/null +++ b/rust/src/day03/input.txt @@ -0,0 +1,323 @@ +...#...#..#....#..#...#..##..#. +.#..#.....#.#............###... +.#...###....#.............##..# +...##...##....#.....##..#.##... +.....###.#.###..##.#.##.......# +#...##.....#..........#..#.#.#. +......##.......##..#....#.#.... +....#.###.##..#.#..##.##....#.# +.......#.......###.#.#.##.....# +.........#.#....#..........#.#. +.#...##.....##.........#..#.... +.##....#.#.#...##......#....... +##.#.#..#....#....#....#...#.#. +##....#.#..##......#....##...#. +....#..#..##..#.###.......#.#.. +.....##....###...........#.#.## +#.....##.........#....##......# +........###.#..#....#....#..... +...#.......#.##..#.###......#.. +...............#..#....#.##.... +..#..###..#.#..#.........##..#. +####..#..####.................. +#...####...#.......#.#.#...#... +......###.....#......#..#..#... +#...#.....##.....#.#..##...#.#. +#...........##.......#......... +.#..#.........#.#..##....#..... +........##...#................# +........#.###.#.###.#.#.##..##. +.#....##.....#...##.#..#.#..... +..#..#.....###....##.#....#.#.# +#......##.##...##..#.........#. +#..#..#.....#.....#.........#.. +#....#.#...###.........#...#... +.#.#.....##......#.#......#.... +..##......##...#.#.#.#......... +..#......#.....##.###.#.#..#... +....#..#.......#..#..#.....#... +.#.#.....#...#..........#...... +#.#..#...........#.#.##.#...#.# +..#.#....###...#...#.....#.#... +....##.#.###....####.......#... +.....##....#.......#..#..#....# +...##..#.#.#.#......#......#... +...##...#....#...#......###...# +........#..#.#.....#.###....... +..#..##.#....#.#.........#...#. +.....#.####....#.##.........#.. +......#...#...#.....#......###. +.##.....#....#..#.#....#.....#. +...........#...#....##..#...#.. +.....#....#.....#...##..#...#.# +.#...#.........#.......#...#..# +...#..#...#........#......#.... +..#..#####.#.....#.#....#...#.# +...#.......#.#....#...##..#..#. +####..#.#.###.#.#.............. +.##........#...#.#....#..#..... +..#..............#.#..##...#.## +.###.#.....#.#.....##.#......## +....###.....#...#...#.#..#..... +....###.#.##.......#....#...#.. +#..#...#......##..#.....#.#...# +....#.#.........#.............. +#.##.##...#..#.#.#.....#...#.## +#...#...#......#...........##.. +#.#.#......#............#.#.... +.#.#..######...#.#.........#.## +..#.#..#...#......#............ +....#.....#......##..#.....#... +.##............#....##..#...... +.#.#.#...#.##.............###.# +#.#...#...#.....#....#.#.#..... +........#..#......##.##.#.....# +.....#.....#.#####...#....#.... +.#...#......#.........#.#...... +...#...#..##.....##....#..#.... +....#....##..#.........#....... +..#........##..#.#........#.... +...#...##...........#...#....#. +.....##.........#..#....#..#.#. +#..#....##..#...##.....#..##.#. +..#.#.#.#...#...#.....#.#....#. +.......#.###...#.#.......#.#... +....#..#..#.###.#.....###..#.#. +.#..##......#..#..#....#.####.. +..##...........#...#.........#. +......#..#...#..........#...... +....#..........#......##...#... +....#..#.##........#.#...##.#.. +#.##......#........##.#...#...# +#..#....#.....###........##.... +...........##.....##..#....#.## +..#....#..#..#......#.#.....#.. +#....#.##....#.....##.......#.. +.#.....#.#..............#.##..# +.#..#..#...#...#....#.#.....#.. +...###...##.#...#..#........#.. +#...#.##.#.....#.#....#..#..... +#.....###.#.......#.#..#.#..##. +....#..#..##.......###.#...#... +.#...####...............#.....# +.#.##.#.....#.....#.#......##.# +#...........#.##....###.##....# +...............#..........#.... +.....#..#.##.###.#............. +...##.............#.....#.#..#. +....#.#...#.#..#..#..#....#.... +..#.......#..........#...#...#. +...............#.#.#...###....# +....#...#.##....#..##....#..... +........#.#.##.........##.##.## +#.....###.......#.#....#..#..## +.#..#...#......#.#..##.......#. +#.....#.#........#.##..#..#.... +.###..##.#.......#......###.... +.#...###.....#.....#....###...# +........##.##......#.#....#...# +.#....#..#.........#..##...##.. +.......#.......##.#..#..##..... +#..##..##......#.#......#.##... +..#..###..#...#....#..#...#.... +#.............#.####.........## +..#..................#...#..#.. +..#......#........##.......#.#. +.#.#.#.#..###.....#....#.#..... +...#.##.###.......#....#....... +................##...#.....#... +..#.###.#...#.####....#..#..#.. +..#....###....##..#.#.........# +.#..#.#.....#........#....##... +.....#..#......#..#..##.#.#.... +.#..#.........##....##......#.. +.....#.#...#...#.#...#.#...#.#. +..#..#...#...#...##.#..###..... +..#..##......#..##.#...##...... +.......#..##....##.#......#..#. +..#......#.#.....#.##....##.... +..#....#......#......##........ +....##.#.#....#.......#.##..... +#.....#...###....#....#...#.... +............#.#..#...#...#..#.. +..##.............##....#....... +.#.......#.##.#......#....##... +...##............#....#..#...#. +.##.####.....#.#..###.#....#.## +....##.#........#..#...#....... +...#...###.##...........##..#.. +..##..##....#...#..#..........# +..#.........#.#...##..........# +.......##....#.#...##.....#..#. +.............#.....#.#.......#. +#.......#..##..##...##.#....... +..............#.....#.#..#...## +........##..#.....#...#...#.#.. +###.#.................#........ +...#........#...#.#######..#..# +...#.##...##.#.#..######...#... +#.......#..#....#..#.##.....#.. +#..#....##....#.##.......#....# +#...#..#.#.#...#..#.##..#...... +....#..##....#..#.#...........# +.##..#.#.............###....... +#....##......#..#..#.....###... +..#..........#...###.#......... +.####......#....#......#.#....# +..#....#.#.#......#....#....... +.....#.....#....#....#####....# +.##..........#...#.###....#.... +....##.....##......#...#.#..... +.#...#...#..#.#.#...#####...... +...#.##..####.##.##.......##... +............#.......#.......... +.#..##.#..#####........#..#...# +#......##..##..##.........##... +....#....#.............#.#....# +###..#.....#.....#.#...#..#.### +#...#.......##......#....#.#.#. +...#......#..#...#....#...###.# +....#....##.......#....#......# +............#......##.##.....#. +...#.........#......#....##..## +.....##....##...#..###...#..#.. +.......##.#..........#.##.##... +....##...........#.#..#..#.##.# +#...#..##.##.#....#....#.#..... +...##.#.....#..#..#..###....##. +#.##.#..#..#.#.............#... +..#.#.............###.....#.... +...#..#....#..#.....#.#..#..#.. +...#.....##.#...........#..##.# +.........#.#.##..#..#.#...#.... +...#..##..#...#...###.##.#..#.. +.#..##...##......##..##........ +......##....##.#.##.#.#........ +...#..................#.....#.. +.##................#.#..#..###. +.##.##.....#................#.. +.....#.#..........#...#..#.#..# +.............#......#..#.#..#.. +...#...##..#........#....#..... +#......#........##.##...##..... +##..#..##....#...#............# +..##..##.##....##..##........#. +...#....#.#.#.#....#.#...##.... +....#...##..##.#.##...#..#...#. +#..#....##.#.....#.......#...## +##.#....#.............#..#..... +.##..#..#.#.....#.......#.#..#. +.......#..#...##...#...###..#.. +..........#...#.#..##.....#...# +..#....#...........#####....#.. +#....#..#.......##............. +.........##..#####.......##.... +#..#..........#.....###...#..#. +.#.#.#..#...#.......##...#####. +.....#....#.###...#.......#.... +#.#.....##...###....###....#... +.#.....#..#.#.#........#...#... +.##.#.#.#......#....###....#... +.#..##..####......###......#... +......#.#.#.#.#...#...####.##.. +.#........##..#.....#....#....# +.....###......##..#....#....... +#.#.##...#.#......###.......... +........#.#...#..#......#....#. +..##...##.........#.......#.#.. +..#.##....#...##.....#.###..... +.........#..#.#....#....#.#.##. +#.........#......#..#.......#.. +...#...##.......#.........#.... +............#......#........... +##.....#.....#.#...#.....#..... +..#.#...#..#...#.#...........#. +#.#.#..#..#...##.#...#.#.....#. +.#..###.#..##.#.....#.....#.... +##....##....#.......##..##..... +.#..#...........###..........#. +.#..#..#..........###..#....... +#..###......#............##...# +#......#........#..#..#..#.#... +.......#.###...#.##............ +.##....#.......#.#...##.....#.# +....#..#.#.......#.#........... +##....#.###.#....#.#..##.#....# +..#..#..#....#...#........##... +...#...##....#..#.#...#..#..... +......#..#......#....#.......#. +#.#..............#...###...#..# +...#....#..#..........#.#...#.. +#.....##..##.....#........#.... +.#...##..#.#..............#.... +##.#....#..##...#..#.####.#..#. +.....#.......#.#.#.#..#.....### +...#.##....#.#........##....... +#...#.#...#.#..###..##.##...#.# +###..............#.#.###....... +...###..#.#..#....##...###.#... +......##...........#...#..#...# +.#..#.........##.......#..#...# +.#.......###......##...#...#... +.#......##...#........#.......# +.#..#.....#.........#.#........ +#...#.#.....#...#..##.........# +......##.#......##.#..##.#..... +...............#.#..#....#....# +#....#..#..#..#.#.....##...##.. +#.#......#.###......#..#...#### +.#.#..#...#...#.#..#.##.##.#.#. +.....#.#...###...#.#.....##.... +...#..#.#..........##.#....#.#. +...#..#.#.##.....###.##.#....#. +..........#..###......#..#.#... +###.....#..###..#...#..###.#... +..#..#.....##.#.#..###.......#. +....#....##........##.......... +.......#..........#...#......#. +.#........#.#.#.#.#.......#.... +.#..#.......##..##....#.#...#.. +.#.#.#.......#..#.............. +#.#....#.#...#.#.#.....#.#...## +.....#..........##..#.......#.. +.##......#.#....#.#.......#.... +..#.##....#.##.#...#........... +...##......##..##.............# +..........##.#.#..#..........#. +.##....#..#..#.#....##.#...#.#. +...........#....#.....#.#..#... +.#.....#....##..#.........#.... +.....#.....#...#....#...#.###.# +..#....#....#.....#...#......#. +.....##..#.............#...#... +........#..#.......#.#.......#. +#...###..#.##.#...###...##..##. +....##..#.......#...#.#........ +.#...#.#.##....####........#..# +.#...#.#.####.##.#............. +#..##...#....#...#.#.#.#.##..#. +.#.......#........#.....###.... +#.#.....#....#..#....#..#....#. +...#..#...#.....#.........##... +.#....#......###...#....#.#.#.. +#.#........#......#...#....##.. +.....#..#......#..#..#......#.. +.#.....#..#.##.#.#.#...#......# +##........#..#.#..#...#.####... +..........##....#.#..#.#....#.. +#.##..#..#....#..#....##..#.#.# +..#......#.......#...##..#..... +##...#.........#......#......#. +.#.....................#..#.##. +.#.......#........#.#.#..##.#.. +..#..........#........#..##.#.. +.#...#...#.........##.#.#.#.... +....#....#.###.#....###....#.## +....##......##........##.#.##.. +....#.#......#.##.#...#.##..... +....#....#..#.#..###.#.#....... +....#......#..#.#.......#..##.. +.....#..#.#.##.##..##.....#.#.. +...#....................##..... +#.....#...##...#.#............. +..#.#...#.#.#.....##..#....#... \ No newline at end of file diff --git a/rust/src/day03/main.rs b/rust/src/day03/main.rs new file mode 100644 index 0000000..60905ce --- /dev/null +++ b/rust/src/day03/main.rs @@ -0,0 +1,45 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +pub struct ParsedInput { + pub field: Vec>, + pub width: usize, + pub height: usize, +} +pub enum Spot { + Empty, + Tree, +} +impl From<&u8> for Spot { + fn from(value: &u8) -> Self { + match value { + b'.' => Self::Empty, + b'#' => Self::Tree, + _ => unreachable!(), + } + } +} + +pub fn parse_input(input: &'static str) -> ParsedInput { + let field = input + .lines() + .map(|line| line.as_bytes().iter().map(Spot::from).collect()) + .collect::>>(); + let height = field.len(); + let width = field[0].len(); + ParsedInput { + field, + width, + height, + } +} + +pub fn main() { + let input = parse_input(INPUT); + part_1(&input); + part_2(&input); +} diff --git a/rust/src/day03/part_1.rs b/rust/src/day03/part_1.rs new file mode 100644 index 0000000..cf25f2c --- /dev/null +++ b/rust/src/day03/part_1.rs @@ -0,0 +1,27 @@ +use crate::{ParsedInput, Spot}; + +pub(crate) fn part_1(input: &ParsedInput) -> u64 { + let mut result = 0; + for y in 0..input.height { + let x = (y * 3) % input.width; + if matches!(input.field[y][x], Spot::Tree) { + result += 1; + } + } + result +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_1(&crate::parse_input(SAMPLE_INPUT)), 7); + } + + #[test] + fn test_with_solution() { + assert_eq!(super::part_1(&crate::parse_input(crate::INPUT)), 187); + } +} diff --git a/rust/src/day03/part_2.rs b/rust/src/day03/part_2.rs new file mode 100644 index 0000000..b429223 --- /dev/null +++ b/rust/src/day03/part_2.rs @@ -0,0 +1,34 @@ +use crate::{ParsedInput, Spot}; + +pub(crate) fn part_2(input: &ParsedInput) -> u64 { + let mut result = 1; + for (dx, dy) in [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)] { + let mut count = 0; + let mut x = 0; + let mut y = 0; + while y < input.height { + if matches!(input.field[y][x], Spot::Tree) { + count += 1; + } + x = (x + dx) % input.width; + y += dy; + } + result *= count; + } + result +} + +#[cfg(test)] +mod tests { + const SAMPLE_INPUT: &str = include_str!("sample_input.txt"); + + #[test] + fn test_with_sample_solution() { + assert_eq!(super::part_2(&crate::parse_input(SAMPLE_INPUT)), 336); + } + + #[test] + fn test_with_solution() { + assert_eq!(super::part_2(&crate::parse_input(crate::INPUT)), 4723283400); + } +} diff --git a/rust/src/day03/sample_input.txt b/rust/src/day03/sample_input.txt new file mode 100644 index 0000000..8f551de --- /dev/null +++ b/rust/src/day03/sample_input.txt @@ -0,0 +1,11 @@ +..##....... +#...#...#.. +.#....#..#. +..#.#...#.# +.#...##..#. +..#.##..... +.#.#.#....# +.#........# +#.##...#... +#...##....# +.#..#...#.# \ No newline at end of file