diff --git a/README.md b/README.md index 39d1cb0..259fca9 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,9 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www. | [Day 6](./src/bin/06.rs) | `118.7µs` | `124.8µs` | | [Day 7](./src/bin/07.rs) | `91.8µs` | `72.4µs` | | [Day 8](./src/bin/08.rs) | `11.2ms` | `9.8ms` | +| [Day 9](./src/bin/09.rs) | `114.1µs` | `-` | -**Total: 23.40ms** +**Total: 23.51ms** --- diff --git a/data/examples/09.txt b/data/examples/09.txt new file mode 100644 index 0000000..cfe09c0 --- /dev/null +++ b/data/examples/09.txt @@ -0,0 +1,10 @@ +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3 + + diff --git a/src/bin/09.rs b/src/bin/09.rs new file mode 100644 index 0000000..51cad27 --- /dev/null +++ b/src/bin/09.rs @@ -0,0 +1,54 @@ +advent_of_code::solution!(9); + +use itertools::Itertools; + +fn parse_coords(input: &str) -> Vec<(i64, i64)> { + input + .lines() + .filter_map(|line| { + let line = line.trim(); + if line.is_empty() { + return None; + } + let (x, y) = line.split_once(',')?; + Some((x.trim().parse().ok()?, y.trim().parse().ok()?)) + }) + .collect() +} + +fn rectangle_area(p1: (i64, i64), p2: (i64, i64)) -> u64 { + let width = (p2.0 - p1.0).abs() + 1; + let height = (p2.1 - p1.1).abs() + 1; + (width * height) as u64 +} + +pub fn part_one(input: &str) -> Option { + let coords = parse_coords(input); + + coords + .iter() + .tuple_combinations() + .map(|(&p1, &p2)| rectangle_area(p1, p2)) + .max() +} + +pub fn part_two(input: &str) -> Option { + None +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_part_one() { + let result = part_one(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, Some(50)); + } + + #[test] + fn test_part_two() { + let result = part_two(&advent_of_code::template::read_file("examples", DAY)); + assert_eq!(result, None); + } +}