From 72d0c489f6ecf09c5fc99806a2a602c3c178cec9 Mon Sep 17 00:00:00 2001 From: mat ess Date: Sat, 3 Dec 2022 00:38:13 -0500 Subject: [PATCH] Day 3 --- inputs/3.small.txt | 6 + inputs/3.txt | 300 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 88 ++++++++++++- 3 files changed, 393 insertions(+), 1 deletion(-) create mode 100644 inputs/3.small.txt create mode 100644 inputs/3.txt diff --git a/inputs/3.small.txt b/inputs/3.small.txt new file mode 100644 index 0000000..f17e726 --- /dev/null +++ b/inputs/3.small.txt @@ -0,0 +1,6 @@ +vJrwpWtwJgWrhcsFMMfFFhFp +jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL +PmmdzqPrVvPwwTWBwg +wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn +ttgJtRGJQctTZtZT +CrZsJsPPZsGzwwsLwLmpwMDw diff --git a/inputs/3.txt b/inputs/3.txt new file mode 100644 index 0000000..e12b761 --- /dev/null +++ b/inputs/3.txt @@ -0,0 +1,300 @@ +NJvhJcQWTJWTNTFFMTqqGqfTmB +VwVzPldRZVLVRmfsvfjvqfmm +ZDPDHZHVcvDhbvnv +FHHwHBzzVCWWmmCzCPrVmgBwbLTtRFFbbbttRGRLjTcLpbbT +vhZZvdsNSdSMdNvjncppCLcLnGnj +CDZZsNZMZqdNSdlNZCqrzPHDzgrgzwVVWwmwwm +ndlndntsFJntFvccLjjLrjBShcBBfc +GpCGHzVwmmzqQWSSSfWHBhQL +mpCMGGCZVzVwGGVwmJsZnFtZnTSTJtdsvl +nCnPDGmDNmVCsVQDmGSWqvzchWSjjcWGqS +gTnBRLpfTRnrTdZgdLfRdrThvqcvWWhFFWvcFSSgjqqzjv +pfZfTMwrbLTTfsbmQtlVtHHnbs +wNdSdsbTvTZMTvTv +rrdRWdWQhFVdHWBGWQmmmnnMvCfmnhvmCmtZ +rJrVDRWpGddpbSlNSlspPP +chTNrthMMwWMTjfsmRzZszJpwm +BLnFFCngbc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l +lZPbhnZLRPnnPZZPdlGMBWcBMgMQHBBcvvvzBL +jpFjmwwwCDDbsjvjjgcvQgcNBQ +rbFmppbwhqhGRGZr +ggrLwFgWCBwbMWBbFwLMgNBZdmZHclJPllnJlNRPmSNZRR +ppszzDfhDfhsqpnvDVTfGpSPlPmclHcdRcZmmmdPPGSP +pvtDDVDVpqDfzDfngBLCwQrgCtCwFwrg +pbGjFFGGDjpbsGsmNhNFNRBBBtRhhhHv +JnczJVCvwWJvhPgghgNtNtNJ +nwVSSzdzzqSpvQSZQG +mssLLttQrsMrMzLCRmMmrrSQpvWpDNlBTBDlvNTccDQl +HdHJwJqVPwHnqJwbjJbGjnSgSTWPpNgWWpgBBgcvDWWN +ZHVwVZGwwdndqJVJqfHbGwnwrRLtLMftMvMMRrhmLMthhLmz +RgHGLbTqlZlPRZPHfvvfZttJnvfvjnzr +sVcChDVDccwNhhvjTvVzWJjnzFff +mpNcCMTCGmLqBLGH +wVJwHJHVMtMpBmDDWPQVPWDGDD +zCrlZzCblBvnCDWNGLmvGDLPNG +dqZglgbzrzbbgZqzTFSBHHFJSSSfjjSMfwhj +NMWJSjLMCnHHNMNNHWCHMbVVGBPZTrPVPBVDrBSDGTTr +zvttlFpgdtldwwvftPDPTWQdBZrsrWrGBZ +hFlFmhRFvfCbmWJWHcnj diff --git a/src/main.rs b/src/main.rs index 9084826..98aed56 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use std::cmp::Ordering; +use std::collections::HashSet; use std::fs; use anyhow::Error; @@ -22,7 +23,7 @@ fn main() -> Result<(), Error> { type Part = fn(String) -> u64; type Day = [Part; 2]; -const DAYS: usize = 2; +const DAYS: usize = 3; const SOLNS: [Day; DAYS] = [ [ // day 1 part 1 @@ -85,6 +86,34 @@ const SOLNS: [Day; DAYS] = [ .sum() }, ], + [ + // day 3 part 1 + |input| { + input + .lines() + .map(|line| { + let line = String::from(line); + let rs = Rucksack::new(line); + rs.score() + }) + .sum() + }, + // day 3 part 2 + |input| { + let lines: Vec<_> = input.lines().collect(); + lines + .chunks(3) + .map(|chunks| match chunks { + [f, s, t] => { + let rs = + Rucksacks::new(String::from(*f), String::from(*s), String::from(*t)); + rs.score() + } + _ => unreachable!("fooey"), + }) + .sum() + }, + ], ]; #[derive(Copy, Clone, PartialEq, Eq)] @@ -170,3 +199,60 @@ impl Outcome { } } } + +struct Rucksack { + first: HashSet, + second: HashSet, +} + +impl Rucksack { + fn new(mut input: String) -> Self { + assert!(input.len() % 2 == 0); + let second = input.split_off(input.len() / 2).chars().collect(); + Self { + first: input.chars().collect(), + second, + } + } + + fn overlap(&self) -> HashSet<&char> { + self.first.intersection(&self.second).collect() + } + + fn score(&self) -> u64 { + self.overlap().into_iter().copied().map(score_char).sum() + } +} + +struct Rucksacks { + first: HashSet, + second: HashSet, + third: HashSet, +} + +impl Rucksacks { + fn new(f: String, s: String, t: String) -> Self { + Rucksacks { + first: f.chars().collect(), + second: s.chars().collect(), + third: t.chars().collect(), + } + } + + fn intersection(&self) -> HashSet { + let start: HashSet<_> = self.first.intersection(&self.second).copied().collect(); + start.intersection(&self.third).copied().collect() + } + + fn score(&self) -> u64 { + self.intersection().into_iter().map(score_char).sum() + } +} + +fn score_char(c: char) -> u64 { + if c.is_lowercase() { + c as u64 - 96 + } else { + c as u64 - 38 + } +}