Skip to content

Instantly share code, notes, and snippets.

@bokenator
Created December 8, 2022 17:24
Show Gist options
  • Save bokenator/4c6a83b3985267a34f7321bdd5d13018 to your computer and use it in GitHub Desktop.
Save bokenator/4c6a83b3985267a34f7321bdd5d13018 to your computer and use it in GitHub Desktop.
use crate::common::get_input;
use std::{cmp::Ordering, iter};
fn directed_range(a: i32, b: i32) -> impl Iterator<Item = i32> {
let mut start = a;
let end = b;
iter::from_fn(move || match start.cmp(&end) {
Ordering::Less => {
start += 1;
Some(start - 1)
}
Ordering::Equal => None,
Ordering::Greater => {
start -= 1;
Some(start + 1)
}
})
}
pub async fn part1() -> i32 {
let input = get_input("https://adventofcode.com/2022/day/8/input")
.await
.lines()
.map(|line| {
line.chars()
.map(|char| {
let char = &format!("{}", char);
char.parse::<i32>().unwrap()
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let mut counter = 0;
for i in (0..input.len()) {
for j in (0..input[0].len()) {
if i == 0 || i == input.len() - 1 || j == 0 || j == input[0].len() {
counter += 1;
} else {
let tree = input[i][j];
let north_trees = (0..i).map(|k| input[k][j]).collect::<Vec<_>>();
let west_trees = (0..j).map(|k| input[i][k]).collect::<Vec<_>>();
let south_trees = (i + 1..input.len())
.map(|k| input[k][j])
.collect::<Vec<_>>();
let east_trees = (j + 1..input[0].len())
.map(|k| input[i][k])
.collect::<Vec<_>>();
if [north_trees, west_trees, south_trees, east_trees]
.iter()
.any(|trees| trees.iter().all(|n| n < &tree))
{
counter += 1;
}
}
}
}
counter
}
pub async fn part2() -> i32 {
let input = get_input("https://adventofcode.com/2022/day/8/input")
.await
.lines()
.map(|line| {
line.chars()
.map(|char| {
let char = &format!("{}", char);
char.parse::<i32>().unwrap()
})
.collect::<Vec<_>>()
})
.collect::<Vec<_>>();
let coordinates = (0..input.len())
.map(|row_index| {
(0..input[0].len())
.map(|col_index| (row_index, col_index))
.collect::<Vec<_>>()
})
.flatten()
.collect::<Vec<_>>();
coordinates
.iter()
.map(|(i, j)| {
let i = *i;
let j = *j;
let tree = input[i][j];
let north_trees = directed_range(i as i32 - 1, -1)
.fold((0, false), |acc, k| {
if acc.1 {
return acc;
}
let north_tree = input[k as usize][j];
if north_tree < tree {
(acc.0 + 1, false)
} else {
(acc.0 + 1, true)
}
})
.0;
let west_trees = directed_range(j as i32 - 1, -1)
.fold((0, false), |acc, k| {
if acc.1 {
return acc;
}
let west_tree = input[i][k as usize];
if west_tree < tree {
(acc.0 + 1, false)
} else {
(acc.0 + 1, true)
}
})
.0;
let south_trees = directed_range(i as i32 + 1, input.len() as i32)
.fold((0, false), |acc, k| {
if acc.1 {
return acc;
}
let south_tree = input[k as usize][j];
if south_tree < tree {
(acc.0 + 1, false)
} else {
(acc.0 + 1, true)
}
})
.0;
let east_trees = directed_range(j as i32 + 1, input[0].len() as i32)
.fold((0, false), |acc, k| {
if acc.1 {
return acc;
}
let east_tree = input[i][k as usize];
if east_tree < tree {
(acc.0 + 1, false)
} else {
(acc.0 + 1, true)
}
})
.0;
north_trees * west_trees * south_trees * east_trees
})
.max()
.unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment