Day 18 part 1 aoc done

This commit is contained in:
2025-01-06 17:32:32 +01:00
parent a2dbfb47da
commit 5c8c774757
4 changed files with 3559 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
use std::collections::HashSet;
fn parse(input: &str) -> Vec<(i64, i64)> {
input
.lines()
.map(|s| {
let (y, x) = s.split_once(',').unwrap();
(x.parse().unwrap(), y.parse().unwrap())
})
.collect()
}
fn solve_1(input: &str, steps: usize, range: i64) -> 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 dirs = [(1, 0), (-1, 0), (0, 1), (0, -1)];
loop {
path_length += 1;
let mut new_stack = HashSet::new();
for pos in stack {
if pos == (range, range) {
return path_length - 1;
}
visited.insert(pos);
for dir in dirs {
let new_pos = (pos.0 + dir.0, pos.1 + dir.1);
if new_pos.0 >= 0 && new_pos.0 <= range && new_pos.1 >= 0 && new_pos.1 <= range {
if !visited.contains(&new_pos) && !memory.contains(&new_pos) {
new_stack.insert(new_pos);
}
}
}
}
stack = new_stack;
}
}
fn solve_2(input: &str) {}
fn main() {
println!("Hello, this is Patrick!");
let input = include_str!("../input.txt");
let result_1 = solve_1(input, 1024, 70);
println!("{}", result_1);
let result_2 = solve_2(input);
//println!("{}", result_2);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1() {
let test_input = include_str!("../test.txt");
assert_eq!(solve_1(test_input, 12, 6), 22);
}
#[test]
fn test_2() {
//let test_input = include_str!("../test.txt");
//assert_eq!(solve_2(test_input), _);
}
}