Part 2 bruteforced, wasnt that hard and quick enough, day 18

This commit is contained in:
2025-01-06 22:46:56 +01:00
parent 5c8c774757
commit afe7131f31

View File

@@ -11,13 +11,13 @@ fn parse(input: &str) -> Vec<(i64, i64)> {
.collect()
}
fn solve_1(input: &str, steps: usize, range: i64) -> i64 {
fn solve_1(input: &str, steps: usize, range: i64) -> Option<i64> {
let memory = parse(input);
let memory: HashSet<_> = memory[..steps].into_iter().collect();
let mut visited: HashSet<(i64, i64)> = HashSet::new();
let mut path_length = 0;
let mut stack = [(0, 0)].into_iter().collect();
let mut stack: HashSet<_> = [(0, 0)].into_iter().collect();
let dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)];
@@ -25,9 +25,13 @@ fn solve_1(input: &str, steps: usize, range: i64) -> i64 {
path_length += 1;
let mut new_stack = HashSet::new();
if stack.is_empty() {
return None;
}
for pos in stack {
if pos == (range, range) {
return path_length - 1;
return Some(path_length - 1);
}
visited.insert(pos);
@@ -46,7 +50,17 @@ fn solve_1(input: &str, steps: usize, range: i64) -> i64 {
}
}
fn solve_2(input: &str) {}
fn solve_2(input: &str, range: i64) -> (i64, i64) {
let memory = parse(input);
for i in 0..memory.len() {
if solve_1(input, i, range).is_none() {
return memory[i - 1];
}
}
return (0, 0);
}
fn main() {
println!("Hello, this is Patrick!");
@@ -54,10 +68,16 @@ fn main() {
let input = include_str!("../input.txt");
let result_1 = solve_1(input, 1024, 70);
println!("{}", result_1);
println!(
"The number of steps of the shortest route from beginning to end is {}",
result_1.unwrap()
);
let result_2 = solve_2(input);
//println!("{}", result_2);
let result_2 = solve_2(input, 70);
println!(
"The first byte that prevents a path being laid is {},{}",
result_2.1, result_2.0
);
}
#[cfg(test)]
@@ -67,12 +87,12 @@ mod tests {
#[test]
fn test_1() {
let test_input = include_str!("../test.txt");
assert_eq!(solve_1(test_input, 12, 6), 22);
assert_eq!(solve_1(test_input, 12, 6).unwrap(), 22);
}
#[test]
fn test_2() {
//let test_input = include_str!("../test.txt");
//assert_eq!(solve_2(test_input), _);
let test_input = include_str!("../test.txt");
assert_eq!(solve_2(test_input, 6), (1, 6));
}
}