Day 15 part 2

This commit is contained in:
Bill Thiede 2021-12-15 11:24:42 -08:00
parent c16e3ad864
commit c7a180dee3

View File

@ -17,6 +17,17 @@ struct Image {
pixels: Vec<usize>, pixels: Vec<usize>,
} }
impl Image {
fn new(width: usize, height: usize) -> Image {
let pixels = vec![0; width * height];
Image {
width,
height,
pixels,
}
}
}
impl Debug for Image { impl Debug for Image {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
writeln!(f)?; writeln!(f)?;
@ -158,17 +169,8 @@ fn shortest_path(adj_list: &Vec<Vec<Edge>>, start: usize, goal: usize) -> Option
// Goal not reachable // Goal not reachable
None None
} }
#[aoc(day15, part1)]
fn part1(input: &str) -> Result<usize> { fn make_graph(im: &Image) -> Vec<Vec<Edge>> {
let im: Image = input.parse()?;
// TODO build graph by walking pixels, finding neighobrs and using their cost.
// let graph = vec![
// // Node 0
// vec![Edge { node: 2, cost: 10 },
// Edge { node: 1, cost: 1 }],
// // Node 1
// vec![Edge { node: 3, cost: 2 }],
// ...
let idx = |x, y| y * im.width + x; let idx = |x, y| y * im.width + x;
let mut graph: Vec<_> = Vec::new(); let mut graph: Vec<_> = Vec::new();
for y in 0..im.height { for y in 0..im.height {
@ -201,16 +203,40 @@ fn part1(input: &str) -> Result<usize> {
graph.push(edges); graph.push(edges);
} }
} }
graph
}
#[aoc(day15, part1)]
fn part1(input: &str) -> Result<usize> {
let im: Image = input.parse()?;
let graph = make_graph(&im);
Ok(shortest_path(&graph, 0, im.pixels.len() - 1).unwrap()) Ok(shortest_path(&graph, 0, im.pixels.len() - 1).unwrap())
} }
/* fn x5(im: &Image) -> Image {
let mut im5 = Image::new(im.width * 5, im.height * 5);
for iy in 0..5 {
for ix in 0..5 {
for y in 0..im.height {
for x in 0..im.width {
let v = im[(x, y)] + ix + iy;
let dst_x = ix * im.width + x;
let dst_y = iy * im.height + y;
im5[(dst_x, dst_y)] = if v > 9 { v % 9 } else { v };
}
}
}
}
im5
}
#[aoc(day15, part2)] #[aoc(day15, part2)]
fn part2(input: &str) -> Result<usize> { fn part2(input: &str) -> Result<usize> {
todo!("part2"); let im: Image = input.parse()?;
Ok(0) let im = x5(&im);
let graph = make_graph(&im);
Ok(shortest_path(&graph, 0, im.pixels.len() - 1).unwrap())
} }
*/
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
@ -235,14 +261,22 @@ mod tests {
Ok(()) Ok(())
} }
/*
#[test] #[test]
fn test_part2()->Result<()> { fn test_part2() -> Result<()> {
let input = r#" let input = r#"
1163751742
1381373672
2136511328
3694931569
7463417111
1319128137
1359912421
3125421639
1293138521
2311944581
"# "#
.trim(); .trim();
assert_eq!(part2(input)?, usize::MAX); assert_eq!(part2(input)?, 315);
Ok(()) Ok(())
} }
*/
} }