From 59c1563ace3881b0d5886064a7f28cdd1783f738 Mon Sep 17 00:00:00 2001 From: Andrew Coleman Date: Tue, 4 Dec 2018 09:05:45 -0500 Subject: [PATCH] day 2 solution --- 2018/day2/Cargo.lock | 14 +++ 2018/day2/Cargo.toml | 7 ++ 2018/day2/input | 250 ++++++++++++++++++++++++++++++++++++++++++ 2018/day2/src/main.rs | 65 +++++++++++ 4 files changed, 336 insertions(+) create mode 100644 2018/day2/Cargo.lock create mode 100644 2018/day2/Cargo.toml create mode 100644 2018/day2/input create mode 100644 2018/day2/src/main.rs diff --git a/2018/day2/Cargo.lock b/2018/day2/Cargo.lock new file mode 100644 index 0000000..0c3e8b9 --- /dev/null +++ b/2018/day2/Cargo.lock @@ -0,0 +1,14 @@ +[[package]] +name = "day2" +version = "0.1.0" +dependencies = [ + "edit-distance 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "edit-distance" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum edit-distance 2.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3bd26878c3d921f89797a4e1a1711919f999a9f6946bb6f5a4ffda126d297b7e" diff --git a/2018/day2/Cargo.toml b/2018/day2/Cargo.toml new file mode 100644 index 0000000..84b26aa --- /dev/null +++ b/2018/day2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day2" +version = "0.1.0" +authors = ["Andrew Coleman "] + +[dependencies] +edit-distance = "2.0.0" diff --git a/2018/day2/input b/2018/day2/input new file mode 100644 index 0000000..5785f74 --- /dev/null +++ b/2018/day2/input @@ -0,0 +1,250 @@ +qywzphxoiseldjrbaoagvkmanu +qywzphxeisulpjrbtfcgvkmanu +qywzxhooiseldjrbtfcgvcmanu +qywzphjojseldjubtfcgvkmanu +qtwzphxoieeldjrbtfcgvrmanu +tywzphzoiseldjritfcgvkmanu +qyuzphxoiseldjrbtfcgykbanu +qswzmhxoiseldjrbtfcgvkaanu +qyczqhxoiseldjrbtfcgvkbanu +qybzpqxooseldjrbtfcgvkmanu +qywzpoxoxseldjrbtfcgvpmanu +qywzphxbipeldjrbtfcgvkmaru +qywzpmxoiseldjrutqcgvkmanu +qywzphxoisesdjrrtfchvkmanu +qywzphxoiseldhrbtfcgukyanu +jywzphyoiseldjrbtfclvkmanu +qywzvhxoiselrjrbtfcgvkmanl +qywzplxojseldjrbtfcgvkmamu +qywzphxoiseldjrbtacgvkmvnd +qywpphxoiseldjrbtfcgvkvenu +qqazphxoiselqjrbtfcgvkmanu +qyozvhxoiseldjrbtfcgvkmadu +xywzphxliseldjhbtfcgvkmanu +qdwzphioiseldqrbtfcgvkmanu +qswyphxoiseldjrbtfcgvkmanx +qowzshxoiseldjrbtfigvkmanu +qywzphxoiseldjjbtfcgvkmawq +qyhzphxoiseldjrbtfvgvkmynu +qxwzphxoiselmjrbtfcgvqmanu +qywzphxoiceldjrbtfcgikmgnu +qywephxoiseldjrbthcgvkmane +yywzphxoisejdjrotfcgvkmanu +qywzxhxoisyldyrbtfcgvkmanu +qywzpjxoiseqdjrbtfcgvwmanu +qywzphxoqweldprbtfcgvkmanu +qywzphnoiseldjrbtfcamkmanu +qylgphxoiseldjrbtfcgvkmauu +qysdphxomseldjrbtfcgvkmanu +lywzpoxoikeldjrbtfcgvkmanu +qywzphxgiseldjrbtfeqvkmanu +qywzdhxozseldjcbtfcgvkmanu +qywzphxeiskedjrbtfcgvkmanu +qmwzphxoispldjrbtfcgvkmany +qywzphxoiselzcrbtfcgvkmanz +qywzphxoisxodjrbtscgvkmanu +qywzphxoiseldjrrtfcgmkmaeu +aywzphxoiseldjrbtfmjvkmanu +qywzthxoiiewdjrbtfcgvkmanu +qywzpsxoisrzdjrbtfcgvkmanu +qywzphxoiseldjkbtqcgvkmank +tywzphxoisewdjkbtfcgvkmanu +qywkchxoiseldjrbtfcghkmanu +qywzphxoiseldjoftfrgvkmanu +qywzpixoisehdjrbtfcgvkmacu +qywzchooiseldarbtfcgvkmanu +qywyphtoaseldjrbtfcgvkmanu +qywyphxotsbldjrbtfcgvkmanu +qywzphxoiseldjrbhsggvkmanu +dywzphxoiseldjrktfegvkmanu +qywzphxoiseldjrmtfcgvkcenu +qywcphxjiseldjybtfcgvkmanu +qywzphxoiseldhrbsfogvkmanu +qmwzpheoiseldjrrtfcgvkmanu +qywzrhxoiselpjrbtfcgvemanu +qyezphpoiseldjrbtfcgvdmanu +qywzphxvisewdjrbtfcgvkmdnu +qywzphkoiseldjrbtfcgvkmdnh +qywzehxoiseldfrbtpcgvkmanu +qywzphxoiseldjrrtfcgvrdanu +qpwzphxoizeldjqbtfcgvkmanu +qywzphxojseldjrbtmcgvkmvnu +vywzphxoiseldjrbtfcgvkmaop +sywzphxoiselbjrttfcgvkmanu +qywwphkoiseldjrbtfcivkmanu +qlwkpcxoiseldjrbtfcgvkmanu +qywzphxoiyesdjrbtfcgvkmvnu +qywzphxoiseldjrbofcgrkmrnu +qywzphxyiseldjrstfcgvkmjnu +qywzphaoiseldtrbnfcgvkmanu +qywzphxhisuldurbtfcgvkmanu +qywzphxdiseldjrbtvugvkmanu +qywzpzxbiseldjrbtfcgukmanu +qyrzphxoixeldjrbtfcgvumanu +qywzphxoiberdjrptfcgvkmanu +qywzphxfiskldjrbtfcgvkmdnu +qyxzphxoiseldjrdhfcgvkmanu +qywzphrqiseldjrbtfcgvbmanu +qyezphxviseldjrbtfcgvkmani +qywgphxoiseldgkbtfcgvkmanu +qywzphxoileldjrbtgcgvkdanu +qywzphxoiseldnrbtfcwvkmpnu +qywzphxoiseidjrbmfcqvkmanu +qywzpkxoiselijrbtfcgvlmanu +mywzphxoiveldjrbtfcgvkmunu +qywzphxooseddjrbtfcgpkmanu +qywzphxokseldjritfcyvkmanu +qywzxhxoiseldjrbtfqgvcmanu +qywzphxoisfldjrbpfcgvkmaju +qywuphxgiseldjrbffcgvkmanu +qywzphociseldjrbtfcgvkuanu +qywzphxoiseldvrbtftgckmanu +qywzpbxoisrldjrbtfngvkmanu +qywzphxoiseldjrntfygvdmanu +qywzphxviseldkrbtfcgvkianu +qywpphxgiseldjrbtfctvkmanu +qywzphxoicewdjrbtfcgvsmanu +qywzpcxoiseldjmbtfcgvcmanu +qrwzphxoiseldjrbtfcgjumanu +qywzphxoiselojrbtfcgxkmaau +qywzphxojsbldjrbtfcgykmanu +oywzphxoiseldjrbtfqgvkmvnu +qywfphxpiseldjrbtfckvkmanu +qyyzwhxwiseldjrbtfcgvkmanu +qywzphxgiseldjrbtfchvkmabu +qywzphxfiseldjrbtfcgukoanu +qywzpdxoisyldjrbtfcgvkxanu +dyuzphxoiseldjrbtfcgvkmamu +qywzphxoiseldjrbifcgvkmnnp +qywzpyxoiseldjrbtfcgvklano +dywzphxoiieldjrbtfcgvwmanu +qywzphxoihemdjrbtfcgvdmanu +gywzphxoxseldvrbtfcgvkmanu +qywzqhxoissldjwbtfcgvkmanu +eywzphxoiieldjrbtfcgekmanu +qyizprxciseldjrbtfcgvkmanu +qywjphxoiseldjrbtfcgckmano +qywznhxoiseldjrbrfcgvkmagu +qywzphxoisrldjdbvfcgvkmanu +qyyxphxoiseldjrbtwcgvkmanu +qywzphxoiseldjdbtfcpvkmjnu +qywzvhxqiseldjrbofcgvkmanu +vywzphxoiseldjrbtfcgckwanu +qywzphgbiseldjrbtfcgvkmazu +qcwzphxoiseldjrbqfcgvkmdnu +qywzphxoismldjrbtfcgkkmznu +qywhphxoiseldjrbtccgvkmane +qywzphzoiseldjrbtfcgvqmauu +hywzphxoiseldjrbtfcuvkmanc +qywzphxozsejdgrbtfcgvkmanu +qyszphxoiseldjrntfygvdmanu +qywzphxoisgldjrbtfcgvklaru +qywzhhxoiseldjrbtscgvkmqnu +qywjphxpiqeldjrbtfcgvkmanu +qywzphxoiseldxrptfclvkmanu +qywlphxoisehdjrbtfcgvkmanc +qydzpfxoiseldjrwtfcgvkmanu +qywzphxoiseldjrbtxcgqkfanu +qywophxoiselfjrbtfcgvkmani +qywzyhxoiszldtrbtfcgvkmanu +qywzphxoxseldfrntfcgvkmanu +qywzphloiseldjqbtfcgvkmtnu +qywzpuxoiseldorbtfcgvkeanu +qywzphxoiueldjrwdfcgvkmanu +qgwzphxoiseldjmbtncgvkmanu +qywzphtggseldjrbtfcgvkmanu +qywzphxoisrldjrbtfmgvhmanu +qfszphxoiseldjqbtfcgvkmanu +qywzphxpisjldjrbxfcgvkmanu +qywznhxoisepdjrbtfqgvkmanu +qywzphioiseldjabtfcgxkmanu +qyizphxaiseldjrbtfcgvkmaxu +xywzphxoiqelvjrbtfcgvkmanu +quwzphxoiseldjretfcgvkmaau +bywzphxoiseldjrbtucgckmanu +jywzphxoiseldjrbofcgvkmani +qywzphxoiseltjkbtfcgvkmabu +eywzphxoiselgjrbtfkgvkmanu +qywzphxoisengjrttfcgvkmanu +qywzphzoiseldjrbtfcgvkmknk +quwdphxoiseldjrbtfcxvkmanu +qcwzzhxoiseldjrgtfcgvkmanu +qywgphxdiseldjrbtfcgjkmanu +qywzpdxoivefdjrbtfcgvkmanu +qywzphxoiseldjrbtfdgvjmpnu +qfwzphxoiseydkrbtfcgvkmanu +qywzphxdiqelqjrbtfcgvkmanu +qywzvhxoiseldjrbtfognkmanu +qywzphgoiseldjrbcfcgvtmanu +qywophxoiseldjrbtpcgvkmank +qywzphxoiszldjretfcgvkmabu +qywzphxoiseldjhbtfcgvxmawu +qcgzphxoiselejrbtfcgvkmanu +qywzphxoisepdjrbtfcfvkcanu +qivzphxniseldjrbtfcgvkmanu +qywzhhxoiseldjrftxcgvkmanu +qyazphxciseldjrbtfcgskmanu +qywzphxoisoldgrbtfczvkmanu +qywzmhxoiseldurbwfcgvkmanu +qywzphxoistldjrbwfcgvkranu +qywzphxoistedjrbtfcgokmanu +qywzqhxodsecdjrbtfcgvkmanu +qywzphxtisxldjrbtfcgvkhanu +qywzphxoeseldjrtrfcgvkmanu +qdwzphxoioeldjrbtfigvkmanu +qjwzphxoisbldjrbtfcgvkmanz +qywzphxoiseldbrbtfdgvlmanu +qywzphxoiselddrbhvcgvkmanu +zywzppxoiseldjrdtfcgvkmanu +qywzppxqiselkjrbtfcgvkmanu +qywzphxoihelbjrbtfcgvkmabu +qywzphxoiseldjreyfcgvknanu +qywzphxxrseldjrbtfcgvkmagu +qywhpfxoiseldjrbtfdgvkmanu +qywzphxoisxldjrdtfagvkmanu +oywzphxxiseldjrbtfcgvkmaeu +qywzphxoiselqirbtfvgvkmanu +qywzphxoqseldhrbtfcgvkhanu +qywzphxokseldjrbtfxgvkmaju +qywzphtoiseldurbtfcfvkmanu +qywzphxoiheudjrrtfcgvkmanu +qrwzphxzigeldjrbtfcgvkmanu +qywzphxoiseldorbtfcgvxmvnu +qywzphxoisaldjpbqfcgvkmanu +qywuphxoiselljrbtfchvkmanu +qywzphxoisaldjrbefcgvkmsnu +qywzphxoiteldjrbnfcgvkmanp +qywhphxoiselqjrbtfcgvkmagu +qywzjhxoisejdjrbtfcgvkmanr +qywephaoiseldjrbtfcavkmanu +qywcphxoireldjqbtfcgvkmanu +qywzphxoiseldirbuicgvkmanu +qywzphxoisecvnrbtfcgvkmanu +qcwzphxoiseldjrbtfcgvrmaiu +qywnphxoiseldjrntftgvkmanu +qywzphxhisyldjrbtfcgvkmafu +qyhzphxoiseldjrytfcgvkmanq +vjwzbhxoiseldjrbtfcgvkmanu +qyvzpsxoisuldjrbtfcgvkmanu +qywzphxaiseldcrbefcgvkmanu +qywzphxoiseldjgbtfsgvkfanu +oiwzphxoiseldjrbtfcgvkmcnu +qyezphxoiseldjrbtfcqvkmjnu +tywzphxriseldzrbtfcgvkmanu +yywzphxoiseldjbbtfugvkmanu +qywzpfxviseldjrbttcgvkmanu +qywzphxoiceldcrbtfugvkmanu +qymzphxoiseldjratfcsvkmanu +qywzphxoiselxjrbdfcgvkpanu +qywzphxoiselujrbtfkgvimanu +qywzshyoiseldjrbtfcgpkmanu +qywzphxoiselfjrbtfsgvkmant +qywpphxoiseldjxbtfcyvkmanu +qywzfhxoiselqjrptfcgvkmanu +qewzphxoiseldprbtfcgvkmand +qywfphxoiseldlrbtfcgvkmgnu +qywzphxoiseldjhbtqcovkmanu +fywzphxoiseldlrbtfcgvkjanu +sywzphxoiseldjrbhfccvkmanu +qywzphxoiseldjfbtfcrvkmpnu +sywzphxoisrldjrbtfczvkmanu diff --git a/2018/day2/src/main.rs b/2018/day2/src/main.rs new file mode 100644 index 0000000..d2a3695 --- /dev/null +++ b/2018/day2/src/main.rs @@ -0,0 +1,65 @@ +extern crate edit_distance; + +use std::collections::HashMap; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use edit_distance::edit_distance; + +fn get_char_counts(input: &String) -> (bool, bool) { + let mut has_two = false; + let mut has_three = false; + let mut map = HashMap::new(); + for char in input.chars() { + map.entry(char).and_modify(|x| *x += 1).or_insert(1); + } + for (_char, count) in map { + if has_two && has_three { + break; + } + if count == 2 { + has_two = true; + } else if count == 3 { + has_three = true; + } + } + (has_two, has_three) +} + +fn calc_checksum(lines: &Vec) { + let mut two_count = 0; + let mut three_count = 0; + for line in lines.iter() { + let (has_two, has_three) = get_char_counts(line); + if has_two { + two_count += 1; + } + if has_three { + three_count += 1; + } + } + println!("checksum: {}", two_count * three_count); +} + +fn find_single_char_difference(lines: &mut Vec) { + let line = lines.pop().expect("ran out of lines to find the single char difference!"); + let mut found = false; + for other in lines.iter() { + if edit_distance(&line, other) == 1 { + found = true; + println!("{} and {}", line, other); + break; + } + } + if !found { + find_single_char_difference(lines); + } +} + +fn main() { + let mut lines: Vec = BufReader::new(File::open("input").unwrap()) + .lines() + .map(|line| line.unwrap()) + .collect(); + calc_checksum(&lines); + find_single_char_difference(&mut lines); +}