Finished aoc year 2015 day 3
This commit is contained in:
69
advent_of_code/2015/3/src/main.rs
Normal file
69
advent_of_code/2015/3/src/main.rs
Normal 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
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user