Day 18 part 1 aoc done
This commit is contained in:
6
advent_of_code/2024/18/Cargo.toml
Normal file
6
advent_of_code/2024/18/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
3450
advent_of_code/2024/18/input.txt
Normal file
3450
advent_of_code/2024/18/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
78
advent_of_code/2024/18/src/main.rs
Normal file
78
advent_of_code/2024/18/src/main.rs
Normal 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), _);
|
||||||
|
}
|
||||||
|
}
|
||||||
25
advent_of_code/2024/18/test.txt
Normal file
25
advent_of_code/2024/18/test.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
5,4
|
||||||
|
4,2
|
||||||
|
4,5
|
||||||
|
3,0
|
||||||
|
2,1
|
||||||
|
6,3
|
||||||
|
2,4
|
||||||
|
1,5
|
||||||
|
0,6
|
||||||
|
3,3
|
||||||
|
2,6
|
||||||
|
5,1
|
||||||
|
1,2
|
||||||
|
5,5
|
||||||
|
2,5
|
||||||
|
6,5
|
||||||
|
1,4
|
||||||
|
0,4
|
||||||
|
6,4
|
||||||
|
1,1
|
||||||
|
6,1
|
||||||
|
1,0
|
||||||
|
0,5
|
||||||
|
1,6
|
||||||
|
2,0
|
||||||
Reference in New Issue
Block a user