From 83c4ba70478aa7ce523254b774b16fb773a6a01f Mon Sep 17 00:00:00 2001 From: JamesPatrickGill Date: Tue, 3 Dec 2024 19:44:23 +0000 Subject: [PATCH] day 2 --- Cargo.lock | 16 +++++++ Cargo.toml | 1 + README.md | 9 ++-- data/examples/02.txt | 6 +++ src/bin/02.rs | 106 +++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 129 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9504be6..e8bbce6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,7 @@ version = "0.11.0" dependencies = [ "chrono", "dhat", + "itertools", "pico-args", "tinyjson", ] @@ -126,6 +127,12 @@ dependencies = [ "thousands", ] +[[package]] +name = "either" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + [[package]] name = "gimli" version = "0.28.1" @@ -155,6 +162,15 @@ dependencies = [ "cc", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" diff --git a/Cargo.toml b/Cargo.toml index 038a1a3..a80ffa7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ test_lib = [] # Template dependencies chrono = { version = "0.4.38", optional = true } dhat = { version = "0.3.3", optional = true } +itertools = "0.13.0" pico-args = "0.5.0" tinyjson = "2.5.1" diff --git a/README.md b/README.md index ab5b3b6..f16200a 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,12 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. - ## Benchmarks -| Day | Part 1 | Part 2 | -| :----------------------: | :------: | :------: | +| Day | Part 1 | Part 2 | +| :---: | :---: | :---: | | [Day 1](./src/bin/01.rs) | `40.7µs` | `58.2µs` | +| [Day 2](./src/bin/02.rs) | `117.1µs` | `205.3µs` | -**Total: 0.10ms** - +**Total: 0.42ms** diff --git a/data/examples/02.txt b/data/examples/02.txt index e69de29..b49c10d 100644 --- a/data/examples/02.txt +++ b/data/examples/02.txt @@ -0,0 +1,6 @@ +7 6 4 2 1 +1 2 7 8 9 +9 7 6 2 1 +1 3 2 4 5 +8 6 4 4 1 +1 3 6 7 9 diff --git a/src/bin/02.rs b/src/bin/02.rs index 3bae35c..7e23508 100644 --- a/src/bin/02.rs +++ b/src/bin/02.rs @@ -1,11 +1,109 @@ +use std::collections::VecDeque; + +use itertools::Itertools; + advent_of_code::solution!(2); pub fn part_one(input: &str) -> Option { - None + let reports = input + .lines() + .map(|line| { + line.split(" ") + .map(|a| a.parse::().unwrap()) + .collect_vec() + }) + .collect_vec(); + + let mut bad_reports = 0; + for report in reports.iter() { + let mut dir = 0; + for idx in 0..report.len() - 1 { + let l = report.get(idx).unwrap(); + let r = report.get(idx + 1).unwrap(); + match r.cmp(l) { + std::cmp::Ordering::Less => { + if dir == 1 || l - r > 3 { + bad_reports += 1; + break; + } + dir = -1; + } + std::cmp::Ordering::Equal => { + bad_reports += 1; + break; + } + std::cmp::Ordering::Greater => { + if dir == -1 || r - l > 3 { + bad_reports += 1; + break; + } + dir = 1; + } + } + } + } + Some((reports.len() - bad_reports) as u32) } pub fn part_two(input: &str) -> Option { - None + let reports = input + .lines() + .map(|line| { + line.split(" ") + .map(|a| a.parse::().unwrap()) + .collect_vec() + }) + .collect_vec(); + + let mut good_reports = 0; + for report in reports.iter() { + let mut to_check_queue = VecDeque::new(); + to_check_queue.push_back(report); + while !to_check_queue.is_empty() { + if is_safe(report) { + good_reports += 1 + } else { + for idx in 0..report.len() { + let mut new_report = report.clone(); + new_report.remove(idx); + if is_safe(&new_report) { + good_reports += 1; + break; + } + } + }; + + to_check_queue.pop_front(); + } + } + + Some(good_reports) +} + +fn is_safe(report: &[i32]) -> bool { + let mut dir = 0; + for idx in 0..report.len() - 1 { + let l = report.get(idx).unwrap(); + let r = report.get(idx + 1).unwrap(); + match r.cmp(l) { + std::cmp::Ordering::Less => { + if dir == 1 || l - r > 3 { + return false; + } + dir = -1; + } + std::cmp::Ordering::Equal => { + return false; + } + std::cmp::Ordering::Greater => { + if dir == -1 || r - l > 3 { + return false; + } + dir = 1; + } + } + } + true } #[cfg(test)] @@ -15,12 +113,12 @@ mod tests { #[test] fn test_part_one() { let result = part_one(&advent_of_code::template::read_file("examples", DAY)); - assert_eq!(result, None); + assert_eq!(result, Some(2)); } #[test] fn test_part_two() { let result = part_two(&advent_of_code::template::read_file("examples", DAY)); - assert_eq!(result, None); + assert_eq!(result, Some(4)); } }