diff --git a/src/day05.rs b/src/day05.rs index a8da1dd..d8f3a24 100644 --- a/src/day05.rs +++ b/src/day05.rs @@ -108,8 +108,8 @@ mod test { #[test] fn test1() { - assert!(valid_book(&vec![97, 5, 13], &HashSet::from([(97, 13)]))); - assert!(!valid_book(&vec![13, 5, 97], &HashSet::from([(97, 13)]))); + assert!(valid_book(&[97, 5, 13], &HashSet::from([(97, 13)]))); + assert!(!valid_book(&[13, 5, 97], &HashSet::from([(97, 13)]))); assert_eq!(part1(TEST_INPUT), 143); } diff --git a/src/day06.rs b/src/day06.rs index a888521..b35b5f1 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -1,5 +1,5 @@ use std::{ - collections::{HashMap, HashSet}, + collections::HashSet, fs::read_to_string, }; @@ -60,21 +60,10 @@ fn part2(input: &str) -> RiddleResult { } fn parse(input: &str) -> (Grid, Coord) { - let mut m = HashMap::new(); - let mut pos = None; - input.lines().enumerate().for_each(|(y, line)| { - line.char_indices().for_each(|(x, c)| { - let x = x as i64; - let y = y as i64; - if c == '^' { - pos = Some((x, y)); - m.insert((x, y), '.'); - } else { - m.insert((x, y), c); - } - }); - }); - (Grid::from(m), pos.unwrap()) + let mut m = Grid::parse(input); + let start = m.entries().find(|(_, c)| **c == '^').unwrap().0; + m[start] = '.'; + (m, start) } fn is_loop(m: &Grid, block: Coord, mut pos: Coord, mut dir: char) -> bool { diff --git a/src/utils/grid.rs b/src/utils/grid.rs index 26b6560..bd76da7 100644 --- a/src/utils/grid.rs +++ b/src/utils/grid.rs @@ -4,6 +4,7 @@ use std::{ }; /// A grid structure, indexed by (x, y) tuples. The top-left coordinate is (0, 0). +#[derive(Eq, PartialEq, Debug)] pub struct Grid { content_width: i64, content_height: i64, @@ -54,6 +55,34 @@ impl Grid { pub fn contains_key(&self, c: Coord) -> bool { 0 <= c.0 && c.0 < self.content_width && 0 <= c.1 && c.1 < self.content_height } + + pub fn entries(&self) -> impl Iterator { + self.content.iter().enumerate().map(|(i, val)| { + ( + (i as i64 % self.content_width, i as i64 / self.content_width), + val, + ) + }) + // .collect_vec() + } +} + +impl Grid { + pub fn parse(input: &str) -> Grid { + let content_width = input.lines().next().unwrap().len(); + let mut content_height = 0; + let mut content = vec![]; + for line in input.lines() { + content_height += 1; + content.reserve(content_width); + line.chars().for_each(|v| content.push(v)); + } + Grid { + content_width: content_width as i64, + content_height, + content, + } + } } impl Index for Grid { @@ -73,9 +102,10 @@ impl IndexMut for Grid { #[cfg(test)] mod test { - use std::collections::HashMap; + use itertools::Itertools; use super::Grid; + use std::collections::HashMap; #[test] fn init_and_read() { @@ -90,6 +120,7 @@ mod test { assert_eq!(&'.', grid.get((1, 0))); assert_eq!(&'#', grid.get((1, 1))); } + #[test] fn mutate_by_index() { let mut grid = generate(); @@ -106,4 +137,24 @@ mod test { ])); grid } + + #[test] + fn from_str() { + let s = "..\n.x"; + let m_str = Grid::parse(s); + let mut m_gen = generate(); + m_gen[(1, 1)] = 'x'; + assert_eq!(m_gen, m_str); + } + + #[test] + fn entries() { + let v = vec![ + ((0, 0), &'.'), + ((1, 0), &'.'), + ((0, 1), &'.'), + ((1, 1), &'.'), + ]; + assert_eq!(v, generate().entries().collect_vec()); + } }