From 3b877bf4cc667eb8bcc787d145203600a4dba2d2 Mon Sep 17 00:00:00 2001 From: Prefetch Date: Sat, 25 Feb 2023 11:41:27 +0100 Subject: Initial commit --- d06/Cargo.toml | 6 + d06/input.txt | 572 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ d06/src/main.rs | 63 +++++++ 3 files changed, 641 insertions(+) create mode 100644 d06/Cargo.toml create mode 100644 d06/input.txt create mode 100644 d06/src/main.rs (limited to 'd06') diff --git a/d06/Cargo.toml b/d06/Cargo.toml new file mode 100644 index 0000000..193d51a --- /dev/null +++ b/d06/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "d06" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/d06/input.txt b/d06/input.txt new file mode 100644 index 0000000..c223f71 --- /dev/null +++ b/d06/input.txt @@ -0,0 +1,572 @@ +drhqjkbv +dtmukohn +pblnptvr +rggqrqre +ihknljci +ncoeigib +qbmbrgzv +zfahdmvg +mblaahfc +irafzbak +wghpbuwg +pyijzopp +dxpqsjkd +nhrosahg +ekfkwiqi +khgwbxsz +fkkinvci +xlmjkxql +rvzsdcve +josyoqlp +wnyzmbhh +bvkkwyxp +zdnlhnlv +hbgmztkb +yugdjalz +ittbvkxm +pbnxevdt +fptnvjdb +dsypeozk +gcjmtlcc +pqsnuztm +meilciol +uolptcbc +jueofzif +mryrumjt +jxmdacbz +plraftrm +ljgrwfiz +ligncqig +hrcmfkwa +kraqruef +mawxovke +fgwermvq +ipfzrpoz +rhfgebjy +ltqkdjag +dvgdhywz +ojnzjack +ktnqjzlc +uvcyfogs +anqjxrbb +rqedwxhi +iliuzgff +lanioopk +kbcwlndi +wuzfguwq +oclgdxdn +euqmnjxb +rwgdgwac +gepgmuuw +xmsfhugr +cmmgvoza +mrdnpirq +muorgsxa +awdmmpth +abwkhkot +hlztzuhm +qwbxfktf +gkdslbhk +rqppdpfj +juuuxmwh +sprwgdoi +eptrqcap +fdtoubid +lnkxrjrr +yxxiuuzb +kcbgcuno +ssszanxv +xajngqze +dqprwcbs +tmehzhsh +eeooqhko +eacbmenw +qpbwiznc +hoofmvah +noicaaqv +reqwbpnl +ctfbzydf +kkjvsvob +pwkramtw +txsebrtx +wpxomtjx +xwcxfihp +iabaswyj +ubvgfrsw +yhxvrlwg +kyighkhi +kfmcjeci +wtxvnufi +eggyqgwj +azywqbdk +egwprvgp +gxtgzqek +hbypquhd +oqbpykib +bsaybmgo +ctzhjzgf +uqcatiea +fkquufln +mzuepzcn +rwteqddf +uandcifd +chqjvavf +xrugbecb +bynqmlhg +keruzmmy +tpxbrwkw +czawkslt +duunzdgl +mhhrgsfi +lhiorqvn +uroxbpki +uyqvmdvr +wbvzxinc +hvjbywqh +opztxiye +jnkvbsss +awfhhjuh +wldwnsms +vzdtdfzz +qcgjdsxf +ncungtbd +bsfmblxe +iztafyde +omxfacnb +bfjgzohh +idowhvnt +etxcropc +dlebejbz +dpgrvvyg +yyetaiyd +zmvvslxw +iolevdzw +pwrbpwyt +ncxnbdcs +bizuessl +csomkqnr +gukypdxo +zzazkgze +mhygipbc +nidnvzql +oblusiue +rnuvqhpy +ybeycdic +sbzmvzxq +gvvqcrug +zkdknqod +zkztprqv +xlprotcp +sndzhrmt +cntdipcw +siiqfres +pnfcbzjo +twbgdnrv +zzxnngor +nmcmxqgt +jayxwvrm +pahclzsw +ztddtnyo +odidwauv +tzlaantg +nttefszm +jahhbgpt +pvxsgjsl +gewlnlqm +bykgsqzo +zqrrhrkl +isxyqlbv +wfzkqgvt +mcwizlzj +ugtgyrez +qfyzoall +cfyqxoyx +vdszizjm +bgrcpovm +einnyfdv +uugncaps +letylmon +ocltwlem +owfskawk +hvripclk +jmgrulzy +kjxyhhle +ionkwbuu +tmjsudwf +kxocxtoa +ejbuoost +nzqdzsdd +dkuiiisp +dxwhxbvj +ghipctmd +mwmcfifl +vfptvuxo +zfqsjsbx +peazobjt +nlwomnpb +ndxbvgqn +zonnhffl +vjnyiejx +ghrcsxgl +fsdiwdmy +jmhtpgis +sscjmpev +oivfuctm +zwtllrfo +uuzpmnjx +wgpccyiu +ujhilevq +hjjieaag +dfbfylpe +ypegnmyg +vjnjrgcu +enylcycn +ptlneeqo +nfiplyjo +dxcrmicb +vdxohgvl +iycioixk +rwqrjyyu +rfatusnv +mnyvdlhu +piyhcljq +edcykiom +wrtwvqub +ejtepubt +vegruhiu +qmrlbprr +wntjsebz +hkdkuasw +jxkfblck +knrwttyx +pmjitnry +lrtbermt +qkhtmeoe +skmqdpek +fmxlqqhn +ureaitwq +qufcyfph +wcidvwgt +ybkrmqem +qhuacrls +yvnsqqdb +xpjchodx +lcwzkemd +jokzbvsi +jjbklvqq +xfswceep +xsjwsymu +slmsyksa +oaquyavd +zxteczie +yglonpuu +rdfvsbno +wgxhuxga +kvpetmdr +fjeoobow +atdqjabp +prjazwst +yqkisdog +wvswvdtm +iogvloma +srxeqnqq +ipjfezkx +joonxwtz +yjovntqa +svbbkcvw +mgyseuqr +dxflpkvp +gqbiytmk +siccxtsn +oepstegr +uwjjhhqb +eosfjfhv +dsaqqhda +vudzwxak +njzjiowv +anfpqwsz +geuqffcr +vhhbkgeb +flkqpzbn +fgtdspvd +yjhxwcps +aimfdnpv +hhvklxlo +yjoxsxhj +bllophbc +ntclfhgs +gtnsuqdp +dazhoeap +klczitkw +tlkedeuy +cvbuidmk +vjubfgqg +qimvfpxg +hnqegigv +cppyezxe +czcmiytj +ypvezoca +adjxiooc +mdshbmjd +urthwyqf +dhoijcrh +vxnnyszn +ttzkydfs +lhnbywji +tyiuyhxa +fyryagxi +hboupxaq +urctvbue +cirtbbfu +bkoxlmkm +rdeoosjs +qemhixen +zqfioppk +vopwlhhe +gmpihxop +aamsrrzs +sdyssprk +hmrqkghm +oevtvzwl +bqufyyuu +ennrxvaf +slmshjpz +qgraeety +mczjxfan +wzwmupvu +sqbkhwxg +abbotwty +trafaoli +bkuarvfz +wuffaong +fqevpper +eekwoblz +spsztgee +yjfbfeif +qcjdtsez +omujkwzt +vjpfndxp +hkpxvjix +eiigrazh +jmtdqwuu +jnsfvufm +xhkdzgjf +rvqigugc +bivqnzgu +ydadmvyq +ghsohaqa +eulugttl +nvhaafrh +ikdtvxpu +fbmztykr +gmhluyfq +biovnlho +xinmgiwl +xsvlnvnr +gryuussb +eeqmavbo +fuftdkgb +wwmwybtx +txshabuj +ewxcrjmj +lskdajks +pabjhzen +xzfmdhaj +gojxghyk +iyqaryra +bnlovokf +mehlaadw +tzqhtnhv +kgacrpdt +qxawodku +fcrouumv +yzqxkmgi +xzspfhmp +omhlnexu +olstosyp +gxslgwcn +juamcglq +irdvybpr +ncsacfpd +hczoulhg +kqjpowtq +bzefqjnj +qnrtwygz +rztxjfyr +lcpxloro +gdibhhkc +qgwuyhea +uweyjukp +hsljwmyy +ayhxycnx +klzvtttr +lgdmpcww +cqvtjkyw +rfyjuybh +tskdjzrt +mhwspcvf +xplpnemj +lrjvgjgs +crllldzy +ikczhybc +oolwtfoj +pqfgligu +cdgktmmx +jbpatjkl +keqepeax +qfirnsdi +juyzjarn +tuymbnri +jijsmffx +xfnbxvzo +oxmscdkf +hkfcgeuv +mkkuxffi +vjmccxrb +wrpagtbd +wcnmlred +pafkwcph +tcvjkxyl +stvhdkom +cfzhzuif +avdzxwyd +wdoyqfpy +gtolniag +nvznsjsi +cabyykqf +bxfoznta +mobkmnwc +sauywroh +xexkiuyy +oyevexto +vqegjclw +xoifoyic +rshyscwd +hvreaslo +negmieaw +dmfggrux +yxmknfpx +ttyuarnj +tiwewqnv +fgmplqux +fsdeavfu +uvqczvae +dydssjnh +ihlykqnj +xygdlwae +usyabtdd +yqikfwvv +nvqrjqzh +rexbjqsu +nzerccmq +apzjtxxq +dgnykfrj +izahycnb +ruubddeh +xkrbsmak +eooaoaxz +alwnxxey +rpxrikkc +syafkuqf +fqwavgfy +jlrielnd +ylmuftje +jigqlhyu +fdbusljj +ltsvqjss +ryzsnxja +sdvtoolp +ttvfgkan +zzbzmsdh +oneyivvm +lmkooris +qbwaumyc +upooekoi +oaplwhwf +zvvsffan +hjujkmzt +ksocowvu +xmaxctph +uohgaowz +wyiqttrj +apdmkleo +ngirmkie +zdmekxlr +zghwmffq +kkwtxmht +aopxxxla +cwaxcndp +mchejzuf +qiisajma +paqlvgwk +neepulbv +locpxcge +zqihibab +mpwwkcnc +acltdkmx +tsveirfy +oyqnektx +roljqjyy +tppfxdlm +pdkdgzbj +hqohdklw +ffaqesqq +cluhwwfj +bfwsfqjl +yxeforet +auhwgphh +wqeumebp +stagigac +typntncz +hdlxeizg +daaredsw +tbgzwdkl +jnncvszy +vzzojziz +uhuiphch +sflgjnmx +zjcebfsa +psvusroj +isyddacs +qjnifltk +lxsjtnwd +gfrahlus +vvzirpuo +zxscyfrr +asdpeiwa +hzrjpvlj +lubqxhin +kauqzmng +vlchbkuk +baivdnom +cuwbwhml +inwmiyfq +ywvdnsqq +sohppxig +vnvqqmjy +aaekdkci +byyylakp +vwctbbrq +pguveyxu +ccbkkuqw +qmtotaum +bbghdbpn +miuprwir +lkjlnmus +zhvpqpwz +xtxzqnix +lxckscjy +lkvlzrta +feoujqdq +myecxjgd +dwkmzbor +gfdlcijs +abwlgpdc +vwspenjl +cigbugvr +ugjalecy +klbkpbsg +qraxrapw +vyjcmvmy +ffhvruro +eluiytkn diff --git a/d06/src/main.rs b/d06/src/main.rs new file mode 100644 index 0000000..e482735 --- /dev/null +++ b/d06/src/main.rs @@ -0,0 +1,63 @@ +use std::collections::HashMap; +use std::fs; + +fn solve_puzzle(lines: &Vec<&str>, use_max: bool) -> String { + let mut freqs = Vec::new(); + for _i in 0..lines[0].len() { + let hm: HashMap = HashMap::new(); + freqs.push(hm); + } + + // Count char occurrences at each index in line + for line in lines { + for (i, c) in line.chars().enumerate() { + *freqs[i].entry(c).or_insert(0) += 1; + } + } + + let mut result = String::new(); + for hm in freqs { + let c = if use_max { + // Part 1: take most common character + hm.into_iter().max_by_key(|x| x.1).unwrap().0 + } else { + // Part 2: take least common character + hm.into_iter().min_by_key(|x| x.1).unwrap().0 + }; + result.push(c); + } + + result +} + +fn main() { + // Read messages from input text file + let input = fs::read_to_string("input.txt").unwrap(); + let lines = input.lines().collect(); + + // Part 1 gives "mlncjgdg" for me + println!("Part 1 solution: {}", solve_puzzle(&lines, true)); + + // Part 2 gives "bipjaytb" for me + println!("Part 2 solution: {}", solve_puzzle(&lines, false)); +} + +#[cfg(test)] +mod tests { + use super::*; + + const EXAMPLE1_LINES: [&str; 16] = [ + "eedadn", "drvtee", "eandsr", "raavrd", "atevrs", "tsrnev", "sdttsa", "rasrtv", + "nssdts", "ntnada", "svetve", "tesnvt", "vntsnd", "vrdear", "dvrsen", "enarar", + ]; + + #[test] + fn part1_example1() { + assert_eq!(solve_puzzle(&Vec::from(EXAMPLE1_LINES), true), "easter"); + } + + #[test] + fn part2_example1() { + assert_eq!(solve_puzzle(&Vec::from(EXAMPLE1_LINES), false), "advent"); + } +} -- cgit v1.2.3