diff --git a/2022/rust/src/day8.rs b/2022/rust/src/day8.rs index eddc104..bda45c9 100644 --- a/2022/rust/src/day8.rs +++ b/2022/rust/src/day8.rs @@ -7,9 +7,9 @@ type Result = usize; fn processed_input(input: &str) -> Input { let mut result = vec![]; - for l in input.lines() { + for l in input.trim().lines() { let mut v = vec![]; - for b in l.bytes() { + for b in l.trim().bytes() { v.push(b - 48); } result.push(v); @@ -19,6 +19,7 @@ fn processed_input(input: &str) -> Input { fn part1(input: &Input) -> Result { let mut trees: HashSet<(usize, usize)> = HashSet::new(); + let mut add = |y, x| { println!( "tree @ row {}, col {} ({})", @@ -28,44 +29,53 @@ fn part1(input: &Input) -> Result { ); trees.insert((y, x)); }; + let h = input.len(); let w = input[0].len(); - for y in 0..h { + println!("width: {}, height: {}", w, h); + + for y in 1..=h - 2 { let row = &input[y]; let mut tallest_left = row[0]; let mut tallest_right = row[w - 1]; + println!( + "scan left-to-right at row {} (tl: {}, tr: {})", + y, tallest_left, tallest_right + ); let mut xd = 1; - add(y, 0); - add(y, w - 1); - while xd < w { + while xd < w - 1 { + println!("{},{}", y, xd); if row[xd] > tallest_left { tallest_left = row[xd]; add(y, xd); } - let rx = w - 1 - xd; - if row[rx] > tallest_right { - tallest_right = row[rx]; - add(y, rx); + let rxd = w - 1 - xd; + if row[rxd] > tallest_right { + tallest_right = row[rxd]; + add(y, rxd); } xd += 1; } } - for x in 0..w { + for x in 1..=w - 2 { let mut tallest_top = input[0][x]; let mut tallest_bottom = input[h - 1][x]; + println!( + "scan top-to-bottom at column {} (tt: {}, tb: {})", + x, tallest_top, tallest_bottom + ); let mut yd = 1; - add(0, x); - add(h - 1, x); - while yd < h { + while yd < h - 1 { + println!("{},{}", yd, x); if input[yd][x] > tallest_top { tallest_top = input[yd][x]; add(yd, x); } - let ry = h - 1 - yd; - if input[yd][ry] > tallest_bottom { - tallest_bottom = input[yd][ry]; - add(ry, x); + let ryd = h - 1 - yd; + if input[ryd][x] > tallest_bottom { + tallest_bottom = input[ryd][x]; + add(ryd, x); } yd += 1; } @@ -73,7 +83,7 @@ fn part1(input: &Input) -> Result { println!("{:?}", trees); - trees.len() + trees.len() + (2 * (h - 1)) + (2 * (w - 1)) } fn part2(input: &Input) -> Result { @@ -85,7 +95,8 @@ fn main() { eprintln!("{}\n\nAbove is your input file.\n\n", input_text); let input = processed_input(&input_text); common::show_answers(&part1(&input), &part2(&input)) - // 1755 for part 1 is too high + // 1755 for part 1 is too high (so is 1750 in case off-by-one-ish) and so is 1745 + // 1705 is wrong // common::show_both_answers(&both_parts(&input)) }