diff options
| author | Shivesh Mandalia <mail@shivesh.org> | 2022-12-20 23:33:51 +0000 |
|---|---|---|
| committer | Shivesh Mandalia <mail@shivesh.org> | 2022-12-20 23:33:51 +0000 |
| commit | a667c19882e1feb542a122781b4efe5545dea0dd (patch) | |
| tree | d7f8a0aa645ad5181995e93381e5c0cf9122cd42 /day04a/src | |
| parent | 5941c6c20ab337194514a5d2bf2b9b9c8b0b3d32 (diff) | |
| download | advent_of_code_2022-a667c19882e1feb542a122781b4efe5545dea0dd.tar.gz advent_of_code_2022-a667c19882e1feb542a122781b4efe5545dea0dd.zip | |
complete day 4
Diffstat (limited to 'day04a/src')
| -rw-r--r-- | day04a/src/main.rs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/day04a/src/main.rs b/day04a/src/main.rs new file mode 100644 index 0000000..9ce72f9 --- /dev/null +++ b/day04a/src/main.rs @@ -0,0 +1,99 @@ +/// --- Day 4: Camp Cleanup --- +/// +/// Space needs to be cleared before the last supplies can be unloaded from the ships, and so +/// several Elves have been assigned the job of cleaning up sections of the camp. Every section +/// has a unique ID number, and each Elf is assigned a range of section IDs. +/// +/// However, as some of the Elves compare their section assignments with each other, they've +/// noticed that many of the assignments overlap. To try to quickly find overlaps and reduce +/// duplicated effort, the Elves pair up and make a big list of the section assignments for each +/// pair (your puzzle input). +/// +/// For example, consider the following list of section assignment pairs: +/// +/// ``` +/// 2-4,6-8 +/// 2-3,4-5 +/// 5-7,7-9 +/// 2-8,3-7 +/// 6-6,4-6 +/// 2-6,4-8 +/// ``` +/// +/// For the first few pairs, this list means: +/// +/// Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and +/// 4), while the second Elf was assigned sections 6-8 (sections 6, 7, 8). +/// The Elves in the second pair were each assigned two sections. +/// The Elves in the third pair were each assigned three sections: one got sections 5, 6, and +/// 7, while the other also got 7, plus 8 and 9. +/// +/// This example list uses single-digit section IDs to make it easier to draw; your actual list +/// might contain larger numbers. Visually, these pairs of section assignments look like this: +/// +/// .234..... 2-4 +/// .....678. 6-8 +/// +/// .23...... 2-3 +/// ...45.... 4-5 +/// +/// ....567.. 5-7 +/// ......789 7-9 +/// +/// .2345678. 2-8 +/// ..34567.. 3-7 +/// +/// .....6... 6-6 +/// ...456... 4-6 +/// +/// .23456... 2-6 +/// ...45678. 4-8 +/// +/// Some of the pairs have noticed that one of their assignments fully contains the other. For +/// example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6. In pairs where one +/// assignment fully contains the other, one Elf in the pair would be exclusively cleaning sections +/// their partner will already be cleaning, so these seem like the most in need of +/// reconsideration. In this example, there are 2 such pairs. +/// +/// In how many assignment pairs does one range fully contain the other? +use clap::Parser; +use itertools::Itertools; + +use std::fs::File; +use std::io::prelude::*; +use std::io::BufReader; +use std::path::PathBuf; + +const FILEPATH: &'static str = "examples/input.txt"; + +#[derive(Parser, Debug)] +#[clap(author, version, about, long_about = None)] +struct Cli { + #[clap(short, long, default_value = FILEPATH)] + file: PathBuf, +} + +fn main() { + let args = Cli::parse(); + + let file = File::open(&args.file).unwrap(); + let reader = BufReader::new(file); + let res = reader + .lines() + .map(|l| { + l.unwrap() + .split(',') + .map(|s| { + s.split('-') + .map(|v| v.parse::<usize>().unwrap()) + .collect_tuple::<(usize, usize)>() + .unwrap() + }) + .collect_tuple::<((usize, usize), (usize, usize))>() + .unwrap() + }) + .filter(|((l1, l2), (r1, r2))| (l1 <= r1 && l2 >= r2) || (r1 <= l1 && r2 >= l2)) + .count(); + + println!("{res}"); +} |
