Skip to content

Commit

Permalink
day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesPatrickGill committed Dec 3, 2024
1 parent 19a9e9c commit 83c4ba7
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 9 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
<!--- advent_readme_stars table --->

<!--- benchmarking table --->

## 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**
<!--- benchmarking table --->
6 changes: 6 additions & 0 deletions data/examples/02.txt
Original file line number Diff line number Diff line change
@@ -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
106 changes: 102 additions & 4 deletions src/bin/02.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,109 @@
use std::collections::VecDeque;

use itertools::Itertools;

advent_of_code::solution!(2);

pub fn part_one(input: &str) -> Option<u32> {
None
let reports = input
.lines()
.map(|line| {
line.split(" ")
.map(|a| a.parse::<i32>().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<u32> {
None
let reports = input
.lines()
.map(|line| {
line.split(" ")
.map(|a| a.parse::<i32>().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)]
Expand All @@ -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));
}
}

0 comments on commit 83c4ba7

Please sign in to comment.