summaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-12-12 00:50:30 -0500
committerJesse Luehrs <doy@tozt.net>2022-12-12 00:50:30 -0500
commit349ec2174dc890a5b7a8d47b5ca5d8c83fea1ec6 (patch)
tree1925abe9976d928cc4d8ef3104ed4a9be3360919 /src/bin
parent161c7e6d477d37003a92dfe48d725e89fbebd3bd (diff)
downloadadvent-of-code-349ec2174dc890a5b7a8d47b5ca5d8c83fea1ec6.tar.gz
advent-of-code-349ec2174dc890a5b7a8d47b5ca5d8c83fea1ec6.zip
day 12
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/2021/day25.rs2
-rw-r--r--src/bin/2022/day12.rs82
-rw-r--r--src/bin/2022/main.rs2
3 files changed, 85 insertions, 1 deletions
diff --git a/src/bin/2021/day25.rs b/src/bin/2021/day25.rs
index ed396c6..68d866e 100644
--- a/src/bin/2021/day25.rs
+++ b/src/bin/2021/day25.rs
@@ -60,7 +60,7 @@ impl Map {
pub fn parse(fh: File) -> Result<Map> {
Ok(Map {
- grid: parse::grid(parse::raw_lines(fh), |b| match b {
+ grid: parse::grid(parse::raw_lines(fh), |b, _, _| match b {
b'v' => Cell::Down,
b'>' => Cell::Right,
b'.' => Cell::None,
diff --git a/src/bin/2022/day12.rs b/src/bin/2022/day12.rs
new file mode 100644
index 0000000..d2d0acf
--- /dev/null
+++ b/src/bin/2022/day12.rs
@@ -0,0 +1,82 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+use advent_of_code::prelude::*;
+
+pub struct Map {
+ grid: Grid<u8>,
+ start: (Row, Col),
+ end: (Row, Col),
+}
+
+impl advent_of_code::graph::Graph<(Row, Col), (Row, Col)> for Map {
+ type Edges = advent_of_code::grid::Adjacent;
+
+ fn edges(&self, v: (Row, Col)) -> Self::Edges {
+ self.grid.adjacent(v.0, v.1, false)
+ }
+
+ fn edge(&self, v: (Row, Col), e: (Row, Col)) -> ((Row, Col), u64) {
+ (
+ e,
+ if self.grid[e.0][e.1] <= self.grid[v.0][v.1] + 1 {
+ 1
+ } else {
+ 999_999_999
+ },
+ )
+ }
+}
+
+pub fn parse(fh: File) -> Result<Map> {
+ let mut start = None;
+ let mut end = None;
+ let grid = parse::grid(parse::raw_lines(fh), |c, row, col| match c {
+ b'a'..=b'z' => c - b'a',
+ b'S' => {
+ start = Some((row, col));
+ 0
+ }
+ b'E' => {
+ end = Some((row, col));
+ b'z' - b'a'
+ }
+ _ => panic!("unknown map char '{c}'"),
+ });
+ Ok(Map {
+ grid,
+ start: start.expect("start not found"),
+ end: end.expect("end not found"),
+ })
+}
+
+pub fn part1(map: Map) -> Result<u64> {
+ Ok(map.dijkstra(map.start, map.end).0)
+}
+
+pub fn part2(map: Map) -> Result<u64> {
+ Ok(map
+ .grid
+ .indexed_cells()
+ .filter_map(|(pos, height)| {
+ if *height == 0 {
+ Some(map.dijkstra(pos, map.end).0)
+ } else {
+ None
+ }
+ })
+ .min()
+ .unwrap())
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2022, 12).unwrap()).unwrap()).unwrap(),
+ 504
+ );
+ assert_eq!(
+ part2(parse(parse::data(2022, 12).unwrap()).unwrap()).unwrap(),
+ 500
+ );
+}
diff --git a/src/bin/2022/main.rs b/src/bin/2022/main.rs
index 09a9248..c694d56 100644
--- a/src/bin/2022/main.rs
+++ b/src/bin/2022/main.rs
@@ -22,6 +22,7 @@ mod day6;
mod day7;
mod day8;
mod day9;
+mod day12;
// NEXT MOD
#[paw::main]
@@ -39,6 +40,7 @@ fn main(opt: Opt) -> Result<()> {
9 => advent_of_code::day!(2022, opt.day, opt.puzzle, day9),
10 => advent_of_code::day!(2022, opt.day, opt.puzzle, day10),
11 => advent_of_code::day!(2022, opt.day, opt.puzzle, day11),
+ 12 => advent_of_code::day!(2022, opt.day, opt.puzzle, day12),
// NEXT PART
_ => panic!("unknown day {}", opt.day),
}