Day 12
This commit is contained in:
parent
8a2aa96881
commit
e40e06510b
41
input/12.txt
Normal file
41
input/12.txt
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
abaaaaacccccccccccccccccccccccccccccccccccccccaaaaaaaccccaaaaaaaaaaaaaaaaacccccaaaaaacccccccccccccccccccccccaaaaaaaaccccccccccccccccccccccccccccccccaaaaaa
|
||||||
|
abaaaaaacccaaaacccccccccccccccccccccccaccccccccaaaaaaaaccaaaaaaaaaaaaaaaaccccccaaaaaacccccccccccccccccccccccccaaaaccccccccccccccccccccccccccccccccccaaaaaa
|
||||||
|
abaaaaaacccaaaacccccccccccccccccaaaaaaaacccccccaaaaaaaaacaaaaaaaaaaaaacccccccccaaaaacccccccccccccccccccccccccaaaaacccccccccccccccccccaaaccccccccccccaaaaaa
|
||||||
|
abaaacaccccaaaaccccccccccccccccccaaaaaacccccccccaaaaaaaccccaaaaaaaaaaacccccccccaaaaacccccccccccccccccccccccccaacaaaccccccccccccccccccaaacccccccccccccccaaa
|
||||||
|
abaaacccccccaaacccccccccccaacccccaaaaaaccccccccaaaaaaccccccaacaaaaaaaacccccccccccccccccccccccaaccccccccccccccacccaaaaacccccccccaaccccaaacccccccccccccccaaa
|
||||||
|
abccccccccccccccccccccccccaaaaccaaaaaaaacccccccaaaaaaaccccccccaaaaaaaaaccccccccccaacccccccccaaaccccccccccccccccccacaaacccccccccaaaaccaaacccccccccccccccaac
|
||||||
|
abccccccccccccccccccccccaaaaaacaaaaaaaaaaccccccaaccaaaaacccccaaaaccaaaaccccccccccaaacaacccccaaacaaacccaaccccccccaaaaaaaacccccccaaaaakkkkkkcccccccccccccccc
|
||||||
|
abccccccccccccccccccccccaaaaaccaaaaaaaaaacccccccccccaaaaaaccccacccaaaaaccccccccccaaaaaaccaaaaaaaaaaaaaaaccccccccaaaaaaaaccccccccaaajkkkkkkkaccccccaacccccc
|
||||||
|
abcccccccccccccccccccccccaaaaacacacaaaccccccccccccccaaaaaaccccccccaaaacccccccccaaaaaaacccaaaaaaaaaaaaaaaaaccccccccaaaaaccccccccccjjjkkkkkkkkccaaaaaacccccc
|
||||||
|
abcccccccccccccccccccccccaacaacccccaaacccaccccccccccaaaaaaccccccccaaaacccccccccaaaaaaacccccaaaaaacaaaaaaaacccccccaaaaacccccccjjjjjjjooopppkkkcaaaaaaaccccc
|
||||||
|
abcccccccccccccccccaacaacccccccccccaaaaaaacccccccccccaaaaacccccccccccccccccccccccaaaaaaccccaaaaaaccaaaaaaacccccccaaaaaacciijjjjjjjjoooopppkkkcaaaaaaaacccc
|
||||||
|
abccccccccccaaaccccaaaaacccccccccccccaaaaacccccccccccaaaaccccccccccccccccccccccccaacaaaccccaaaaaaacaaaaacccccccccaccaaaciiiijjjjjjoooopppppkllcaaaaaaacccc
|
||||||
|
abccaaccccccaaaaacaaaaacccccccccccccaaaaaacccccccccccccccccccccccccccccccccccccccaacccccccaaaacaaaaaaaaacccaaccccaaaaaciiiiinoooooooouuuupplllaaaaaacccccc
|
||||||
|
abcaaacccccaaaaaacaaaaaacccccccccccaaaaaaaaccccccccaacaccccccccccccccccccccccccccccccccccccaccccccccccaaccaaaccccaaaaaciiinnnooooooouuuuuppplllaaacacccccc
|
||||||
|
abaaaaaacccaaaaaacccaaaacccccccccccaaaaaaaaccccccccaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaacaacaaaaaaiiinnnnntttoouuuuuupppllllcccccccccc
|
||||||
|
abaaaaaaccccaaaaacccaaccccccccccacccccaaccccccccccaaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaaaacaaaaaaiiinnnnttttuuuuxxuuupppllllccccccccc
|
||||||
|
abaaaaacccccaacaaccccccccccccccaaaccccaacccccaacccaaaaaacccccccccccccccccccccccccccccccccccccccccccccccccaaaaaccaaaaaaiiinnnttttxxuuxxyyuuppppllllcccccccc
|
||||||
|
abaaaacccccccccccccccccccccaaacaaaccccccaaacaaaaccacaaaacccccccccccccccccccccccccccccccccccaacccccccccccaaaaaccccaaaccciinnntttxxxxxxxyyvvvqqqqqlllccccccc
|
||||||
|
abaaaaaccccccccccccccccccccaaaaaaaaaacccaaaaaaacccccaaccccccccccccccccccccccccccccccccccccaaacccccccccccaacaaaccccccccciiinntttxxxxxxxyyvvvvvqqqqljjcccccc
|
||||||
|
abccaaaccaccccccccaaacccccccaaaaaaaaaccccaaaaaacccccccccccccccccccccccccccccccaacccccccaaaaacaaccccccccccccaacccccccccchhinnnttxxxxxxyyyyyvvvvqqqjjjcccccc
|
||||||
|
SbccccaaaacccccccaaaaaacccccccaaaaaccccccaaaaaaaaccccccccccccccccccccaaccccccaaaaccccccaaaaaaaacccccccccccccccccccccccchhhnnntttxxxxEzyyyyyvvvqqqjjjcccccc
|
||||||
|
abccccaaaacccccccaaaaaaccccccaaaaaacccccaaaaaaaaaacccccccccccccccccccaaccccccaaaaccccccccaaaaacccccccccccccccccccccccccchhhnntttxxxyyyyyyyvvvvqqqjjjcccccc
|
||||||
|
abcccaaaaaaccccccaaaaaacccccaaaaaaaccccaaaaaaaaaacccccccccccccccccaaaaaaaacccaaaacccccccaaaaaccccccccccccccccccccccccccchhmmmttxxxyyyyyyvvvvvqqqjjjdcccccc
|
||||||
|
abcccaaaaaacccccccaaaaacccccaaacaaacaaaaaaaaaaccccccccccccaaacccccaaaaaaaaccccccccccccccaacaaacccccccaacaaacccccccccccchhhmmmtswwwyyyyyyvvvqqqqjjjjdddcccc
|
||||||
|
abcccccaacccccccccaacaacccccccccccacaaaaaccaaaccccccccccaaaaacccccccaaaacccccccccccccccccccaaccccccccaaaaaacccccccccccchhhmmssswwwwwwyyywvrqqqjjjjdddccccc
|
||||||
|
abcccccccccccccccccccccccccccccccccaaaaaccccaaccccccccacaaaaaacccccaaaaacccccccccccccccccccccccccccccaaaaaacccccccccccchhhmmssswwwwwwywywwrrqjjjjddddccccc
|
||||||
|
abcccccccccccccccccccccccccccccccccaaaaaccccccccaaacaaacaaaaaacccccaaaaaaccccccccccccccccccccccccccccaaaaaaaccccccccccchhmmmsssswwsswwwwwwrrkkjjddddcccccc
|
||||||
|
abccccccccccccccccccccccccccccccccccaaaaacccccccaaaaaaacaaaaaccccccaaccaacccccccccccaaccccccccccccccaaaaaaaacaacaaccccchhhmmmsssssssswwwwrrrkkjddddaaccccc
|
||||||
|
abcccccccccccccccccccccccccaaaaaccccaacccccccccccaaaaaacaaaaacccccccccccccaacccccccaaaaaacccccccccccaaaaaaaacaaaaaccccchhgmmmmssssssrrwwwrrrkkddddaaaccccc
|
||||||
|
abcccccccccccccccccccccccccaaaaacccccccccccccccccaaaaaaaacccccccccccccccaaaaaaccccccaaaaaccccaaccccccccaaacccaaaaaaccccgggmmmmmmllllrrrrrrrkkkeedaaaaccccc
|
||||||
|
abcccccccccccaaccccccccccccaaaaaacccccccccccccccaaaaaaaaacccccccccccccccaaaaaaccccaaaaaaacccaaaacccccccaaccccaaaaaaccccggggmmmmllllllrrrrrkkkkeedaaaaacccc
|
||||||
|
abcccccccccccaaacaacaaaccccaaaaaaccccccccccccccaaaaaaaaaacccccccccccccccaaaaaaccccaaaaaaaaccaaaacccccccccccccaaaaaccccccgggggglllllllllrrkkkkeeeaaaaaacccc
|
||||||
|
abcccccccccccaaaaaacaaaacccaaaaaaccccccccccccccaaacaaaaaaccccccccccccccccaaaaaccccaaaaaaaaccaaaacccccccccccaaccaaaccccccgggggggggffflllkkkkkkeeeaaaaaacccc
|
||||||
|
abaccccccccaaaaaaaccaaaacccccaaacccccccccccccccccccaaaaaacaccccccccaaccccaaaacccccccaaacacccccccccccccccaaaaaccccccccccccccgggggffffflllkkkkeeeccaaacccccc
|
||||||
|
abaccccccccaaaaaaaccaaacccccccccccccccccaaaccccccccaaacaaaaaccccccaaacccccccccccccaaaacccccccccccccccccccaaaaaccccccccccccccccccaffffffkkkeeeeeccaaccccccc
|
||||||
|
abaaaccccccccaaaaaaccccccccccccccccccccaaaaaacccccccaaaaaaaacaaaacaaacccccccccaaaaaacccccccccccccccccccccaaaaaccccccccccccccccccccaffffffeeeeecccccccccccc
|
||||||
|
abaacccccccccaacaaaccccccccccccccccccccaaaaaaccccccccaaaaaccaaaaaaaaacccccccccaaaaaaaaccccccccccaaccccccaaaaacccccccccccccccccccccaaaffffeeeecccccccccccaa
|
||||||
|
abaacccccccccaaccccccccccccccccaaccccccaaaaacaaccaacccaaaaacaaaaaaaaacccccccccaaaaaaaaccccccaaacaacccccccccaacccccccccccccccccccccaaaccceaecccccccccccccaa
|
||||||
|
abaacccccccccccccccccccccccccccaaaaaacccaaaaaaaaaaaccaaacaaccaaaaaaaaaaaaacccccaaaaaaacccccccaaaaaccccccccccccccccccccccccccccccccaaacccccccccccccccaaacaa
|
||||||
|
abcccccccccccccccccccccccccccccaaaaaccccaacaacaaaaacccaaccccccaaaaaaaaaaaacccccaaaaacccccccccaaaaaaaccccccccccccccccccccccccccccccaaacccccccccccccccaaaaaa
|
||||||
|
abcccccccccccccccccccccccccccaaaaaaaccccccccaaaaaaaaccccccccccaaaaaaaaaaccccccaaaaaaccccccccaaaaaaaaccccccccccccccccccccccccccccccccccccccccccccccccaaaaaa
|
BIN
src/day12/.DS_Store
vendored
Normal file
BIN
src/day12/.DS_Store
vendored
Normal file
Binary file not shown.
125
src/day12/mod.rs
125
src/day12/mod.rs
@ -1,20 +1,137 @@
|
|||||||
use crate::read;
|
use crate::read;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
//#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
#[derive(Debug, PartialEq, Eq, Clone, Hash, Default)]
|
||||||
|
struct Coord {
|
||||||
|
pub row: i32,
|
||||||
|
pub col: i32,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
let input = read("12");
|
let input = read("12");
|
||||||
let lines = input.lines();
|
let lines = input.lines();
|
||||||
let lines = input.split("\n\n");
|
let mut start: Coord = Default::default();
|
||||||
|
let mut target: Coord = Default::default();
|
||||||
|
let map = lines
|
||||||
|
.enumerate()
|
||||||
|
.map(|(r, l)| {
|
||||||
|
l.chars()
|
||||||
|
.enumerate()
|
||||||
|
.map(|(col, c)| match c {
|
||||||
|
'S' => {
|
||||||
|
start = Coord {
|
||||||
|
row: r as i32,
|
||||||
|
col: col as i32,
|
||||||
|
};
|
||||||
|
0
|
||||||
|
}
|
||||||
|
'E' => {
|
||||||
|
target = Coord {
|
||||||
|
row: r as i32,
|
||||||
|
col: col as i32,
|
||||||
|
};
|
||||||
|
25
|
||||||
|
}
|
||||||
|
c => (c as u32) - ('a' as u32),
|
||||||
|
})
|
||||||
|
.collect_vec()
|
||||||
|
})
|
||||||
|
.collect_vec();
|
||||||
|
|
||||||
#[cfg(feature = "part1")]
|
#[cfg(feature = "part1")]
|
||||||
{
|
{
|
||||||
println!("Day 12, Part 01: {}", "TODO");
|
println!("Day 12, Part 01: {:?}", bfs(&map, start, target.clone()));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "part2")]
|
#[cfg(feature = "part2")]
|
||||||
{
|
{
|
||||||
println!("Day 12, Part 02: {}", "TODO");
|
let starting_points = map
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.flat_map(|(row, v)| {
|
||||||
|
v.iter()
|
||||||
|
.enumerate()
|
||||||
|
.filter(|(_, vv)| **vv == 0)
|
||||||
|
.map(|(col, _)| Coord {
|
||||||
|
row: row as i32,
|
||||||
|
col: col as i32,
|
||||||
|
})
|
||||||
|
.collect_vec()
|
||||||
|
})
|
||||||
|
.collect_vec();
|
||||||
|
let min = starting_points
|
||||||
|
.iter()
|
||||||
|
.filter_map(|start| bfs(&map, start.clone(), target.clone()))
|
||||||
|
.min();
|
||||||
|
println!("Day 12, Part 02: {:?}", min);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn bfs(map: &Vec<Vec<u32>>, start: Coord, target: Coord) -> Option<usize> {
|
||||||
|
let mut visited = vec![vec![false; map[0].len()]; map.len()];
|
||||||
|
let mut queue = std::collections::VecDeque::new();
|
||||||
|
queue.push_back((start, 0));
|
||||||
|
|
||||||
|
while let Some((next, path_length)) = queue.pop_front() {
|
||||||
|
if next == target {
|
||||||
|
return Some(path_length);
|
||||||
|
}
|
||||||
|
let up = Coord {
|
||||||
|
row: next.row - 1,
|
||||||
|
col: next.col,
|
||||||
|
};
|
||||||
|
let down = Coord {
|
||||||
|
row: next.row + 1,
|
||||||
|
col: next.col,
|
||||||
|
};
|
||||||
|
let left = Coord {
|
||||||
|
row: next.row,
|
||||||
|
col: next.col - 1,
|
||||||
|
};
|
||||||
|
let right = Coord {
|
||||||
|
row: next.row,
|
||||||
|
col: next.col + 1,
|
||||||
|
};
|
||||||
|
let can_down = check_bounds(&down, map)
|
||||||
|
&& !visited[down.row as usize][down.col as usize]
|
||||||
|
&& map[next.row as usize][next.col as usize] + 1
|
||||||
|
>= map[down.row as usize][down.col as usize];
|
||||||
|
if can_down {
|
||||||
|
visited[down.row as usize][down.col as usize] = true;
|
||||||
|
queue.push_back((down, path_length + 1));
|
||||||
|
}
|
||||||
|
let can_up = check_bounds(&up, map)
|
||||||
|
&& !visited[up.row as usize][up.col as usize]
|
||||||
|
&& map[next.row as usize][next.col as usize] + 1
|
||||||
|
>= map[up.row as usize][up.col as usize];
|
||||||
|
if can_up {
|
||||||
|
visited[up.row as usize][up.col as usize] = true;
|
||||||
|
queue.push_back((up, path_length + 1));
|
||||||
|
}
|
||||||
|
let can_left = check_bounds(&left, map)
|
||||||
|
&& !visited[left.row as usize][left.col as usize]
|
||||||
|
&& map[next.row as usize][next.col as usize] + 1
|
||||||
|
>= map[left.row as usize][left.col as usize];
|
||||||
|
if can_left {
|
||||||
|
visited[left.row as usize][left.col as usize] = true;
|
||||||
|
queue.push_back((left, path_length + 1));
|
||||||
|
}
|
||||||
|
let can_right = check_bounds(&right, map)
|
||||||
|
&& !visited[right.row as usize][right.col as usize]
|
||||||
|
&& map[next.row as usize][next.col as usize] + 1
|
||||||
|
>= map[right.row as usize][right.col as usize];
|
||||||
|
if can_right {
|
||||||
|
visited[right.row as usize][right.col as usize] = true;
|
||||||
|
queue.push_back((right, path_length + 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn check_bounds(coord: &Coord, map: &Vec<Vec<u32>>) -> bool {
|
||||||
|
coord.row >= 0
|
||||||
|
&& coord.col >= 0
|
||||||
|
&& coord.row < map.len() as i32
|
||||||
|
&& coord.row < map.len() as i32
|
||||||
|
&& coord.col < map[coord.row as usize].len() as i32
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user