summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-12-08 00:28:08 -0500
committerJesse Luehrs <doy@tozt.net>2022-12-08 00:28:08 -0500
commit335dfe990b043e6695a0bb463694cf4dfc44f17a (patch)
treec5a3d6bbdfdaa46a6219451e0311ede18574fe0f /src
parentf0c5a56065fdd9cab0b2bd7992e0fb08c7161a44 (diff)
downloadadvent-of-code-335dfe990b043e6695a0bb463694cf4dfc44f17a.tar.gz
advent-of-code-335dfe990b043e6695a0bb463694cf4dfc44f17a.zip
day 8
Diffstat (limited to 'src')
-rw-r--r--src/2022/8/mod.rs142
-rw-r--r--src/2022/mod.rs4
2 files changed, 146 insertions, 0 deletions
diff --git a/src/2022/8/mod.rs b/src/2022/8/mod.rs
new file mode 100644
index 0000000..59f561c
--- /dev/null
+++ b/src/2022/8/mod.rs
@@ -0,0 +1,142 @@
+#![allow(dead_code)]
+#![allow(unused_variables)]
+
+use crate::prelude::*;
+
+pub fn parse(fh: File) -> Result<Grid<u8>> {
+ Ok(parse::digit_grid(parse::lines(fh)))
+}
+
+pub fn part1(trees: Grid<u8>) -> Result<i64> {
+ let mut total = 0;
+ for row in (0..trees.rows().0).map(Row) {
+ 'tree: for col in (0..trees.cols().0).map(Col) {
+ let tree = trees[row][col];
+
+ if (0..col.0).map(Col).all(|col| trees[row][col] < tree) {
+ total += 1;
+ continue 'tree;
+ }
+ if ((col.0 + 1)..trees.cols().0)
+ .map(Col)
+ .all(|col| trees[row][col] < tree)
+ {
+ total += 1;
+ continue 'tree;
+ }
+ if (0..row.0).map(Row).all(|row| trees[row][col] < tree) {
+ total += 1;
+ continue 'tree;
+ }
+ if ((row.0 + 1)..trees.rows().0)
+ .map(Row)
+ .all(|row| trees[row][col] < tree)
+ {
+ total += 1;
+ continue 'tree;
+ }
+ }
+ }
+ Ok(total)
+}
+
+pub fn part2(trees: Grid<u8>) -> Result<i64> {
+ let mut max = 0;
+ for row in (0..trees.rows().0).map(Row) {
+ for col in (0..trees.cols().0).map(Col) {
+ let tree = trees[row][col];
+
+ let mut seen = false;
+ let left = (0..col.0)
+ .map(Col)
+ .rev()
+ .take_while(|col| {
+ if seen {
+ return false;
+ }
+
+ let other = trees[row][*col];
+ if other < tree {
+ true
+ } else {
+ seen = true;
+ true
+ }
+ })
+ .count();
+
+ let mut seen = false;
+ let right = ((col.0 + 1)..trees.cols().0)
+ .map(Col)
+ .take_while(|col| {
+ if seen {
+ return false;
+ }
+
+ let other = trees[row][*col];
+ if other < tree {
+ true
+ } else {
+ seen = true;
+ true
+ }
+ })
+ .count();
+
+ let mut seen = false;
+ let up = (0..row.0)
+ .map(Row)
+ .rev()
+ .take_while(|row| {
+ if seen {
+ return false;
+ }
+
+ let other = trees[*row][col];
+ if other < tree {
+ true
+ } else {
+ seen = true;
+ true
+ }
+ })
+ .count();
+
+ let mut seen = false;
+ let down = ((row.0 + 1)..trees.rows().0)
+ .map(Row)
+ .take_while(|row| {
+ if seen {
+ return false;
+ }
+
+ let other = trees[*row][col];
+ if other < tree {
+ true
+ } else {
+ seen = true;
+ true
+ }
+ })
+ .count();
+
+ let scenic = left * right * up * down;
+ if scenic > max {
+ max = scenic;
+ }
+ }
+ }
+ Ok(max.try_into().unwrap())
+}
+
+#[test]
+fn test() {
+ assert_eq!(
+ part1(parse(parse::data(2022, 8).unwrap()).unwrap()).unwrap(),
+ 1851
+ );
+ assert_eq!(
+ part2(parse(parse::data(2022, 8).unwrap()).unwrap()).unwrap(),
+ 574080
+ );
+}
diff --git a/src/2022/mod.rs b/src/2022/mod.rs
index c6beea8..e6faf0c 100644
--- a/src/2022/mod.rs
+++ b/src/2022/mod.rs
@@ -14,6 +14,8 @@ mod day5;
mod day6;
#[path = "7/mod.rs"]
mod day7;
+#[path = "8/mod.rs"]
+mod day8;
// NEXT MOD
pub fn run(day: u8, puzzle: u8) -> Result<i64> {
@@ -33,6 +35,8 @@ pub fn run(day: u8, puzzle: u8) -> Result<i64> {
(6, 2) => day6::part2(day6::parse(parse::data(2022, 6)?)?),
(7, 1) => day7::part1(day7::parse(parse::data(2022, 7)?)?),
(7, 2) => day7::part2(day7::parse(parse::data(2022, 7)?)?),
+ (8, 1) => day8::part1(day8::parse(parse::data(2022, 8)?)?),
+ (8, 2) => day8::part2(day8::parse(parse::data(2022, 8)?)?),
// NEXT PART
_ => Err(anyhow!("unknown puzzle {}-{}", day, puzzle)),
}