From 29186c44c0e35f074df9a11a794e44ed50008e4b Mon Sep 17 00:00:00 2001 From: Tobias Berger Date: Sat, 3 Dec 2022 09:37:29 +0100 Subject: [PATCH] Rust Day 3 --- rust/Cargo.lock | 2 +- rust/Cargo.toml | 8 +- rust/src/day03/input.txt | 300 +++++++++++++++++++++++++++++++++++++++ rust/src/day03/main.rs | 19 +++ rust/src/day03/part_1.rs | 36 +++++ rust/src/day03/part_2.rs | 23 +++ 6 files changed, 385 insertions(+), 3 deletions(-) create mode 100644 rust/src/day03/input.txt create mode 100644 rust/src/day03/main.rs create mode 100644 rust/src/day03/part_1.rs create mode 100644 rust/src/day03/part_2.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index dfa1114..9686bdd 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -4,4 +4,4 @@ version = 3 [[package]] name = "advent-of-code" -version = "22.2.2" +version = "22.3.2" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index d277742..f9d300e 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "advent-of-code" -version = "22.2.2" +version = "22.3.2" edition = "2021" resolver = "2" @@ -33,4 +33,8 @@ path = "src/day01/main.rs" [[bin]] name = "day02" -path = "src/day02/main.rs" \ No newline at end of file +path = "src/day02/main.rs" + +[[bin]] +name = "day03" +path = "src/day03/main.rs" \ No newline at end of file diff --git a/rust/src/day03/input.txt b/rust/src/day03/input.txt new file mode 100644 index 0000000..8039180 --- /dev/null +++ b/rust/src/day03/input.txt @@ -0,0 +1,300 @@ +NJvhJcQWTJWTNTFFMTqqGqfTmB +VwVzPldRZVLVRmfsvfjvqfmm +ZDPDHZHVcvDhbvnv +FHHwHBzzVCWWmmCzCPrVmgBwbLTtRFFbbbttRGRLjTcLpbbT +vhZZvdsNSdSMdNvjncppCLcLnGnj +CDZZsNZMZqdNSdlNZCqrzPHDzgrgzwVVWwmwwm +ndlndntsFJntFvccLjjLrjBShcBBfc +GpCGHzVw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jjlgqnTqZZZPlHnTngTTgn +BGqGqqFBFggjjdGHlj +QDhhLbDQCDFMNcmhRhqJNW +BnRnRvMnLGLSCHvvSnlRfWbbTNQJsJsbNbJTBfQT +tzMmmMwjhcpFjDmMcptrcjzFQggfQPTsWsfgNbbgfhJbPhQT +FdzcrtDwDMtcwtFGRZdRLvdnHRSZZv +HVpsSpvjpNjsBmbGFBnMNnDM +WRRWhZtfrVtLJrBZMnDmDbnZBTGF +thhPLzWzhzwPtLRLWrQlpPvvClcVcCppSvpl +lZPbhnZLRPnnPZZPdlGMBWcBMgMQHBBcvvvzBL +jpFjmwwwCDDbsjvjjgcvQgcNBQ +rbFmppbwhqhGRGZr +ggrLwFgWCBwbMWBbFwLMgNBZdmZHclJPllnJlNRPmSNZRR +ppszzDfhDfhsqpnvDVTfGpSPlPmclHcdRcZmmmdPPGSP +pvtDDVDVpqDfzDfngBLCwQrgCtCwFwrg +pbGjFFGGDjpbsGsmNhNFNRBBBtRhhhHv +JnczJVCvwWJvhPgghgNtNtNJ +nwVSSzdzzqSpvQSZQG +mssLLttQrsMrMzLCRmMmrrSQpvWpDNlBTBDlvNTccDQl +HdHJwJqVPwHnqJwbjJbGjnSgSTWPpNgWWpgBBgcvDWWN +ZHVwVZGwwdndqJVJqfHbGwnwrRLtLMftMvMMRrhmLMthhLmz +RgHGLbTqlZlPRZPHfvvfZttJnvfvjnzr +sVcChDVDccwNhhvjTvVzWJjnzFff +mpNcCMTCGmLqBLGH +wVJwHJHVMtMpBmDDWPQVPWDGDD +zCrlZzCblBvnCDWNGLmvGDLPNG +dqZglgbzrzbbgZqzTFSBHHFJSSSfjjSMfwhj +NMWJSjLMCnHHNMNNHWCHMbVVGBPZTrPVPBVDrBSDGTTr +zvttlFpgdtldwwvftPDPTWQdBZrsrWrGBZ +hFlFmhRFvfCbmWJWHcnj \ No newline at end of file diff --git a/rust/src/day03/main.rs b/rust/src/day03/main.rs new file mode 100644 index 0000000..48b07db --- /dev/null +++ b/rust/src/day03/main.rs @@ -0,0 +1,19 @@ +const INPUT: &str = include_str!("input.txt"); + +mod part_1; +use part_1::part_1; +mod part_2; +use part_2::part_2; + +pub(crate) fn character_priority(character: u8) -> u64 { + (match character { + b'a'..=b'z' => character - b'a' + 1, + b'A'..=b'Z' => character - b'A' + 27, + _ => panic!("Invalid character '{character}', no priority"), + }) as u64 +} + +pub fn main() { + part_1(INPUT); + part_2(INPUT); +} diff --git a/rust/src/day03/part_1.rs b/rust/src/day03/part_1.rs new file mode 100644 index 0000000..14f8fd0 --- /dev/null +++ b/rust/src/day03/part_1.rs @@ -0,0 +1,36 @@ +use std::collections::HashSet; + +pub(crate) fn part_1(input: &'static str) { + let rucksacks = input.lines(); + let compartments = rucksacks.map(|rucksack| { + let compartment_size = rucksack.len() / 2; + let compartments = ( + rucksack + .bytes() + .take(compartment_size) + .collect::>(), + rucksack + .bytes() + .skip(compartment_size) + .take(compartment_size) + .collect::>(), + ); + compartments + }); + let mut running_sum = 0; + for compartments in compartments { + let same_bytes = + HashSet::intersection(&compartments.0, &compartments.1).collect::>(); + assert_eq!( + same_bytes.len(), + 1, + "{}", + same_bytes + .iter() + .map(|byte| char::from(**byte)) + .collect::() + ); + running_sum += crate::character_priority(*same_bytes[0]); + } + println!("Part 1: {running_sum}"); +} diff --git a/rust/src/day03/part_2.rs b/rust/src/day03/part_2.rs new file mode 100644 index 0000000..ecbf423 --- /dev/null +++ b/rust/src/day03/part_2.rs @@ -0,0 +1,23 @@ +use std::collections::HashSet; + +use crate::character_priority; + +pub(crate) fn part_2(input: &'static str) { + let rucksacks = input.lines(); + let compartments = rucksacks + .map(|rucksack| rucksack.bytes().collect::>()) + .collect::>(); + assert_eq!(compartments.len() % 3, 0); + + let mut running_sum = 0; + for group_compartments in compartments.chunks_exact(3) { + let group_bytes = &group_compartments[0] + .intersection(&group_compartments[1]) + .copied() + .filter(|byte| group_compartments[2].contains(byte)) + .collect::>(); + assert_eq!(group_bytes.len(), 1); + running_sum += character_priority(group_bytes[0]); + } + println!("Part 2: {running_sum}"); +}