No performance improvement, but the code is a lot cleaner
This commit is contained in:
1
day_10/Cargo.lock
generated
1
day_10/Cargo.lock
generated
@@ -205,7 +205,6 @@ dependencies = [
|
|||||||
"ndarray",
|
"ndarray",
|
||||||
"nom",
|
"nom",
|
||||||
"num",
|
"num",
|
||||||
"rayon",
|
|
||||||
"strum",
|
"strum",
|
||||||
"strum_macros",
|
"strum_macros",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ csv = "1.3.0"
|
|||||||
ndarray = "0.15.6"
|
ndarray = "0.15.6"
|
||||||
nom = "7.1.3"
|
nom = "7.1.3"
|
||||||
num = "0.4.1"
|
num = "0.4.1"
|
||||||
rayon = "1.8.0"
|
|
||||||
strum = "0.25.0"
|
strum = "0.25.0"
|
||||||
strum_macros = "0.25.3"
|
strum_macros = "0.25.3"
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
|
Before Width: | Height: | Size: 1.3 MiB After Width: | Height: | Size: 7.7 MiB |
BIN
day_10/perf.data
BIN
day_10/perf.data
Binary file not shown.
Binary file not shown.
@@ -6,44 +6,32 @@ struct Point {
|
|||||||
y: usize,
|
y: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part1(input: &str) -> String {
|
fn count_map(input: &str) -> Array2<usize> {
|
||||||
let mut current_nodes = vec![input.lines().enumerate().find_map(|(y,line)| {
|
let rows = input.lines().count();
|
||||||
line.chars().position(|character| {character == 'S'}).and_then(|x| {Some(Point{x, y})})
|
|
||||||
}).unwrap()];
|
|
||||||
let rows = input.lines().collect::<Vec<_>>().len();
|
|
||||||
let columns = input.lines().next().unwrap().len();
|
let columns = input.lines().next().unwrap().len();
|
||||||
let mut count_map: Array2<usize> = Array::zeros((rows, columns));
|
let mut count_map: Array2<usize> = Array::zeros((rows, columns));
|
||||||
let mut num_steps = 0;
|
|
||||||
|
let s_point = input.lines().enumerate().find_map(|(y,line)| {
|
||||||
|
line.chars().position(|character| {character == 'S'}).and_then(|x| {Some(Point{x, y})})
|
||||||
|
}).unwrap();
|
||||||
|
let initial_directions = [
|
||||||
|
(['|','7','F'], (0_i32, -1_i32), s_point.y > 0),
|
||||||
|
(['-','7','J'], (1_i32, 0_i32), s_point.x < columns),
|
||||||
|
(['|','L','J'], (0_i32, 1_i32), s_point.y < rows),
|
||||||
|
(['-','F','L'], (-1_i32, 0_i32), s_point.x > 0),
|
||||||
|
];
|
||||||
|
let mut current_nodes = initial_directions.into_iter().filter_map(|(characters, (x, y), condition)| {
|
||||||
|
condition.then(|| {
|
||||||
|
let next_character = input.as_bytes()[((s_point.y as i32 + y)*(columns as i32+1) + s_point.x as i32 + x) as usize] as char;
|
||||||
|
characters.contains(&next_character).then_some(Point{x: (s_point.x as i32 + x) as usize, y: (s_point.y as i32 + y) as usize})
|
||||||
|
})
|
||||||
|
}).flatten().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let mut num_steps = 1;
|
||||||
while current_nodes.len() > 0 {
|
while current_nodes.len() > 0 {
|
||||||
let mut next_nodes = vec![];
|
let mut next_nodes = vec![];
|
||||||
current_nodes.iter().for_each(|point| {
|
current_nodes.iter().for_each(|point| {
|
||||||
let character = input.lines().nth(point.y).unwrap().chars().nth(point.x).unwrap();
|
let character = input.lines().nth(point.y).unwrap().chars().nth(point.x).unwrap();
|
||||||
if character == 'S' {
|
|
||||||
if point.y != 0 {
|
|
||||||
let next_character = input.lines().nth(point.y-1).unwrap().chars().nth(point.x).unwrap();
|
|
||||||
if ['|','7','F'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y-1});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.x != columns-1 {
|
|
||||||
let next_character = input.lines().nth(point.y).unwrap().chars().nth(point.x+1).unwrap();
|
|
||||||
if ['-','7','J'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x+1, y: point.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.y != rows-1 {
|
|
||||||
let next_character = input.lines().nth(point.y+1).unwrap().chars().nth(point.x).unwrap();
|
|
||||||
if ['|','L','J'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y+1});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.x != 0 {
|
|
||||||
let next_character = input.lines().nth(point.y).unwrap().chars().nth(point.x-1).unwrap();
|
|
||||||
if ['-','F','L'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x-1, y: point.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ['|', 'L', 'J'].contains(&character) && point.y != 0 {
|
if ['|', 'L', 'J'].contains(&character) && point.y != 0 {
|
||||||
next_nodes.push(Point{x: point.x, y: point.y-1});
|
next_nodes.push(Point{x: point.x, y: point.y-1});
|
||||||
}
|
}
|
||||||
@@ -56,81 +44,26 @@ pub fn part1(input: &str) -> String {
|
|||||||
if ['-', 'J', '7'].contains(&character) && point.x != 0 {
|
if ['-', 'J', '7'].contains(&character) && point.x != 0 {
|
||||||
next_nodes.push(Point{x: point.x-1, y: point.y});
|
next_nodes.push(Point{x: point.x-1, y: point.y});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
});
|
||||||
num_steps += 1;
|
num_steps += 1;
|
||||||
current_nodes = vec![];
|
current_nodes = next_nodes.iter().filter_map(|point| {
|
||||||
next_nodes.iter().for_each(|point| {
|
(count_map[[point.y, point.x]] == 0).then(|| {
|
||||||
if count_map[[point.y, point.x]] == 0 {
|
|
||||||
count_map[[point.y, point.x]] = num_steps;
|
count_map[[point.y, point.x]] = num_steps;
|
||||||
current_nodes.push(Point{x: point.x, y: point.y});
|
Point{x: point.x, y: point.y}
|
||||||
|
})
|
||||||
|
}).collect();
|
||||||
}
|
}
|
||||||
});
|
count_map
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn part1(input: &str) -> String {
|
||||||
|
let count_map = count_map(input);
|
||||||
count_map.iter().max().unwrap().to_string()
|
count_map.iter().max().unwrap().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part2(input: &str) -> String {
|
pub fn part2(input: &str) -> String {
|
||||||
let mut current_nodes = vec![input.lines().enumerate().find_map(|(y,line)| {
|
let mut count_map = count_map(input);
|
||||||
line.chars().position(|character| {character == 'S'}).and_then(|x| {Some(Point{x, y})})
|
let &[rows, columns] = count_map.shape() else { panic!() };
|
||||||
}).unwrap()];
|
|
||||||
let rows = input.lines().collect::<Vec<_>>().len();
|
|
||||||
let columns = input.lines().next().unwrap().len();
|
|
||||||
let mut count_map: Array2<usize> = Array::zeros((rows, columns));
|
|
||||||
let mut num_steps = 0;
|
|
||||||
while current_nodes.len() > 0 {
|
|
||||||
let mut next_nodes = vec![];
|
|
||||||
current_nodes.iter().for_each(|point| {
|
|
||||||
let character = input.lines().nth(point.y).unwrap().chars().nth(point.x).unwrap();
|
|
||||||
if character == 'S' {
|
|
||||||
if point.y != 0 {
|
|
||||||
let next_character = input.lines().nth(point.y-1).unwrap().chars().nth(point.x).unwrap();
|
|
||||||
if ['|','7','F'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y-1});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.x != columns-1 {
|
|
||||||
let next_character = input.lines().nth(point.y).unwrap().chars().nth(point.x+1).unwrap();
|
|
||||||
if ['-','7','J'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x+1, y: point.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.y != rows-1 {
|
|
||||||
let next_character = input.lines().nth(point.y+1).unwrap().chars().nth(point.x).unwrap();
|
|
||||||
if ['|','L','J'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y+1});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if point.x != 0 {
|
|
||||||
let next_character = input.lines().nth(point.y).unwrap().chars().nth(point.x-1).unwrap();
|
|
||||||
if ['-','F','L'].contains(&next_character) {
|
|
||||||
next_nodes.push(Point{x: point.x-1, y: point.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if ['|', 'L', 'J'].contains(&character) && point.y != 0 {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y-1});
|
|
||||||
}
|
|
||||||
if ['-', 'F', 'L'].contains(&character) && point.x != columns-1 {
|
|
||||||
next_nodes.push(Point{x: point.x+1, y: point.y});
|
|
||||||
}
|
|
||||||
if ['|', '7', 'F'].contains(&character) && point.y != rows-1 {
|
|
||||||
next_nodes.push(Point{x: point.x, y: point.y+1});
|
|
||||||
}
|
|
||||||
if ['-', 'J', '7'].contains(&character) && point.x != 0 {
|
|
||||||
next_nodes.push(Point{x: point.x-1, y: point.y});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
num_steps += 1;
|
|
||||||
current_nodes = vec![];
|
|
||||||
next_nodes.iter().for_each(|point| {
|
|
||||||
if count_map[[point.y, point.x]] == 0 {
|
|
||||||
count_map[[point.y, point.x]] = num_steps;
|
|
||||||
current_nodes.push(Point{x: point.x, y: point.y});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
let s_position = input.lines().enumerate().find_map(|(y,line)| {
|
let s_position = input.lines().enumerate().find_map(|(y,line)| {
|
||||||
line.chars().position(|character| {character == 'S'}).and_then(|x| {Some(Point{x, y})})
|
line.chars().position(|character| {character == 'S'}).and_then(|x| {Some(Point{x, y})})
|
||||||
}).unwrap();
|
}).unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user