Rust Day 3

This commit is contained in:
Tobias Berger 2023-04-27 12:51:03 +02:00
parent 4a0a756323
commit 526806b224
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
9 changed files with 451 additions and 4 deletions

3
rust/.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"rust-analyzer.showUnlinkedFileNotification": false
}

2
rust/Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "advent-of-code" name = "advent-of-code"
version = "20.1.2" version = "20.3.2"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "advent-of-code" name = "advent-of-code"
version = "20.1.2" version = "20.3.2"
edition = "2021" edition = "2021"
resolver = "2" resolver = "2"
@ -30,3 +30,7 @@ path = "src/day01/main.rs"
[[bin]] [[bin]]
name = "day02" name = "day02"
path = "src/day02/main.rs" path = "src/day02/main.rs"
[[bin]]
name = "day03"
path = "src/day03/main.rs"

View file

@ -21,7 +21,7 @@ impl FromStr for Password {
type Err = ParseError; type Err = ParseError;
fn from_str(line: &str) -> Result<Self, Self::Err> { fn from_str(line: &str) -> Result<Self, Self::Err> {
let parts = line.split(" ").collect::<Vec<_>>(); let parts = line.split(' ').collect::<Vec<_>>();
if parts.len() != 3 { if parts.len() != 3 {
return Err(ParseError(line.into())); return Err(ParseError(line.into()));
@ -30,7 +30,7 @@ impl FromStr for Password {
let first_num = nums_iter.next().unwrap(); let first_num = nums_iter.next().unwrap();
let second_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(); let password = parts[2].into();
Ok(Password { Ok(Password {

323
rust/src/day03/input.txt Normal file
View file

@ -0,0 +1,323 @@
...#...#..#....#..#...#..##..#.
.#..#.....#.#............###...
.#...###....#.............##..#
...##...##....#.....##..#.##...
.....###.#.###..##.#.##.......#
#...##.....#..........#..#.#.#.
......##.......##..#....#.#....
....#.###.##..#.#..##.##....#.#
.......#.......###.#.#.##.....#
.........#.#....#..........#.#.
.#...##.....##.........#..#....
.##....#.#.#...##......#.......
##.#.#..#....#....#....#...#.#.
##....#.#..##......#....##...#.
....#..#..##..#.###.......#.#..
.....##....###...........#.#.##
#.....##.........#....##......#
........###.#..#....#....#.....
...#.......#.##..#.###......#..
...............#..#....#.##....
..#..###..#.#..#.........##..#.
####..#..####..................
#...####...#.......#.#.#...#...
......###.....#......#..#..#...
#...#.....##.....#.#..##...#.#.
#...........##.......#.........
.#..#.........#.#..##....#.....
........##...#................#
........#.###.#.###.#.#.##..##.
.#....##.....#...##.#..#.#.....
..#..#.....###....##.#....#.#.#
#......##.##...##..#.........#.
#..#..#.....#.....#.........#..
#....#.#...###.........#...#...
.#.#.....##......#.#......#....
..##......##...#.#.#.#.........
..#......#.....##.###.#.#..#...
....#..#.......#..#..#.....#...
.#.#.....#...#..........#......
#.#..#...........#.#.##.#...#.#
..#.#....###...#...#.....#.#...
....##.#.###....####.......#...
.....##....#.......#..#..#....#
...##..#.#.#.#......#......#...
...##...#....#...#......###...#
........#..#.#.....#.###.......
..#..##.#....#.#.........#...#.
.....#.####....#.##.........#..
......#...#...#.....#......###.
.##.....#....#..#.#....#.....#.
...........#...#....##..#...#..
.....#....#.....#...##..#...#.#
.#...#.........#.......#...#..#
...#..#...#........#......#....
..#..#####.#.....#.#....#...#.#
...#.......#.#....#...##..#..#.
####..#.#.###.#.#..............
.##........#...#.#....#..#.....
..#..............#.#..##...#.##
.###.#.....#.#.....##.#......##
....###.....#...#...#.#..#.....
....###.#.##.......#....#...#..
#..#...#......##..#.....#.#...#
....#.#.........#..............
#.##.##...#..#.#.#.....#...#.##
#...#...#......#...........##..
#.#.#......#............#.#....
.#.#..######...#.#.........#.##
..#.#..#...#......#............
....#.....#......##..#.....#...
.##............#....##..#......
.#.#.#...#.##.............###.#
#.#...#...#.....#....#.#.#.....
........#..#......##.##.#.....#
.....#.....#.#####...#....#....
.#...#......#.........#.#......
...#...#..##.....##....#..#....
....#....##..#.........#.......
..#........##..#.#........#....
...#...##...........#...#....#.
.....##.........#..#....#..#.#.
#..#....##..#...##.....#..##.#.
..#.#.#.#...#...#.....#.#....#.
.......#.###...#.#.......#.#...
....#..#..#.###.#.....###..#.#.
.#..##......#..#..#....#.####..
..##...........#...#.........#.
......#..#...#..........#......
....#..........#......##...#...
....#..#.##........#.#...##.#..
#.##......#........##.#...#...#
#..#....#.....###........##....
...........##.....##..#....#.##
..#....#..#..#......#.#.....#..
#....#.##....#.....##.......#..
.#.....#.#..............#.##..#
.#..#..#...#...#....#.#.....#..
...###...##.#...#..#........#..
#...#.##.#.....#.#....#..#.....
#.....###.#.......#.#..#.#..##.
....#..#..##.......###.#...#...
.#...####...............#.....#
.#.##.#.....#.....#.#......##.#
#...........#.##....###.##....#
...............#..........#....
.....#..#.##.###.#.............
...##.............#.....#.#..#.
....#.#...#.#..#..#..#....#....
..#.......#..........#...#...#.
...............#.#.#...###....#
....#...#.##....#..##....#.....
........#.#.##.........##.##.##
#.....###.......#.#....#..#..##
.#..#...#......#.#..##.......#.
#.....#.#........#.##..#..#....
.###..##.#.......#......###....
.#...###.....#.....#....###...#
........##.##......#.#....#...#
.#....#..#.........#..##...##..
.......#.......##.#..#..##.....
#..##..##......#.#......#.##...
..#..###..#...#....#..#...#....
#.............#.####.........##
..#..................#...#..#..
..#......#........##.......#.#.
.#.#.#.#..###.....#....#.#.....
...#.##.###.......#....#.......
................##...#.....#...
..#.###.#...#.####....#..#..#..
..#....###....##..#.#.........#
.#..#.#.....#........#....##...
.....#..#......#..#..##.#.#....
.#..#.........##....##......#..
.....#.#...#...#.#...#.#...#.#.
..#..#...#...#...##.#..###.....
..#..##......#..##.#...##......
.......#..##....##.#......#..#.
..#......#.#.....#.##....##....
..#....#......#......##........
....##.#.#....#.......#.##.....
#.....#...###....#....#...#....
............#.#..#...#...#..#..
..##.............##....#.......
.#.......#.##.#......#....##...
...##............#....#..#...#.
.##.####.....#.#..###.#....#.##
....##.#........#..#...#.......
...#...###.##...........##..#..
..##..##....#...#..#..........#
..#.........#.#...##..........#
.......##....#.#...##.....#..#.
.............#.....#.#.......#.
#.......#..##..##...##.#.......
..............#.....#.#..#...##
........##..#.....#...#...#.#..
###.#.................#........
...#........#...#.#######..#..#
...#.##...##.#.#..######...#...
#.......#..#....#..#.##.....#..
#..#....##....#.##.......#....#
#...#..#.#.#...#..#.##..#......
....#..##....#..#.#...........#
.##..#.#.............###.......
#....##......#..#..#.....###...
..#..........#...###.#.........
.####......#....#......#.#....#
..#....#.#.#......#....#.......
.....#.....#....#....#####....#
.##..........#...#.###....#....
....##.....##......#...#.#.....
.#...#...#..#.#.#...#####......
...#.##..####.##.##.......##...
............#.......#..........
.#..##.#..#####........#..#...#
#......##..##..##.........##...
....#....#.............#.#....#
###..#.....#.....#.#...#..#.###
#...#.......##......#....#.#.#.
...#......#..#...#....#...###.#
....#....##.......#....#......#
............#......##.##.....#.
...#.........#......#....##..##
.....##....##...#..###...#..#..
.......##.#..........#.##.##...
....##...........#.#..#..#.##.#
#...#..##.##.#....#....#.#.....
...##.#.....#..#..#..###....##.
#.##.#..#..#.#.............#...
..#.#.............###.....#....
...#..#....#..#.....#.#..#..#..
...#.....##.#...........#..##.#
.........#.#.##..#..#.#...#....
...#..##..#...#...###.##.#..#..
.#..##...##......##..##........
......##....##.#.##.#.#........
...#..................#.....#..
.##................#.#..#..###.
.##.##.....#................#..
.....#.#..........#...#..#.#..#
.............#......#..#.#..#..
...#...##..#........#....#.....
#......#........##.##...##.....
##..#..##....#...#............#
..##..##.##....##..##........#.
...#....#.#.#.#....#.#...##....
....#...##..##.#.##...#..#...#.
#..#....##.#.....#.......#...##
##.#....#.............#..#.....
.##..#..#.#.....#.......#.#..#.
.......#..#...##...#...###..#..
..........#...#.#..##.....#...#
..#....#...........#####....#..
#....#..#.......##.............
.........##..#####.......##....
#..#..........#.....###...#..#.
.#.#.#..#...#.......##...#####.
.....#....#.###...#.......#....
#.#.....##...###....###....#...
.#.....#..#.#.#........#...#...
.##.#.#.#......#....###....#...
.#..##..####......###......#...
......#.#.#.#.#...#...####.##..
.#........##..#.....#....#....#
.....###......##..#....#.......
#.#.##...#.#......###..........
........#.#...#..#......#....#.
..##...##.........#.......#.#..
..#.##....#...##.....#.###.....
.........#..#.#....#....#.#.##.
#.........#......#..#.......#..
...#...##.......#.........#....
............#......#...........
##.....#.....#.#...#.....#.....
..#.#...#..#...#.#...........#.
#.#.#..#..#...##.#...#.#.....#.
.#..###.#..##.#.....#.....#....
##....##....#.......##..##.....
.#..#...........###..........#.
.#..#..#..........###..#.......
#..###......#............##...#
#......#........#..#..#..#.#...
.......#.###...#.##............
.##....#.......#.#...##.....#.#
....#..#.#.......#.#...........
##....#.###.#....#.#..##.#....#
..#..#..#....#...#........##...
...#...##....#..#.#...#..#.....
......#..#......#....#.......#.
#.#..............#...###...#..#
...#....#..#..........#.#...#..
#.....##..##.....#........#....
.#...##..#.#..............#....
##.#....#..##...#..#.####.#..#.
.....#.......#.#.#.#..#.....###
...#.##....#.#........##.......
#...#.#...#.#..###..##.##...#.#
###..............#.#.###.......
...###..#.#..#....##...###.#...
......##...........#...#..#...#
.#..#.........##.......#..#...#
.#.......###......##...#...#...
.#......##...#........#.......#
.#..#.....#.........#.#........
#...#.#.....#...#..##.........#
......##.#......##.#..##.#.....
...............#.#..#....#....#
#....#..#..#..#.#.....##...##..
#.#......#.###......#..#...####
.#.#..#...#...#.#..#.##.##.#.#.
.....#.#...###...#.#.....##....
...#..#.#..........##.#....#.#.
...#..#.#.##.....###.##.#....#.
..........#..###......#..#.#...
###.....#..###..#...#..###.#...
..#..#.....##.#.#..###.......#.
....#....##........##..........
.......#..........#...#......#.
.#........#.#.#.#.#.......#....
.#..#.......##..##....#.#...#..
.#.#.#.......#..#..............
#.#....#.#...#.#.#.....#.#...##
.....#..........##..#.......#..
.##......#.#....#.#.......#....
..#.##....#.##.#...#...........
...##......##..##.............#
..........##.#.#..#..........#.
.##....#..#..#.#....##.#...#.#.
...........#....#.....#.#..#...
.#.....#....##..#.........#....
.....#.....#...#....#...#.###.#
..#....#....#.....#...#......#.
.....##..#.............#...#...
........#..#.......#.#.......#.
#...###..#.##.#...###...##..##.
....##..#.......#...#.#........
.#...#.#.##....####........#..#
.#...#.#.####.##.#.............
#..##...#....#...#.#.#.#.##..#.
.#.......#........#.....###....
#.#.....#....#..#....#..#....#.
...#..#...#.....#.........##...
.#....#......###...#....#.#.#..
#.#........#......#...#....##..
.....#..#......#..#..#......#..
.#.....#..#.##.#.#.#...#......#
##........#..#.#..#...#.####...
..........##....#.#..#.#....#..
#.##..#..#....#..#....##..#.#.#
..#......#.......#...##..#.....
##...#.........#......#......#.
.#.....................#..#.##.
.#.......#........#.#.#..##.#..
..#..........#........#..##.#..
.#...#...#.........##.#.#.#....
....#....#.###.#....###....#.##
....##......##........##.#.##..
....#.#......#.##.#...#.##.....
....#....#..#.#..###.#.#.......
....#......#..#.#.......#..##..
.....#..#.#.##.##..##.....#.#..
...#....................##.....
#.....#...##...#.#.............
..#.#...#.#.#.....##..#....#...

45
rust/src/day03/main.rs Normal file
View file

@ -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<Vec<Spot>>,
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::<Vec<Vec<_>>>();
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);
}

27
rust/src/day03/part_1.rs Normal file
View file

@ -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);
}
}

34
rust/src/day03/part_2.rs Normal file
View file

@ -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);
}
}

View file

@ -0,0 +1,11 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#