From 13e3a5202b82c34ce4afdf22e623a7fd84ab5c62 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Wed, 12 Dec 2018 23:35:34 -0500 Subject: [PATCH] Day 2 part 1 --- inputs/2.txt | 250 ++++++++++++++++++++++++++++++++++++++++++++++ inputs/2_test.txt | 7 ++ src/day2.rs | 43 ++++++++ src/main.rs | 3 + 4 files changed, 303 insertions(+) create mode 100644 inputs/2.txt create mode 100644 inputs/2_test.txt create mode 100644 src/day2.rs diff --git a/inputs/2.txt b/inputs/2.txt new file mode 100644 index 0000000..50dbef6 --- /dev/null +++ b/inputs/2.txt @@ -0,0 +1,250 @@ +cnjxpritdzhubeseewfmqagkul +cwyxpgitdzhvbosyewfmqagkul +cnfxpritdzhebosywwfmqagkul +cnjxpritdzgvbosyawfiqagkul +cnkxpritdzhvbosyewfmgagkuh +gnjxprhtdzhebosyewfmqagkul +cnjxpriedzevbosyewfjqagkul +cnjxpritdzhpyosyewfsqagkul +cnjxprltdzhvbosyewfmhagkzl +cnjxfritdjhvbosyewfmiagkul +xnjxpritdzhvbosyewfmqagkgn +cnjxpritdzmvzosyewfhqagkul +cljxxritdzhvbosyewfmragkul +cnjxjritdzhvbovyewfmvagkul +cnjxprdtdzhpbosyewvmqagkul +cojxprdtdzhzbosyewfmqagkul +cnjxpritgzhvfgsyewfmqagkul +knjxprptdzhvbosyecfmqagkul +cnjxpritdzhvbvsyeyfmqagkuc +cnjxpritdzhvbosvewfmoagjul +cnjxpritdzhvbosyfwfmbagkjl +cnjxpjitazhvbosfewfmqagkul +cnjtpfitdzhvbosyewfmiagkul +ckjxpritdzhvbysyewfmqagoul +cnjxvritdzhvbfsyewfmqalkul +cnjipqitdzhvbosyewfeqagkul +cnjhpritdzhvbosyewymqjgkul +cnjxprrtdzhvbosyewfmlkgkul +cnjxnritdzhvbopyewfmqaskul +cxjxpritdzhvtosyewjmqagkul +cnjxpritdzhvbjsyewfrqagkwl +cnjxhritdzhubosyewfmqagvul +cnjxpritdzhvbosyyyfmeagkul +cnjxkritdzhvaoeyewfmqagkul +cnjxpritdzhvtotyewfmqazkul +cnjxoriadzhvbosyewfmqcgkul +cnjxpritdzhcbosyewfmkapkul +fnjxprtddzhvbosyewfmqagkul +cnjxmvitdzhvbosyewfmqagrul +cnjxpyitdzhibosyewfmqagktl +cyjxprxtdzhvbosyewbmqagkul +onjxpditdzhvbosyeofmqagkul +cnjxprixdzhvbosuewftqagkul +cnjxpritdrhvaosyewymqagkul +cnjxpritdzhhbokyewfvqagkul +cnjxpritczhvbosyewfmqwgxul +cnjxpribdzqvbnsyewfmqagkul +ynpxpritdzhvbvsyewfmqagkul +cnjxprirdzhvboerewfmqagkul +cnjxpritdxhvbosyewfmgavkul +cnwxprntdzhvbosyewfmqagkuk +cnjxpritzzhvbosyewfmcagktl +bbjxpritdzhvbosyetfmqagkul +cnjxpbitdzhvbosyewrmqagkui +cnjxwrildzcvbosyewfmqagkul +cnqxpoitdzhvbosnewfmqagkul +cnzxpritdzhvbosyewfmqazkfl +cnjxpriddzhvoosyewfmhagkul +znjxpritdzhvbosjewfmqagkur +cnjxpritdzhvbosyewcmfagkuk +cnjxpritdzhvbomyywnmqagkul +cnjxpgitjzhvbosyejfmqagkul +cnjxpkitdzjvbosyewfmqcgkul +cnjxpritduhvbosyewfmqfkkul +cnfxpritdzhvbgsyewfmqwgkul +cnjxpritdzhvbosywufmqaskul +cnjxprittzhvboryswfmqagkul +cndxpritpzrvbosyewfmqagkul +cnjxpritdzhvbosyewfwqazkum +cccxprmtdzhvbosyewfmqagkul +cnjxpzitdzhvlbsyewfmqagkul +cnjxdrigdzhvbosyewfmqagsul +fhjxpritdzhvbosyewfmqagkcl +cnjxpritdzhvdosyewffqagaul +cnjxprikdztvbosyekfmqagkul +cnjxpritdzhvbohiewfmqagkue +cnjxpritdzhvbowyetfmqegkul +cnuxpritdzhvbosyewmmqapkul +qnjxpritdzhvbosyewfmjakkul +cnjxpritdzlvbosyewiaqagkul +cnjxpritdzhpoosyewfmvagkul +cdjxpritdzhvboryewfbqagkul +cnjxppitxzhvbosyewymqagkul +cnjxpywtdzhvboiyewfmqagkul +cnjxpritdzpvbosyezfmqaqkul +cnjppritdghvbosyewfdqagkul +cmjxpritdzhvbosvewfmqagkup +cnjxoritdzhvbosylffmqagkul +cnjxfritdzhvbojyewfmqagkvl +cnjxpritdzhvbozyewgmqlgkul +cnjxlritdzhvbosyewfmqalkug +cnyxprittzhvbosyewfmsagkul +cnjxprytdzcvdosyewfmqagkul +ctjxpritdzhvbosyedfmqagkil +cnjxpvitdzhrbosyewfmqaekul +cnyxyritdzhvbospewfmqagkul +cnjxoritwzhvbosyewrmqhgkul +cnjxpritdzhjbosyqwsmqagkul +cnjzprindzhvbosyewfmqabkul +cnjspritdzhvbosysffmqagkul +cnxxpritdzhvbosyelfmqageul +bnjhpritdzhvbosyewfmzagkul +cnjxbhitdzhdbosyewfmqagkul +cnjxprktdzmvbosyewfmqagkuj +cnjxprixdzhvbqsyewfmqmgkul +cnjxpkitdzhvbosyewfmqagbum +cnjhpritdzhxbosyewfmqagjul +cnjxpritdzzvbosyewuqqagkul +cnjxprhtdzhvuopyewfmqagkul +cnjxpritdzhjqosyewfmqagkgl +cnzxpritdzhvbosyejfmuagkul +cnvxpritoohvbosyewfmqagkul +cnjxpmitdzwvbosyemfmqagkul +cnjoprittzzvbosyewfmqagkul +cnjxpgitdzhvbosytwfmqsgkul +cnjxprrtdehvbosyewfnqagkul +onjxpjitdzgvbosyewfmqagkul +cnjxpmitdzhvbopaewfmqagkul +cnjxpritqzhvbosoewfrqagkul +cnjxpnitdzhvbosyewfmqagkjy +cnsxpritdzhvbosyewfmqjykul +cnjxpriidzhvbosyewfmqxgkut +cnjxpyitdzhnbosyewfgqagkul +cnjxpritdzhbboyyewfmqagsul +cnjxpeitdzsvbosyewfmqabkul +cnjxpritdzhzvosyewfmragkul +cnjrpritdzhmbosyewfmqrgkul +cnjxpritdzhmbosyenfmqaglul +cnjxqrntdzhvboswewfmqagkul +cnjxprdtpzhvbosyewfmqagkcl +cnjxpritdzhvsdsyewfmqagkur +cnjxpritdzhvvosyewumqhgkul +cnzxpritdznvhosyewfmqagkul +ynjypritdzhvbosyewfmqagkuz +cnjxpnitdzhvbocyezfmqagkul +vnjxpritdzhvbfsyewfmjagkul +cnjfpritdzhvbosyewfmqagkzu +cnjxpritdzhbbosyewfmlegkul +cnjxpnitdzhvbosyesfmbagkul +cnjxpritezwvbosyewfmqagkgl +cujxpritdzhqbosyawfmqagkul +cnjxprindzhrbosyerfmqagkul +cntxpritdzhvbosyewfmqauxul +cnjxpvitdzhvbosyepfmqagkuy +cnjxdrqtdzhvbosdewfmqagkul +cnnxpritdzhvvosyenfmqagkul +lnjxphitdzhvbosyewfaqagkul +cngxpritdzhhbobyewfmqagkul +uncxphitdzhvbosyewfmqagkul +cnjxpribdehvbosfewfmqagkul +cnjxppitdqhvbmsyewfmqagkul +gnjxpritkzhvbosyewfbqagkul +znjxpritdzhvbowycwfmqagkul +cnjxpgitdzhvbosyewidqagkul +cnjxhritdzhvbowyswfmqagkul +injxkritdzhvbjsyewfmqagkul +cmjupritgzhvbosyewfmqagkul +cnjxpritdzbvjoeyewfmqagkul +cnjxpritdkhvbosyewlmuagkul +cnkxpritdzhebvsyewfmqagkul +cyjxptitdzhvbosyewfmqagkuv +cnjxpritdzhvbodrewflqagkul +cnjxpratdzhvbksyewfhqagkul +cnjxpoitdzhvbosjewxmqagkul +cnjxprhidzhvbasyewfmqagkul +cnjxpritdzhvbosqewvmqagmul +cnjxoritdzhvbosyzifmqagkul +mnjxpritdzhvbcsyeyfmqagkul +cnjhpritgzhvbosyewfmqngkul +cnjxprijdzevbesyewfmqagkul +cnexprqtdzhvbosyewvmqagkul +cnjxpxitdzhvbosyawfmqmgkul +cnjxpritdzhvbosyirfmqaxkul +cqjxpcitdzhvboslewfmqagkul +cmjxpqitdztvbosyewfmqagkul +cnbxpritdzhvfosyewfmuagkul +cnjxprrtdzhvbosumwfmqagkul +cnjxprttdvhvbossewfmqagkul +cnjxpritdzhvbcsuewfaqagkul +cbjxpritdzhvbosyewfhqalkul +cnjxprithzhvbosjcwfmqagkul +chjxpritdzhvbosyewftcagkul +cnjxprirdchvdosyewfmqagkul +cnjxpritdxhvbosyewfmqcgkal +cnjxpriidchvbosrewfmqagkul +cnjhprizdzhvbosyewfmqagvul +cnjwpritdzhpbosyewfmqaqkul +cnjxpgitfzhvbosyxwfmqagkul +cnjxpjiedzhvbosywwfmqagkul +cnjxpritdzhvbosyewfpqynkul +xnixlritdzhvbosyewfmqagkul +cnjxoritdznvbosyehfmqagkul +cnjxpritdzhvbjsyewsmqagcul +lnjxpritdzhvkosyewjmqagkul +cnjxpritdzhvbosyedfiqvgkul +cnjxpritdzhqbdsyerfmqagkul +cnjxpritdzavbosyywfmqagvul +dmjxprithzhvbosyewfmqagkul +cnjxpriqdzhvnosyeofmqagkul +cnjxpritdxhvboszewfmqkgkul +cnjxpritdzxvbosjewymqagkul +cnjxpritdzngbosyewfmqugkul +cajxpritdnhvbosyerfmqagkul +cnsxpritdzhvbosymwfmqagcul +cnjxoritdzhvbosyewrmqhgkul +cnjxpritdzhvposyewfmqagkwo +cnjxpriazzhvbosyeufmqagkul +cnjxrritdzhvbosymhfmqagkul +cnjxprztdzhvbosyewfmqtgkum +cnjxpritdzhvbmsyewfmqatkun +cnuxpritdzhvbosyewfmqagvur +ctjxxritdzhvbosyewfvqagkul +cnjxpritdzlvbosyevfmqagkll +cnjxpritdzhlbosyewfmqagasl +cnjxpritwzhvbosyewfcxagkul +cyjxpritdzhfbosyewfmqagcul +cnjxpritxghvkosyewfmqagkul +ctjxpritdjhvbosyewfmqkgkul +cnjxpritxzhvbosyewjmbagkul +unjxpritdzhkbosyewfmqaghul +cnjoprqtdzhvbosyewzmqagkul +rnjxprgtgzhvbosyewfmqagkul +cnjgpqitdzhvbosyewfaqagkul +cnjxpritdzuybosyewfmqagful +cnjxprqtdahvbosyewfnqagkul +cnjxpritdzhmkhsyewfmqagkul +wnjxpritdzhvbosiewfmqagkml +cnjmpritdzhvbosyjwfmqagkdl +cnjopritdzhvbksyewfmqrgkul +cnlxpritdzhvbosyewfmomgkul +cgjxpritdzhvbbsyewfmxagkul +cnaxpritdvhvnosyewfmqagkul +cnjxprijdzhvbkmyewfmqagkul +cnjxpritdzhvposyewzmqagkuz +cnuxpuitdzdvbosyewfmqagkul +cnjxprifdzjvbosyewfyqagkul +cnhspritdzhvbosyewfmqaghul +cnjxprcbdzfvbosyewfmqagkul +lnjapritdzhvbosyewfmqegkul +cnjxprisszhvbosyewqmqagkul +cnjxpritdzhvbosyeifmsagoul +cnjxpritrfhvbosyewfmqagkuz +cnjxkritdzmvboqyewfmqagkul +cnjxpritdzhvbosyedfmqzgkzl +cnjxprifdzhvbosyswfmqagksl +cnjxoritdzhvbosyxwfmhagkul +cnjhpritdzzvbosfewfmqagkul +cnjxprityjhvbomyewfmqagkul +cnjbpritdzhvbosyywfmqagkuf +cnjxprrtdzhvbosyewgmqagtul diff --git a/inputs/2_test.txt b/inputs/2_test.txt new file mode 100644 index 0000000..c4576be --- /dev/null +++ b/inputs/2_test.txt @@ -0,0 +1,7 @@ +abcdef +bababc +abbcde +abcccd +aabcdd +abcdee +ababab diff --git a/src/day2.rs b/src/day2.rs new file mode 100644 index 0000000..0fbe7e1 --- /dev/null +++ b/src/day2.rs @@ -0,0 +1,43 @@ +use std::error::Error; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::collections::HashMap; + +const INPUT: &str = "inputs/2.txt"; + +pub fn solve() -> Result> { + calculate_checksum(INPUT) +} + +fn calculate_checksum(filename: &str) -> Result> { + let mut two_count = 0; + let mut three_count = 0; + let file = File::open(filename)?; + for line in BufReader::new(file).lines() { + let mut char_map: HashMap = HashMap::new(); + for c in line?.chars() { + let current_count = char_map.get(&c).unwrap_or(&0); + char_map.insert(c, current_count + 1); + } + + if char_map.values().any(|&count| count == 2) { + two_count += 1 + } + if char_map.values().any(|&count| count == 3) { + three_count += 1 + } + } + Ok(two_count * three_count) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "inputs/2_test.txt"; + + #[test] + fn calculates_correct_checksum() { + assert_eq!(calculate_checksum(TEST_INPUT).unwrap(), 12); + } +} diff --git a/src/main.rs b/src/main.rs index e1ea972..816f178 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ mod day1; +mod day2; fn main() { println!("Day 1:"); println!("{}", day1::solve().unwrap()); + println!("Day 2:"); + println!("{}", day2::solve().unwrap()); }