diff options
Diffstat (limited to '2023/day03.rs')
| -rw-r--r-- | 2023/day03.rs | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/2023/day03.rs b/2023/day03.rs new file mode 100644 index 0000000..be02594 --- /dev/null +++ b/2023/day03.rs @@ -0,0 +1,168 @@ +#![allow(dead_code)] +#![allow(unused_variables)] +#![allow(unused_mut)] +use std::collections::HashMap; +use std::fs::File; +use std::io::Read; +// use std::cmp::max; +// use std::cmp::min; + +fn is_symbol(c: char) -> bool { + if c.is_digit(10) || c == '.' { + false + } else { + true + } +} + +fn is_valid(x: usize, y: usize, m: &Vec<Vec<char>>) -> bool { + for dy in -1i32..2 { + for dx in -1i32..2 { + let mut xx: usize; + let mut yy: usize; + if x == 0 && dx == -1 { + xx = x; + } else if x == m[0].len() - 1 && dx == 1 { + xx = x; + } else { + xx = ((x as i32) + dx) as usize; + } + + if y == 0 && dy == -1 { + yy = y; + } else if y == m.len() - 1 && dy == 1 { + yy = y; + } else { + yy = ((y as i32) + dy) as usize; + } + // println!("{} {} {} {} {} {}", x, y, dx, dy, xx, yy); + // print!("{}", m[y][x]); + + if is_symbol(m[yy][xx]) { + return true; + } + } + } + return false; +} + +fn main() { + // let filename = "in/day03.ref"; + let filename = "in/day03.pzl"; + + let mut f = File::open(filename).expect("cannot open file"); + let mut content = String::new(); + f.read_to_string(&mut content).expect("cannot read file"); + let lines: Vec<_> = content.trim_end().split('\n').collect(); + // println!("{:?}", lines); + + let mut res1 = 0; + let mut res2 = 0; + + let mut m: Vec<Vec<char>> = Vec::new(); + for line in &lines { + let mut l: Vec<char> = Vec::new(); + for c in line.bytes() { + l.push(c as char) + } + m.push(l) + } + + let mut hm: HashMap<(usize, usize), (u32, u32)> = HashMap::new(); + let mut id: u32 = 0; + for y in 0..m.len() { + let mut have_num = false; + let mut num_valid = false; + let mut start_x = 0; + for x in 0..m[0].len() { + if have_num { + if m[y][x].is_digit(10) == true && x == (m[0].len() - 1) { + if num_valid { + let num: u32 = String::from_iter(m[y][start_x..=x].iter()).parse().unwrap(); + for xx in start_x..=x { + hm.insert((xx, y), (id, num)); + } + id += 1; + res1 += num; + } + have_num = false; + num_valid = false; + } else if m[y][x].is_digit(10) == false { + if num_valid { + let num: u32 = String::from_iter(m[y][start_x..x].iter()).parse().unwrap(); + for xx in start_x..x { + hm.insert((xx, y), (id, num)); + } + id += 1; + res1 += num; + } + have_num = false; + num_valid = false; + } + } else if m[y][x].is_digit(10) { + have_num = true; + start_x = x; + } + + if have_num { + num_valid = num_valid || is_valid(x, y, &m); + } + } + } + + println!("{:?}", hm); + + for y in 0..m.len() { + for x in 0..m[0].len() { + if m[y][x] == '*' { + // println!("{} {}", y+1, x+1); + let mut neighbour: HashMap<u32, u32> = HashMap::new(); + for dy in -1i32..2 { + for dx in -1i32..2 { + let mut xx: usize; + let mut yy: usize; + if x == 0 && dx == -1 { + xx = x; + } else if x == m[0].len() - 1 && dx == 1 { + xx = x; + } else { + xx = ((x as i32) + dx) as usize; + } + + if y == 0 && dy == -1 { + yy = y; + } else if y == m.len() - 1 && dy == 1 { + yy = y; + } else { + yy = ((y as i32) + dy) as usize; + } + if let Some((id, val)) = hm.get(&(xx, yy)) { + neighbour.insert(*id, *val); + } + } + } + + if neighbour.len() == 2 { + let mut mul: u32 = 1; + for (key, val) in neighbour.iter() { + mul *= val; + } + // res2 += neighbour.iter().map(|(_, val)| val).reduce(|a,b| a*b).unwrap(); + println!("{}", mul); + res2 += mul; + } + // let a: u32 = neighbour.iter() + // .inspect(|x| println!("asdf {:?}", x)) + // .map(|(_, val)| val) + // .reduce(|a,b| a*b); + // println!("{:?}", a); + + // println!("{:?}", neighbour); + // asdf + } + } + } + + println!("res1: {}", res1); + println!("res2: {}", res2); +} |
