Day 2: Red-Nosed Reports
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://blocks.programming.dev if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/22323136
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
I forgot that this started yesterday, so I’m already behind. I quite like my solution for part one, but part two will have to wait.
Rust
use color_eyre::eyre; use std::{fs, num, str::FromStr}; #[derive(Debug, PartialEq, Eq)] struct Report(Vec<isize>); impl FromStr for Report { type Err = num::ParseIntError; fn from_str(s: &str) -> Result<Self, Self::Err> { let v: Result<Vec<isize>, _> = s .split_whitespace() .map(|num| num.parse::<isize>()) .collect(); Ok(Report(v?)) } } impl Report { fn is_safe(&self) -> bool { let ascending = self.0[1] > self.0[0]; let (low, high) = if ascending { (1, 3) } else { (-3, -1) }; self.0.windows(2).all(|w| { let a = w[0]; let b = w[1]; b >= a + low && b <= a + high }) } } fn main() -> eyre::Result<()> { color_eyre::install()?; let part1 = part1("d02/input.txt")?; println!("Part 1: {part1}"); Ok(()) } fn part1(filepath: &str) -> eyre::Result<isize> { let mut num_safe = 0; for l in fs::read_to_string(filepath)?.lines() { if Report::from_str(l)?.is_safe() { num_safe += 1; } } Ok(num_safe) } #[cfg(test)] mod tests { use super::*; #[test] fn sample_part1() { assert_eq!(part1("test.txt").unwrap(), 2); } }