grid: use coord2d more consistently
This commit is contained in:
		@@ -25,7 +25,7 @@ impl TrailMap {
 | 
			
		||||
            .iter()
 | 
			
		||||
            .enumerate()
 | 
			
		||||
            .filter(|(_, v)| **v == b'0')
 | 
			
		||||
            .map(|(i, _v)| self.map.coord(i as i64).unwrap())
 | 
			
		||||
            .map(|(i, _v)| self.map.coord(i as i64).unwrap().into())
 | 
			
		||||
            .collect_vec()
 | 
			
		||||
    }
 | 
			
		||||
    fn count_reachable_from(&self, pos: &(i64, i64), needle: u8, visited: &mut Grid<bool>) -> u64 {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								src/day16.rs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/day16.rs
									
									
									
									
									
								
							@@ -1,5 +1,5 @@
 | 
			
		||||
use aoc_runner_derive::aoc;
 | 
			
		||||
use grid::Grid;
 | 
			
		||||
use grid::{Coord2d, Grid};
 | 
			
		||||
use std::{
 | 
			
		||||
    cmp::Ordering,
 | 
			
		||||
    collections::{BinaryHeap, HashMap},
 | 
			
		||||
@@ -108,10 +108,10 @@ impl Maze {
 | 
			
		||||
            })
 | 
			
		||||
    }
 | 
			
		||||
    fn dijkstra(&self) -> usize {
 | 
			
		||||
        let (start_x, start_y) = self.map.find(&b'S').expect("can't find start");
 | 
			
		||||
        let Coord2d {x: start_x, y: start_y} = self.map.find(&b'S').expect("can't find start");
 | 
			
		||||
        let start = (start_x as CoordType, start_y as CoordType);
 | 
			
		||||
 | 
			
		||||
        let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish");
 | 
			
		||||
        let Coord2d {x: finish_x, y: finish_y} = self.map.find(&b'E').expect("can't find finish");
 | 
			
		||||
        let finish = (finish_x as CoordType, finish_y as CoordType);
 | 
			
		||||
 | 
			
		||||
        let mut distances = HashMap::new();
 | 
			
		||||
@@ -149,10 +149,11 @@ impl Maze {
 | 
			
		||||
        usize::MAX
 | 
			
		||||
    }
 | 
			
		||||
    fn path_dijkstra(&mut self) -> (usize, Vec<Vec<Coord>>) {
 | 
			
		||||
        let (start_x, start_y) = self.map.find(&b'S').expect("can't find start");
 | 
			
		||||
        let start = (start_x.try_into().unwrap(), start_y.try_into().unwrap());
 | 
			
		||||
        let (finish_x, finish_y) = self.map.find(&b'E').expect("can't find finish");
 | 
			
		||||
        let finish = (finish_x.try_into().unwrap(), finish_y.try_into().unwrap());
 | 
			
		||||
        let Coord2d {x: start_x, y: start_y} = self.map.find(&b'S').expect("can't find start");
 | 
			
		||||
        let start = (start_x as CoordType, start_y as CoordType);
 | 
			
		||||
 | 
			
		||||
        let Coord2d {x: finish_x, y: finish_y} = self.map.find(&b'E').expect("can't find finish");
 | 
			
		||||
        let finish = (finish_x as CoordType, finish_y as CoordType);
 | 
			
		||||
 | 
			
		||||
        let mut distances = HashMap::new();
 | 
			
		||||
        let mut queue = BinaryHeap::with_capacity(self.map.data.len());
 | 
			
		||||
 
 | 
			
		||||
@@ -118,7 +118,7 @@ fn part1_impl(input: &str, cheat_min: usize) -> i64 {
 | 
			
		||||
    let track = parse(input);
 | 
			
		||||
    let start = track.map.find(&b'S').unwrap();
 | 
			
		||||
    let goal = track.map.find(&b'E').unwrap();
 | 
			
		||||
    let (best_path, costs) = track.path_costs(start, goal);
 | 
			
		||||
    let (best_path, costs) = track.path_costs(start.into(), goal.into());
 | 
			
		||||
    let cheats = track.find_cheats(&best_path, &costs, cheat_min);
 | 
			
		||||
 | 
			
		||||
    cheats.len() as i64
 | 
			
		||||
 
 | 
			
		||||
@@ -104,7 +104,7 @@ impl<T: BufRead> From<T> for Map {
 | 
			
		||||
        let guard_facing = FacingDirection::Up;
 | 
			
		||||
        Self {
 | 
			
		||||
            grid,
 | 
			
		||||
            guard_pos,
 | 
			
		||||
            guard_pos: guard_pos.into(),
 | 
			
		||||
            guard_facing,
 | 
			
		||||
            visited_from,
 | 
			
		||||
            path: Vec::new(),
 | 
			
		||||
 
 | 
			
		||||
@@ -42,9 +42,9 @@ impl AntennaMap {
 | 
			
		||||
                // permutations generates both pairs, ie. ((1,2),(2,1)) and ((2,1),(1,2)) so we don't need
 | 
			
		||||
                // to consider the 'negative' side of the line, which will be generated by the other pair
 | 
			
		||||
                let (a, b) = (pair[0], pair[1]);
 | 
			
		||||
                let offset = (a.0 - b.0, a.1 - b.1);
 | 
			
		||||
                let offset = (a.x - b.x, a.y - b.y);
 | 
			
		||||
                for i in (start..).map_while(|i| if Some(i - start) != reps { Some(i as i64) } else { None }) {
 | 
			
		||||
                    let node_pos = (a.0 + i * offset.0, a.1 + i * offset.1);
 | 
			
		||||
                    let node_pos = (a.x + i * offset.0, a.y + i * offset.1);
 | 
			
		||||
                    if antinodes.set(&node_pos, true).is_none() {
 | 
			
		||||
                        // left the grid
 | 
			
		||||
                        break;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user