Compare commits

...

10 commits

Author SHA1 Message Date
07a1391030 2021 day8/rust: make v4 function unsafe 2021-12-09 21:28:01 +01:00
591cde8631 2021 day8/rust: add test 2021-12-09 21:28:01 +01:00
11907cf8b5 2021 day8/rust: prettify calculation of output number
Now that `digits` is a fixed-size array, both methods get unrolled the
same and there is no performance difference.
2021-12-09 21:28:01 +01:00
edd8bdfa71 2021 day8/rust: only trim start of output digits 2021-12-09 21:28:01 +01:00
92aba2db95 2021 day8/rust: use ArrayVec
This avoids an allocation and, more importantly, turns `digits` into a
fixed size array, allowing the final calculations to be unrolled
completely.
2021-12-09 21:28:01 +01:00
a231883ea7 2021 day9/python: add cleaner solution 2021-12-09 21:28:01 +01:00
8775b26fe6 2021 day9/python: add dirty solution 2021-12-09 21:28:01 +01:00
b259572265 2021 day9: add data 2021-12-09 21:28:01 +01:00
891e9861ef 2021 day8/rust: add benchmarks 2021-12-09 21:28:01 +01:00
aa464884fe 2021 day8/rust: add solutions 2021-12-09 21:27:22 +01:00
15 changed files with 100818 additions and 0 deletions

2
2021/data/day9.expected Normal file
View file

@ -0,0 +1,2 @@
468
1280496

100
2021/data/day9.input Normal file
View file

@ -0,0 +1,100 @@
9821016789345689876545245989999932987654349769898765104567898765634567899765432123788999891045698701
7632145894234599987432134567898799999979498956789854323458999854323456789898321094567988789234987612
6543234789345987654321015778987678987898987545678987634569899843212349899987432989679875678949896543
7654345678956798765692126789854567896587898621567898545698789765103478999876549878998754599998789654
8765496789987899976989238898763459965456999533478999768789678973212567899989698767898765989987678967
9876787898798989899879345997652378954345987644567899878893467895323878989998987658999879878876567998
6987898987689876789768959876543789993234599785698999989912457895456989878987898767899998767765456899
5798999996579765878957892987984578989356679876789998692101567896569999765496789878989789856312345788
4569899987498654567898901499876789678967889987896987543213456998778998954345678989775698543201234567
3659789986329943458999212345987894569898995498945998764394967899989987893236789998664987656312345678
2545678965439892347895425457998913456789654329434898765989898901298546789959899899543498765453456789
1234568896798789456789434569879324599998993210125679899876789312987634567892998798992379879567867899
0123456797987689967897545698765434987876789321234567989865695459896523498921987657989467988789878978
1294968999996579898998656789876549976745898934545679876974489598765434789439876545878989499999989767
2989899998765458789998767897997698865434567896898789995432378999876555678998765434567894345678997656
9878789987654323678999898956798987654323456987899893496321267899989666789019896325679921234899876545
7654678998765412567899999543459876543212369898934912985432456789999879894323998437789210345799985632
6543568999654323456999897632498765432101458799329899876543569899985997995439896558994351456989994321
9854567898798654567896789321239878543212345679498798997854679999874356789998797667895942349878989990
8767698999899766878925459210123987654323457789989657998965989398765234567895689978999893598769878789
9878989896999987989412368991234598765454769898878945899876793219876347698924567899998789699754965679
7989876765788998994323456789947699876767899987653434678987894101987456999213345978987678987643434678
6799995454567899987434569896899789989878978998762123789098999919598567895101234569876549876542123689
5678989323456799876565678935678993497989767895421012392129998798439878963214395678987634984321034599
4895678910167976987676789124569012976593656789532323679245987656522989954323689989876545695432234569
3434799321279875498787891013689929876432545678993434568956798743210197895434567899987656987954345778
2223789534569989329898999124567898954321434567989546979767898654321356789545679989998967899876456989
1012578945678998910949678935678987895910125789879999899898998765432459997676989878999878956987867891
2123459656989987921234568945989876789891234598767889789999649899543567898787894567898989347898978932
3436578987899976899499689656798765676799345987655678678998767988999878939898923456957893236799989893
4547678998999894988988998767998654545678957898743244569759889876789989923969212367898994345689998789
5789789349987673976567899879876543234569768997542123458945998765678998799654323459999987658789989656
6998993299876542987678943989995432123456979876543764567896987654589987678976434598989998767898978942
9887894986987431099789992199986543434567895998654876678929876543498943569996565987878999878987867891
8656789875698549129897889249897696545678934569765987989210987654567892456789699876569689989876545990
7645878954987678998965678956798987858789124579876898999391998765678921239999987943489597898787434689
5434567893298899987654667897899298767891035899989929998989899877889210198789876542123456789632123599
2123678942109959876543456989999019888989186789891012987678789988994323987678998753244897896521012678
1034589543212345987632345678978999999679997898752129876567679999995439876567899894556789965432123789
2137897654323459876321234789567989656567898999643498765434567999987656985498956965678890986563234599
3246789765434598765430125897679878643456789998756569876323456789598767976989999876899921987784346789
4557999876545679898641236998789966532345899899867891987212345895459899899876989987999932398765659897
5768954987786789989752347939899854321236998789878990195323467932378998789765678998989893459876789956
6879543098899899879765458949998767432347897678989989996934589549999987678954367899876789598987892345
7889632129978999769878569998769876543456976456799879889895697698789996589895256989865679987798931234
8996543234567898754989678999654987656567965345898767679789899789678985456789345679654598765679892349
9987656745678987643298789998743298777678954234987654545699967998799876789895458989765987654566789598
9898787898789999732109898999856899988989432129876543236789656899893997897987667899879996532345699987
8789898989898998743412967899987967799996573236987664127894346789965698976498788921989987621234789876
7678949678987987658583456999998956667899794345698543234993237899876989899329899990199998710123898765
7543234567896798867678967898999345556798989456987656786789379912999876788934998789239879823939999654
7654949698965679878789989987893212445987678998998767897895467902987665677945987698998765439898998743
8969898989754567989896899876789901239879567899999878998996598929876554566899876567899876598767897632
9997767879543456798945698765899892398768456789898989879987679939875432345789985458912998987656789541
9986556568912345987656987654356789987654345698767694767998789898954321234679876345793989876545997679
8765432457893959898979876543245678998743256789654543459899899767899434348789965457989878976434569898
7654321348999898769899985432123899987654569896543232498789987856798995679892396769876767994323498987
9874210156789787656789876543012790198767698989692101987679876543456789989921987898765456789434987876
1984321347896576545699987654125691239988987678989919976567985532345999897899598999876345699949876545
2995432348954321034989998876234789545699876563479898765459874321248789766998439998765236789899975434
9876543456796542129878969984345897676798987432365679876398765210123678955987645989854101896789964323
6987678567897656298767456798767898987987654321234795988987654321234569543499659876543212345899873210
5498987678998967987658345679878999898999865672347894399598765432345678932398767997654345456998754321
6399798789899879876543238899989998789999876989456789212459976576556789543459898998785456767898765452
7987659896789989998630127678999989679899987896567893101969897677679897659569989129886578898969876599
9876545965678999987621234569999876567789998999879964239898798988989998998998678998997889949658987987
9987631274798999876542355679987654345678939989998999398787689899196569896987589897698995432347899876
9999820123457899876543498789898543234599649878987678987654578789023498785876456797549876545756998765
8987634799598910987656569898765455125989998769876587898743434679234987654212356789432987659867987654
7898545678919999998767898999874321039878898654322456987652123678949799765101347894321098792998996543
6999657889209878999898987598765432398767789863210345698543234589998652976432456985432129891239987432
5878968994398767892999763459876543987654598754321239789654345678999430987543479876549298910129876521
4659878965943658991987642398989656798543459865434678998766556789987521987674569989998987621234987632
3234989879892345789986543987898767929432123998765989659877677894987432398989678999887996545345698783
2123699998789496892397959876789878912949234899876796546989788943596543459998789898656989656957989894
1014568987678989989998999765678989109898946798987895432399899912987896578959895697645678969898979965
2123456798545678979899987854569993298767958897699976541034987893498997989543934985434567899789569876
3654567899434989656789876543458954987658969989549865432123456794579989895432129876323458987693456987
4765878998745699967895998652367969876549878978934976543234569896989877789541034965434569876532345698
5878999987658999878944349763456893989689989569019987894345698999998765678952199876545679986321236789
7989398798867989989431239854567892199789893459198898765467987898987654567893988998756798765445789899
9895497659978978998532345967678943239897654598976789876879886767897653678999877899767899876556895978
8789989431299567897644459878789765456998765987865678987998765458975542768998756789898954987667954567
7655978990123456789765567989899876769999979896754569899019876769654321658789546678969993298778943458
6544567789236767899899698998910987878989998765443475698998989989643210145678934589654989109899432346
5432345678945988912998999987891298989878987654321254567897694498754321234789123498799878912954321234
4321234989856789923987899876789349998969899863210123458976543239865632355678934599988767899765432345
5430129899767899899876798765478956997658789654431734567899854129876548766789545989877656778998545458
4321239788978998798995459854357899876545678966545675678998765934987656878999769878965437567987657867
5432998657899987656789345965456798765434567899656789789219999896798787989659898767894323456898768978
9659876545699986547893239879767989854323456998767890998909878789899898997545999656789214356789899989
8769985434589997656792123989979876543412397899878921457898767679989949876434398769898901234589998991
9898767124567998767891012995989989872101289998989762345987654568975439877321239898987892345678987890
9949656012369899889952129894398799965432378987899843569898743678954321965452345987676789657989996789
9239743223456789996543298765219679878554567896987654698769832367895932987565459976585778967898965678
8949854337567894987964987674101567987669878965498765987654321456789793498976798765434567898987874567
7698765445878943299899876543212346798778989874349876799776532368995689569987998654323456999876553656
8549977656989654998754987894334587899899498763256997899897543479434568978999876543213797898765432347
7632988767899879879543498985455679945954349854367898965999654567925678989456988654435678999897321055
6543499878921998765432459876576789656891298768478999654298776778936899994345699766576789998765432123

View file

@ -0,0 +1,16 @@
[package]
name = "day8_rs"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
arrayvec = "0.7.2"
[dev-dependencies]
criterion = "0.3.5"
[[bench]]
name = "unscramble"
harness = false

View file

@ -0,0 +1,47 @@
use std::{fs, path::PathBuf};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use day8_rs::{v1, v2, v3, v4};
pub fn criterion_benchmark(c: &mut Criterion) {
let mut group = c.benchmark_group("unscramble");
let mut data_file = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
data_file.push("inputs/input.txt");
let input = fs::read_to_string(data_file).unwrap();
let lines: Vec<&str> = input.lines().collect();
group.bench_function("v1", |b| {
b.iter(|| {
for line in &lines {
let _ = v1::unscramble(black_box(line));
}
})
});
group.bench_function("v2", |b| {
b.iter(|| {
for line in &lines {
let _ = v2::unscramble(black_box(line));
}
})
});
group.bench_function("v3", |b| {
b.iter(|| {
for line in &lines {
let _ = v3::unscramble(black_box(line));
}
})
});
group.bench_function("v4", |b| {
b.iter(|| {
for line in &lines {
let _ = unsafe { v4::unscramble(black_box(line)) };
}
})
});
group.finish();
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

View file

@ -0,0 +1,200 @@
bgafcde gfcd agc ebdgac adfceb bafeg efgca cgdfae cg ecadf | fabgced gc agc cdfg
gbdacfe gcabfd cdb dcfba bfacg cgad fadeb feabcg cd gcbfed | bdagcef dcb cdag gbfca
dgcbafe dbfca fbaed be cedb gefad dcfeab facdgb eba gbface | eb gadfcbe cfbad gfbeca
ebc cb aedbf agcef badecg gaebfc bcgf adbcfge ceabf daecgf | cb bce efdab ecbaf
fedbc cebad gfcbd fec gcdfab ecbfga dacgbfe gfed fe gefbdc | bfecag cef egdf fgde
bafedc baefg dbfga daegcb gae egbcfa eg cefab fgce decbafg | abdgf cgfe cedgba befga
dcba fagbed cgbfed dgfbeac da dag acbgde fcaeg becgd acgde | agd bacd dga gbecad
ec aebgcfd fecd bagfec efagd edfgab cgdfea dgcab ecdga eac | ce ec ce gbacefd
aegdbfc fe dacbf aefbgd aecgb cdfe ebadcf ecbfa cbfdga aef | dfce fe fcde afebdgc
bgaf gcbad cagdfb gb dfbeca degfcb bfadc cfgeadb bdg agcde | fdegacb acbefgd bdg gafb
gdecba dcae ec dbfeag bgead dgbce gec cgefab dcbgefa cdbfg | afdbgce agbfde abcgefd ec
gacbefd fgbedc egafdc adf fcbae afedb ad bfeadg adbg dfgeb | ad degcfa ad dfbge
fadebc baef eadfcg ebgdc eda cfgabd afbdc ea edcab cdgbefa | aefb ead ea ebaf
badgecf caefd aebfdc fecbdg fadcg fed de ebad abgecf bceaf | ed gcfebd aebd gfdca
bcdae aefcbg fgdea bf cdabegf adcebf fcbd fbead afb begadc | gbcafe faegbc fba eabdc
bfcdeg ecd eabgc adeg ed dbaec bcfad ecabfg dbgcfea dbacge | de bdfcaeg ecgfbd bcdea
deacf bf eagfdb gfecadb facbeg bfa cbdage abfce fbcg gecab | aebcf ecfagb abegfd bgeac
eag dgbecf gdfae caed dfebgca ea ebcgfa dfbag gecfda dgcfe | ae ea deac fcdega
edbga egdabc cgfdbe gabfe efcdgab gbdefa fb abdf fbg gecfa | cgaebd cbgdea acgfe begdca
cgfedb egbdc agcfd cdagb eabgdf ab abce adb agcebfd dabegc | ba bda abd deagfb
fc agfbce fdebg dgafeb ebfdc cbf cfedgba fdcg gcefdb eacdb | gbfaec fgcbed gbceafd bedagf
bdcgefa acdfbe ed gbcea cfgbad ecbda edfcag afcbd ead bdef | dfbe de dea dea
gfbeca gcefdb fegacd cgbda fbde ed bgedc begcf agdcefb ced | gbacd ced dec bcegfd
dgbacf gfabec agd egdaf badcegf ad ecgaf defgb cade dfegca | cdae dga bgcaef ad
dcf cfadb bcagd afebd fbgdec egfcbda fcea gfeabd fc ebdfac | gabdc ebfad aefc eacbdf
cfgdba fcdab cgfbade adeb afdebc egcfdb de aedfc cfage edf | ed eadb dgebcf cefdabg
bcae befga dgebfc fbagce cbefg agb fcedgab ba bdgacf dgfae | beca dcaegbf gab geafb
dgafbec fedba cbega aebfdc eabfdg bdgfce edc fdac cd adceb | abedf dfeabg dc gdbefc
adegfbc gbedcf cfabd gacedb abcfe fdbacg dgabc df dfga bfd | fdag dbf dafg fd
dfbcag ebfcad ed agecdb bfacd egbfa badfe afbcegd aed cdef | afebd ecdf cfed eadfbgc
fca dbcaf ebdcf fadbg cdbefa adec ac dacbgfe fbcdge cgefba | ac edca edac ca
agdfce cedbfag agc dcbaeg gc bcdga bgdfa cfaebd ebdac cbge | ceadb becg ebadc gc
aefbc aecdg efgcad cdebag bcg agfdbc bceag bdge bg fecdgab | bgecda bcdaeg gb caebf
cfgebd acedf ag gbad gcfbad acg bgecfa bdfcg gbadecf cdfga | dgba dgcbf gac bgfdc
cdgbafe cdgbe gd agde bcaged dfebc geacb cdg bgeacf gbdcfa | efgcba edag gecfdba cgd
bcg bcaf cegdaf dfacg dgacb fgcebda bgdcaf efbcgd bc gadbe | gcb bcg caebdfg cfab
eg cdgfae aecfd fgbaedc gbdfc facdeb cfegd ceag efg efdgab | fge acefbgd egf efg
fcdg gefda cf edabc cedgfa cfa acefd cfebadg gdbafe bgecfa | fc fca cfabged fca
ed bcadegf egdfb fgcdb edbafg facbeg abegf deba fadgec dge | eadb ged eadb adfegb
bgedc cf cefadg ecbgdf gbfae cbdf fcg ecbgad abfgced becfg | bdcf dbceg fgcbde cdbeg
cg gcaf defcag cdefg gdafe dgfabe gec dabegfc bcegad ebdcf | feadg degcf gcaf dcgefa
fdeac gdcebf daecbg bgfec fdeagbc gca gecfa aefcbg fbag ag | ga decfagb ag agfb
bc fbdc bagecdf gfdcab acdgb dcaeg dbgfa cgbaef ebgfda gbc | bdfc cb cfbd fbcd
efagc bafegd abecg eab eb fgbeca gbedcfa bfec fcgade cgbad | abfgdce degacf fcaebg cdabfeg
egbfcd afde ecgab fac fa edabcf cbdagfe afbec cfabgd fdebc | deaf feda cgbdfa fa
gbafd defca ebcf eb cefbad dcebag dcgfae edfab deb bgedcfa | be deb gdbafce be
afdgceb cf gbdeaf acdbfe abcef acf deafb fedgac cdbf cgeba | fac cfdb bdfcae fdcb
cfdba ca afbgd eabc cda afbcdeg cfedb ecfgbd efcgad bcfeda | bfgad abec abce caeb
cfebad debfacg acgfeb cab fedcb afcdb cdae febcdg ac dgbaf | bac ca faecbgd cgbfae
eg dfcegb dge abedcgf facdg bafdgc degacf dafeg agec adebf | egd fegda fedbcag eagdf
age gcbad feacb cgbdfae ge gfbe agebc fcadeg cbafde cgebaf | fagecdb bfeg baecg fceabd
bfcdge fgcbead ebdgac ba dagbfe gab dabc bcegd gface cebga | cbda gbedc egcbafd gab
dbcegfa fadceg fcage bcage gb bdagfc abdec gbfe bgafce bcg | ebfdagc ebfg ebgf egfb
gcdefa bagf dgebf bdefc bgcafde dbefga edabg gfe gf dcgeba | fg efg agfb gebfd
fabdc gdeabf dag gbace adcgb gd eagfcb gaedbc egdfabc egdc | adgbc bcadf gd cedfabg
deagcf ce dfcaeb gfadceb bgade cde daegc cgdafb gadcf efcg | dbaecf fdecbga ce ecd
dfcbag abceg db dgcefa dabec bcegdaf cdb bdef defac cfbdea | gdafec dcb febd bdef
ead fgceabd edgcaf da fgda fgcea ecdgb cgaed fdbace cbagef | ad eafcdb bfecagd ad
ab caefg agbcef efacb gecdfba ecfagd fbdec bdecga cba fgab | eafgc abfg cdagbfe dcageb
afbcge ebafcgd dacegb cfbea afbg agc egcfa ag cgdfe bdceaf | adfbceg fagb gfba aecgbf
acg cdegbf ecfadg cgdab fcbadg cdbgfae ca agedb cbgdf facb | cdeabfg ca afbc dagfec
dca dbfae bagdcf aceg ac afdegc cegfd fcedgb cbdaegf cedfa | dac acge cedgf cfdbgae
gdcafe ega cgbfde cgba afdbe bacegd ga begdc dcebagf edgab | abgc ga bacg bdacge
begfda gdafce fg gdbf gbaced agf decagbf aebdg gabfe cebfa | fag acdbeg agcdfe dgbefca
gfcdbea fecab cbadge afgce gc fgdace fdcg cga eagdf eagbfd | efgacbd gc dgfc dacbeg
gdbefc feadg dbagef afd abde gebfd cefga bdgafc da efdacbg | cbegfda adf bade cdgfbe
aefbdgc eabcfg cgefa degbf cedfg cd cadg cde fgecad fecadb | dbfegca egcdabf gcda dc
baed bcgda abdegc decag ega dgbfaec gacbfe ae cgfde dbacgf | aebd age ea bgdecfa
abecg faegcd egfcd efcga cdaf baedgf efa fcbaedg fcgedb fa | afe af aef fa
abgfce cbedgaf fbedc eacdfb dcf fd faegcd fecba dfba gdceb | faecdg gdceb dfc gcbde
fgce fgadb egdabc edg defbg eg bfced fdbace bdfgeac gbcfde | gdcbfea deg afbedc dgcaeb
eg dgcfe efdgab bfdec aecg acbegfd bdcafg adcfg efg daefgc | fge aecg gfe ecagfdb
acdfe bedcfg abgec cfeabd aedbfcg fecdag fg fcg afecg gdaf | cdaefg fdag cgf gadcfbe
aedc gbdac cbagfe bfdcg gdbfea acb cbgaed ac eadgb gecadfb | gbfdc gbdaec fbgeadc decabg
gbc afgdb cfbdga badgef gadbce egabcdf geacf bcdf cb gabfc | fgaec fcagdeb gbcfa bc
cfgabd cfdbe eafgdc badcfeg dgfab cbag eafbdg gc dfgbc cdg | fagecd cgd afdbge cg
eadcbf fcag gc gbc gadcefb gebad febdgc egcbaf ecbag bacef | dgbfec egadcbf gcfa bcegfda
acbeg acgedb eg dgbfce cdagb cgdbaf gaed bafce cfebadg ecg | dbgac eabcf eabfdgc eagd
bgefda eacg abdcfe eabcd abegcd ebg cbedg cbfdg dgfecab eg | ecag eg ge afebcd
dgecbf bg dfcba abge gadebc edagc bcg ecdfga fbgecad bgcad | cgb gbea ebgcdf gbae
dcabg gdfbca debc aed ed fabeg afdecg gbeda caegbd agbfcde | fcbdgea cedb dea efcdagb
edbcafg cgaed gc gbdc acfebd cge gceabd abecd afegd bgafce | gc facegbd edbcaf cg
gadbce gbdaf acebgf bd abcgfd edfga cdfb gbd cfgab dgafbce | gfdab fdegbac eabcfdg bgcdae
eacgf fb cegbdf dgbfca cfb dfba agfcb cgabd ebcdgfa gedacb | acbdg dbaf ceafg bf
gebac egdacf fdceg daef af gfa dagfbc fcgbead afgce cebfgd | afg bcage gcdeabf cgfea
cdabfg fgbec gcdafe ceafgb egc ec beac febdg bdegcfa fabcg | cafegdb beca ecgbfa bace
gda feabdg ebgaf bgdfc bgfcae bfeagdc edab ad adgbf fceagd | gad ad afgbde dfaebg
bfc bgfced edbfag fcea cf afgbdce cgbda abgef fcgba aebcfg | bcf efac eacf fc
bdeag bgdcf ec ebgdfac gcbde gaec dec cafdeb afdebg cebdag | gcbfd agedb acfdbe ecd
befad gd edgbf bcgafd cebgfd abgcef gfd ecgd cgdfaeb ecfgb | cegd cdeg efadbgc egcdbaf
gfdbae bc cegbfd gadcf cdaefgb gfbed fbcgd beagcd gbc becf | gedcfba edcagb fbcgdae cfbe
ecf cbdaf bdfcaeg fagdec efba bcdfe bfecda gcedb ef cbadgf | ef fbedc dbceg edgfcab
cabde ae ecfdba cbdfe faed dabcg cfbgaed gbfcea begdcf abe | adfe aeb cabgd ae
gefcabd ecabd gebafc ag ecfgbd gacdbf dfcbg fdga cagbd acg | gebcadf gefbac dgaf dagf
bgde efcgd cebgfa dbacgf cgfeb gdc fdaec dfbceg gd fegcadb | efbcadg cgd gbdcfa facebg
gefcbd bgfdcea fg dcbag fbgcd fgeb defcag fcg becfad dfcbe | cefbda cfg fegbdc fg
bcfed bcfdge aedbfg fad decbfa facge cdba efdca ad dbfagce | eabfgd fadce begfdca gecdbaf
edfgbca dbega fdb gfecab fd ebcfda cfda feabd becfa edgbfc | df ebgad cabegfd adcf
daegbc fbacg edfg befdac efbcd edafbgc cdg bcgfd fdegcb gd | dg becdf dbeafcg gadefcb
egdacf gde ed dfebcag acgdfb cfadg agebdf cgeab gecda dfce | fedagc gabfecd eadgfbc cfgebda
dfcea afbde fdgac gefbac eabcdf ace bced ec ebagfd adgbcfe | ce fcdga baecfg cea
adebg gdfcae gb acegd abg begc bdaef bfgcda dgaefbc abgdec | bg cbafgd ecbg gba
bfacde egfadcb cb fcdea eacbf cedb dcgeaf bgadfc abc ebgfa | adfebc dceb bdcafe efdac
faegbd dgfce dcgafe fgadecb bcgd bcfge bfaec gb cdbfeg fgb | aecfb fbg fcgade dafgbec
fgacbe fc ebcadg agfdb fdbca cagbdfe cdfe abcde fcb bfacde | cabed cfde bfdac dgbaf
defabc gfcbe fea bdacfg adgecf fabce ae aebd bfecdga cdafb | fea ea bgfaced bead
dfgae dcafe dce dgebfca abfcd ecab bfcdea gcfebd gafcbd ec | bcdagf cde gbedcfa bfdace
cea gaedfb acdf ac ebacgd gefdabc befda cfbae egcfb efcabd | gcaebfd cbaef dfac ca
gebac befadgc bedcgf cgb gc eagbdc cebaf cadg bgfaed aegdb | cbg cbg cbgadef acdg
gfcdeb bfdegca beac befda bcf dcfga fbcda cb adecfb gbefda | fbc dbafc cbf cfb
cbdfeag becagf acgeb be cgbda gfcdbe efba acfeg ecb eadgcf | aebf eb bagcfe eb
febg aecbdg fedacb ecg ge fbgaedc dfgce afcgd fcbed efcdgb | aedcbg baegcfd ecg bedcf
bcfedg gdbcae febd fcbag fgbcd bd ecdgfa dbc fdcagbe gdcfe | efgbadc fcbedg abfecgd cgbedf
feagc cb dcgeabf fadbgc cgfdae fbdea feagcb bcafe cbeg bac | bgce bc cgadbf cba
efagbd dageb dcgbeaf gc acg gdecab gecd fdbca gbcfea acbgd | eagbd egfabcd gfbace gca
ae cgae dea eacgfd fadgc gfebd afdcbe dfeag acbfgd dacbgef | dgbafc cbdfgae ae ead
egb gcfe abcfdeg begfcd abcdfg dabec eg bdfcg edgcb bedagf | bdecg beg gbdcef dbeac
fcgae dfcebag aefdb geacbf cda dagbcf cd adcfe fedacg gdce | bcdafg adefb fdeba gecbfa
afegd gadfb fcgadeb gbcade gdabef cefgad dfbe bdg fagbc bd | dabegc bfgca bd dgb
dfceba faecd bd afbedgc ebad fcadb bafgc dcb efgdcb gfaedc | faedc bead egcdbaf bd
cgb bdcfeg cg gdbeca cgef gdbcfea febdg fdcgb dafgeb bacfd | cgfe cgb gc gfec
cadgfb cefdbag cg begacf dfgbea bgc adgbf bgcad fdgc eabcd | cg fgceab ebdacgf ecbdgaf
bgdefc decfg cfedga cefgb bfdcea edgfacb bgcd fegba ceb cb | cfgbe dcaefg cbgfe dbcg
gefbda acde gcbaf cd cfd bdcgfae daefg gdcbfe cgefad agdcf | gcebafd efgcda cfd aecd
gedfc cead egbfda fgcdabe de fcgea gfdbc deg cagdef gebacf | deg bfgead gdfbc gde
fbgae bfecg dbcaef gdcaeb cbfdge gbc decgbfa edbfc fdgc gc | gc gbcdef fdcg gfcbe
fc caf cadbe dgabfc gebdfa fecg fagbe cebaf acbegdf cgabfe | egcf ceagbf gcfe ebgafc
abfcd cgbfa bcgafd gadefc abfde cdf dc agfbced cbegfa gcbd | cd cfd fgeacd cdf
bdgae gfbce cbagfd gedbcaf ebcgd gbcefa dc fcde cegdfb dbc | dcegb fgdcbea deabcfg bdc
ebcagf df bgcdefa fdceba cgdf dbacgf eadbg afd abgcf gabfd | ceagdbf dagcbf fd dcfbaeg
dga cadbgef efbcgd bgdca eacd cedbga gdbec gdeabf fbacg da | dag adg edac edgcb
febcg gbadce aedbcf acbed cbgae cag gadb degbcfa ga fcgdae | bgad abdgfec acbed acg
gdcaf dcbfag gdefa abcdg bcfg cf fedgacb dbaegc fcedba afc | cf afc gcfb eafdgbc
gdcaef gcabfd bcefgda cde gcdfb gbecfd ec dfaeb cefbd egbc | egbfdc ce gbdcfa afbcgd
fdea fga fa adcgbef abfcge gbeda fagdb abedgc cgbdf bdefag | fa gaecfb beadgc cbfgd
ba gcbafd decgba bfga fbadc bda dfbeacg ecfgbd cedfa fdbgc | gfab gbdfc adb ba
bd ecdgab aedfg bde fbcae decfba cgafbe fdeab dbfc fdgceba | db dbe cdfb bdgfaec
bd gcefbd ebcd dafbgc ebfgd decgaf edcfg fbd fbaeg gafcdbe | cbeagdf cebd cedb cedb
edagf cdaefbg ead efgdb bgafed cfdag bfea dabgce dfbceg ae | gcafd feab dgfca feba
gecfda bgcdf ba abd edgabf bcdeaf gedfa fdbga baeg abegcdf | ab bad bda fgebcda
egadb bec dbefga cbafg cageb ce ebdgca dceg gdebfca cfbead | ec ebc egdc ecb
cbgfe fadcbg agde cbdge egdacb degcabf bed faebdc ed dcabg | eadg facdbe bcdeg debfac
fabe fb bgf edcgaf bgacd bcfegd fcdegab adgef afbdg gbfade | bfg dbcgfae edacbgf bf
bcdfe becgd cbafegd decag geb dgfb bfcade dgcefb bg gfbaec | bdgce gb dcbge gdfb
egcadf bfg fgbdac faedg ebaf gcedb begdf fb adfebg cafdgeb | faedbcg bafcgde bf becgd
edafgb ecbgd gcdfaeb ef gbfadc cgfade gef fdegc fgcda efca | agfecdb dcabgef ef dgcaf
cbdgea fegcad bagdc abdfgc age gebac afbcdge egbfc adeb ea | ae ega ecagbd gbacd
ecdfb edafbc fe afdcb efc degcb aedf acbgfd ecgfab fecabdg | fe dbegcaf fe cedgfba
edbfgc caeb ca cbdafg bcdge bgcdae edfga eacgd gca cgabfed | facebdg ca aedbgc ac
gfeba aegc cbfega ea bea dfgbe edfacgb fadgbc gbafc aefcdb | gbecfda bcdfag gcea afcegdb
fgcea dc egfdba cdafe gfdecb fbaedc ebdfa dacb bdafgec dec | ced bcfdaeg cefda fagcbed
dbefa defacg agd dcbfg bdfagce ga efgbdc abgc fdgab bacfdg | cbga dga gedcfab fdabcg
eacbf eac dfagbce cfga fbgea ca bfced agebdc fbedga fcbega | dabfgce gfca ebfacg gbaecf
cfagb cgfdea gdc agfcbe fdeacbg bcfdg dg badg fdgacb bcdef | cgd gfacb gd gdba
cfga gbadef decabfg cbged baefcd ca cda dbagf fdbcag dbgca | afgebdc bdgeaf gafc cfag
cgfbd cdafgb gbcead dbg afdb bgecf adfcg ecbgdfa bd gdacfe | fgacd cgdfa bdaf dbg
abf fb febd dbafg febcga gbead egdacb fedgba fbagdce gdcaf | bf fb dbceafg fba
fd dgfabe ecafdg deabcg cbefa fed dbgea bdefa bgdf dbfgeca | fde bdfg fdgb gfeabcd
fgeadb abfed dgfbcea bafgd agcfd gfb bdfaec bg dgbe cegbaf | egdb fegadbc dcgaf bg
fdabc acfbgde dg fcage cdfgab dfebag dga gdafc cdgb eafbcd | gfcad aegdcbf dag afbdc
dbfe efdag fgdcae cgbfaed bgfead acebg fb bgf egabf abfcdg | ebdf gfb ebfd fgabed
bafegd egcfadb fbc bc fcebgd becaf afbed agecf beacfd bdca | cb dbca cfb afgbcde
gbefdc bfead gfcdab gbedca fc cabegdf bedfc fdc cefg cgbde | fdbceg cgfe fc bdcaefg
fg dbfea fedagbc cageb aecfdg dfgb dbfaec gafbed begaf gaf | gfecad dgcfea dafeb gfbd
dabfg eb egacdfb bfcdga faedc gdeb fbagec aeb bgefad dfeba | bafdcg gfbda gebd be
adb dgeabc bfdg gaecfd afdgc cbdgfa bd dcgeafb dafbc bafce | bd fcagbde defagcb dbfg
fge bagcf dfaebg fgebc eg cdeg fabcedg bedcf dfcegb acfbde | egcd ecgd cebafd cgdaefb
bafdgc gecabf caegf ae beaf gfacb decgf cfedbag aec cdabeg | dcfge ae egcdafb eca
fcgba ebgaf agefd agcdfb beac gdbcfe beg bcdfgae gacfbe eb | cfebgd abecdgf ceab abcfdge
abcgfd bac feabd adcgeb fcgb bc debgcfa fbdac gceafd fagdc | fbcg gcfad afbed cfbda
afdbegc dea abegd agecbd afbgdc dgbfe cabgd afcbed ceag ae | daecbfg cdbag acge ea
ebdcg cbfg cdgebf bge adbec eabgdf baecfdg fgadce gedfc bg | dcbae fbcg beg gfbc
cfbga cefabg ebfca abeg ae bcedfga gbacfd eac bdefc dcafge | abge fedbc bacgf eac
eb aedcg dbfcge cegdbaf gfbe bed dagcbf dacebf dcfgb dbceg | gbef dbe fegb eb
cgfe ced dgeba ce cegad abedcf cdaefg cfagbd cadfg gbcedaf | ecgad gcabefd dfbcgae ce
dbagc afdc dgfaeb bcfage gbedfac cafbdg cdgeb ac cab gdafb | cab dafbcg dacf acdgfeb
dgbae bfa eafgb bafegd fgaec bcfadg fb dbfe egbadc gbecadf | cdegbfa dagfcbe fdeb fab
be aecgdf gbdacef bagfc bdef beagf beg dbefag gedfa cbgade | be eb afegb beg
cfdagb ac gcfdae cbad bfgda bcfedga begcf afc ebafdg cfbga | deagcf abdc ca cagfb
eg efdbga adbcfg ebfcga aefgd bdfga adcbgef defac gbed gae | edbg gedb bfdeag dbcgaf
bacegf gbfca cfa ceab gafeb ca gcfdb gadfec dbafge gbdfaec | caf fbgedac fca afc
fbagde cbeg afecg gdaecbf fedcga gab efacbg gb acbfg cfdba | gba ecbg agb abgcf
abfg dafcgb gcafd bdcgaef ab cba ebdcf bgdaec cfaegd fcbda | acb ecadbg cgdabe fgab
agcbd cegdba feagb fd daf gbfad bacdfg cgdfabe dcefab fgcd | df dagbc fgcd fd
gdfeabc bcd debaf cb gedfab bcefd edfgc bdceaf aecb cdgbfa | ebac beac cbea abce
cbgdef eg beg dbgfc bfgcead gbefd efcg edgbca eabfd acbfdg | gbe gbe cefg gbe
fedg begcad gdb bafdge gd egbcaf gdbaf bfacd eacgdbf eafgb | cbefag baegf gefcdab bedgac
gbce dcafgb agfdceb efdac acg agcfe aefgb befagd cegfab gc | fbedgac abdefg cga cag
gabc bceadf agfcd agd ag fcdgbea gabdef gedfc dbcfa agbfdc | gacb gad bgceadf cbgafde
afedc fabced gfcbed feb cegdbaf dbfa ebfac dacfeg fb gbace | feb afdb ebf acfde
cbed edgcf ecabdfg ceg cfbgd abgfdc gebfdc ec efbagc gedfa | ec decb ecdb gec
egd gfbeac gedcb de dabgfe dcfbg cbefdag eadc dcegba acegb | ed adgfceb gde abcged
efadcgb fbdce fagedc agebfd gbafe abedf da bgad ead ceagbf | efagb ecdbf ade aebcgfd
cgeb bfaed cbedga cbaed bc bca afgcbd dgface bgfecda decga | fgadcb bca dabfe cab
dgaec deagcf egf cdeabg abgdf gdfceab dfcbeg gfead efac ef | agebcd efdbcag aefc bgefcad
fbeac gebcaf efa fgce bgfdea dgbcfae gcbdea adcbf ef egacb | gebfacd bfadcge ef eagbc
acdbgfe adfg edfcbg efacb egacf gca dgaceb egdcf ga gefcda | fdga cfgae cga ga
dfebcg gdaebf dagcf gbaecd dcgebfa bf cfbe bgf bgdce fdbgc | cgbefd bcfe gbf ecbf
dcafeg ca acf fcdeb abcfdge facdgb bfagd dfaegb bagc dfbac | dfabgce dfaegc gbafde bacg
cgefb cbe cfab afdbeg cb faebg gcfde cefbag ebadgc afdbgec | egbdcfa cbe cafb fecagbd

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,13 @@
#![warn(clippy::pedantic)]
#![deny(unsafe_op_in_unsafe_fn)]
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub struct LineResult {
pub unique_digits: usize,
pub number: usize,
}
pub mod v1;
pub mod v2;
pub mod v3;
pub mod v4;

View file

@ -0,0 +1,23 @@
#![warn(clippy::pedantic)]
use day8_rs::{v3::unscramble, LineResult};
use std::io::{stdin, BufRead};
fn main() {
let result = stdin()
.lock()
.lines()
.map(|s| unscramble(&s.unwrap()))
.fold(
LineResult {
unique_digits: 0,
number: 0,
},
|a, b| LineResult {
unique_digits: a.unique_digits + b.unique_digits,
number: a.number + b.number,
},
);
println!("{}", result.unique_digits);
println!("{}", result.number);
}

View file

@ -0,0 +1,51 @@
use std::collections::HashMap;
use crate::LineResult;
#[inline]
fn lookup(n: usize) -> usize {
match n {
17 => 1,
25 => 7,
30 => 4,
34 => 2,
37 => 5,
39 => 3,
41 => 6,
42 => 0,
49 => 8,
45 => 9,
_ => unreachable!(),
}
}
#[inline]
#[must_use]
pub fn unscramble(line: &str) -> LineResult {
let mut parts = line.split('|');
let input = parts.next().unwrap();
let mut counts: HashMap<_, usize> = HashMap::new();
for c in input.chars() {
*counts.entry(c).or_default() += 1;
}
let digits: Vec<_> = parts
.next()
.unwrap()
.trim()
.split(' ')
.map(|s| s.chars().map(|c| counts[&c]).sum())
.map(lookup)
.collect();
LineResult {
unique_digits: digits.iter().filter(|d| [1, 4, 7, 8].contains(d)).count(),
number: digits
.iter()
.map(|&d| char::from_digit(d as u32, 10).unwrap())
.collect::<String>()
.parse()
.unwrap(),
}
}

View file

@ -0,0 +1,47 @@
use crate::LineResult;
use arrayvec::ArrayVec;
const FREQ_TABLE: [usize; 50] = {
let mut tab = [0; 50];
tab[17] = 1;
tab[25] = 7;
tab[30] = 4;
tab[34] = 2;
tab[37] = 5;
tab[39] = 3;
tab[41] = 6;
tab[42] = 0;
tab[45] = 9;
tab[49] = 8;
tab
};
#[must_use]
#[inline]
pub fn unscramble(line: &str) -> LineResult {
let mut parts = line.split('|');
let input = parts.next().unwrap();
let mut counts = [0; 7];
for c in input.bytes() {
if (b'a'..=b'g').contains(&c) {
counts[c as usize - b'a' as usize] += 1;
}
}
let digits = parts
.next()
.unwrap()
.trim_start()
.split(' ')
.map(|s| s.bytes().map(|c| counts[c as usize - b'a' as usize]).sum())
.map(|n: usize| FREQ_TABLE[n])
.collect::<ArrayVec<_, 4>>()
.into_inner()
.unwrap();
LineResult {
unique_digits: digits.iter().filter(|d| [1, 4, 7, 8].contains(d)).count(),
number: digits.iter().fold(0, |acc, d| acc * 10 + d),
}
}

View file

@ -0,0 +1,63 @@
use crate::LineResult;
const FREQ_TABLE: [usize; 256] = {
let mut tab = [0; 256];
tab[17] = 1;
tab[25] = 7;
tab[30] = 4;
tab[34] = 2;
tab[37] = 5;
tab[39] = 3;
tab[41] = 6;
tab[42] = 0;
tab[45] = 9;
tab[49] = 8;
tab
};
#[must_use]
#[inline]
pub fn unscramble(line: &str) -> LineResult {
let mut bytes = line.bytes();
let mut counts = [0; 7];
loop {
match bytes.next().unwrap() {
c @ b'a'..=b'g' => {
counts[c as usize - b'a' as usize] += 1;
}
b'|' => break,
_ => {}
}
}
bytes.next();
let mut freq = 0;
let mut unique_digits = 0;
let mut number = 0;
loop {
let c = bytes.next();
match c {
Some(b' ') | None => {
let digit = FREQ_TABLE[freq & 0xff];
if [1, 4, 7, 8].contains(&digit) {
unique_digits += 1;
}
number = number * 10 + digit;
freq = 0;
if c.is_none() {
break;
}
}
Some(c) => {
freq += counts[c as usize - b'a' as usize];
}
}
}
LineResult {
unique_digits,
number,
}
}

View file

@ -0,0 +1,63 @@
use crate::LineResult;
const FREQ_TABLE: [usize; 256] = {
let mut tab = [0; 256];
tab[17] = 1;
tab[25] = 7;
tab[30] = 4;
tab[34] = 2;
tab[37] = 5;
tab[39] = 3;
tab[41] = 6;
tab[42] = 0;
tab[45] = 9;
tab[49] = 8;
tab
};
#[must_use]
#[inline]
pub unsafe fn unscramble(line: &str) -> LineResult {
let mut bytes = line.bytes();
let mut counts = [0; 7];
loop {
match bytes.next().unwrap() {
c @ b'a'..=b'g' => {
*unsafe { counts.get_unchecked_mut(c as usize - b'a' as usize) } += 1;
}
b'|' => break,
_ => {}
}
}
bytes.next();
let mut freq = 0;
let mut unique_digits = 0;
let mut number = 0;
loop {
let c = bytes.next();
match c {
Some(b' ') | None => {
let digit = FREQ_TABLE[freq & 0xff];
if [1, 4, 7, 8].contains(&digit) {
unique_digits += 1;
}
number = number * 10 + digit;
freq = 0;
if c.is_none() {
break;
}
}
Some(c) => {
freq += unsafe { counts.get_unchecked(c as usize - b'a' as usize) };
}
}
}
LineResult {
unique_digits,
number,
}
}

View file

@ -0,0 +1,81 @@
use std::path::PathBuf;
use day8_rs::{v1, v2, v3, v4, LineResult};
fn test_unscramble_with_input(f: fn(&str) -> LineResult, input: &[&str]) -> LineResult {
let mut result = LineResult {
unique_digits: 0,
number: 0,
};
for line in input {
let LineResult {
unique_digits,
number,
} = f(line);
result.unique_digits += unique_digits;
result.number += number;
}
return result;
}
fn test_unscramble(f: fn(&str) -> LineResult) {
let test_file = |name| {
let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
path.push("inputs/");
path.push(name);
let input = std::fs::read_to_string(path).unwrap();
let input: Vec<_> = input.lines().collect();
test_unscramble_with_input(f, &input)
};
assert_eq!(
test_file("example.txt"),
LineResult {
unique_digits: 26,
number: 61229
}
);
assert_eq!(
test_file("input.txt"),
LineResult {
unique_digits: 543,
number: 994266
}
);
assert_eq!(
test_file("large.txt"),
LineResult {
unique_digits: 159946,
number: 498570828
}
);
}
#[test]
pub fn test_unscramble_v1() {
test_unscramble(v1::unscramble)
}
#[test]
pub fn test_unscramble_v2() {
test_unscramble(v2::unscramble)
}
#[test]
pub fn test_unscramble_v3() {
test_unscramble(v3::unscramble)
}
#[test]
pub fn test_unscramble_v4() {
fn safe_v4(input: &str) -> LineResult {
unsafe { v4::unscramble(input) }
}
test_unscramble(safe_v4)
}

62
2021/day9/clean.py Executable file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env python
from collections import Counter
from dataclasses import dataclass
import math
import sys
from typing import Optional, Tuple
@dataclass
class Point:
height: int
low_point: Optional[Tuple[int, int]]
class Grid:
def __init__(self, data):
self.data = data
self.rows = len(data)
self.cols = len(data[0])
def get(self, x, y):
if 0 <= x < self.rows and 0 <= y < self.cols:
return self.data[x][y]
else:
return None
def find_low_point(self, x, y):
current = self.get(x, y)
if current is None:
raise ValueError
if current.low_point is not None:
return current.low_point
if current.height == 9:
return None
for dx, dy in ((-1, 0), (1, 0), (0, -1), (0, 1)):
target = self.get(x+dx, y+dy)
if target is not None:
if target.height < current.height:
current.low_point = self.find_low_point(x+dx, y+dy)
return current.low_point
return (x, y)
if __name__ == '__main__':
grid = Grid([
[
Point(height=int(n), low_point=None)
for n in i.strip()
]
for i in sys.stdin.readlines()
])
counts = Counter()
for x in range(grid.rows):
for y in range(grid.cols):
low_point = grid.find_low_point(x, y)
if low_point is not None:
counts.update([low_point])
print(sum(grid.get(x, y).height + 1 for x, y in counts.keys()))
print(math.prod(c for _, c in counts.most_common(3)))

50
2021/day9/dirty.py Executable file
View file

@ -0,0 +1,50 @@
#!/usr/bin/env python
import sys
import itertools
def walk_up(grid, x_orig, y_orig):
rows = len(grid)
cols = len(grid[0])
current = grid[x_orig][y_orig]
points = set()
for dx in (-1, 0, +1):
for dy in (-1, 0, +1):
if (dx == 0) == (dy == 0):
continue
x = x_orig + dx
y = y_orig + dy
if 0 <= x < rows and 0 <= y < cols:
if current < grid[x][y] < 9:
points.add((x, y))
points |= walk_up(grid, x, y)
return points
if __name__ == '__main__':
inp = [[int(n) for n in i.strip()] for i in sys.stdin.readlines()]
rows = len(inp)
cols = len(inp[0])
acc = 0
sizes = []
for x, row in enumerate(inp):
for y, n in enumerate(row):
if y > 0 and row[y-1] <= n:
continue
if y < cols-1 and row[y+1] <= n:
continue
if x > 0 and inp[x-1][y] <= n:
continue
if x < rows-1 and inp[x+1][y] <= n:
continue
points = walk_up(inp, x, y)
points.add((x, y))
sizes.append(len(points))
acc += n + 1
sizes.sort()
print(acc)
acc2 = 1
for x in sizes[-3:]:
acc2 *= x
print(acc2)