Finished aoc year 2015 day 3

This commit is contained in:
2023-10-12 12:11:50 +02:00
parent c009060c48
commit d644336030
3 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "main"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,69 @@
use std::{borrow::BorrowMut, collections::HashSet};
fn solve_first(input: &str) -> usize {
let mut coord = (0, 0);
let mut visited = HashSet::new();
visited.insert(coord);
for c in input.chars() {
match c {
'>' => coord.0 += 1,
'<' => coord.0 -= 1,
'^' => coord.1 += 1,
'v' => coord.1 -= 1,
_ => (),
}
visited.insert(coord);
}
visited.len()
}
fn solve_second(input: &str) -> usize {
let mut santa_coord = (0, 0);
let mut robo_coord = (0, 0);
let mut visited = HashSet::new();
visited.insert(santa_coord);
for (idx, c) in input.chars().enumerate() {
let coord = if idx % 2 == 0 {
santa_coord.borrow_mut()
} else {
robo_coord.borrow_mut()
};
match c {
'>' => coord.0 += 1,
'<' => coord.0 -= 1,
'^' => coord.1 += 1,
'v' => coord.1 -= 1,
_ => (),
}
visited.insert(coord.clone());
}
visited.len()
}
fn main() {
println!("Hello, this is Patrick!");
let input_txt = include_str!("../input.txt");
let result_first = solve_first(input_txt);
println!(
"The number of houses that receive at least one present is {}",
result_first
);
let result_second = solve_second(input_txt);
println!(
"The number of houses that receive at least one present in the year after is {}",
result_second
);
}