From fead587b2ac0bf939e29122656b4904a33970bca Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 8 Dec 2022 11:01:48 +0100 Subject: [PATCH] Implement 2022 day 8 part 1 --- 2022/benches/days.rs | 2 +- 2022/inputs/08.txt | 99 +++++++++++++++++++++++++++++++++++++++++ 2022/src/day08.rs | 76 ++++++++++++++++++++++++++++++- 2022/src/samples/08.txt | 5 +++ 4 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 2022/inputs/08.txt create mode 100644 2022/src/samples/08.txt diff --git a/2022/benches/days.rs b/2022/benches/days.rs index a75ead7..9bf8384 100644 --- a/2022/benches/days.rs +++ b/2022/benches/days.rs @@ -8,7 +8,7 @@ use criterion::BenchmarkId; use criterion::Criterion; /// Number of days we have an implementation to benchmark -const DAYS_IMPLEMENTED: u8 = 7; +const DAYS_IMPLEMENTED: u8 = 8; fn read_input(day: u8) -> Vec { let input_path = format!("inputs/{:02}.txt", day); diff --git a/2022/inputs/08.txt b/2022/inputs/08.txt new file mode 100644 index 0000000..67f3d8f --- /dev/null +++ b/2022/inputs/08.txt @@ -0,0 +1,99 @@ +133120320210233440424211425033311533112110111336536142004454550513525522325223123404213204010312200 +201131111014211324423255354022022243226445013613610423653614522135534505055120330313403031333103010 +312033232323231244025301315245341424106564334260061464515142114141551050411122254121204214442432210 +101223224313414001513045124413405604251532415616200623342234245060512030200002055453441222043022132 +302000022010423415300513533001221654231646352603366222420353100303160621313144421120541324031322221 +022041343104313414435021243154525050055065506466215004364316065443031303052541012204020300331224312 +103341401201343234101335152026143021665443260024145323023321363412215203352344024010350241320230403 +113420142141151035404302242616255522460104100534576523546245441245051223305614204355454223220120301 +141313120334140413444311032422155464031057323373712672513175353623531154646460010504312302300002224 +222122233241135333145102102244620325637577115357373355166254434732406430504542404102101335430322111 +303334221050505214133200215501142243567431213624743475716112467711442460620524310041114411011423020 +314200333004550151023253063621162447552734266222424315622125733562141045411643325165010440012411104 +002041023423222500521530364652321342767176666734452465675417573276212213343035032650010252311440041 +424112310115314154624123026322722176144615437667237551164455332134452657635041110204303331153310423 +403331525242111322464321027125164326531546671615634451453753666614652523355664316050035035431040223 +043240150545010061116502263626227235517673287377662722735257631252161774623714640414211242003124031 +120153432445266666010553121747235535567222344854553227754373283562766712275433030145041514430322020 +130205252451225625553367153422361135376345652238658565468884466665635712475577163231554521534443142 +111112125502543266131422234726554665525278247588365447865236325776254766323137341434403365242323513 +303144132003506154604725457356146262354554756268227328243768455722852644644163423635454133132303535 +355321430425362320027571672735837582654822875282477453446323242873743335342262324741355251635040132 +134315335666154514547724226424352647855667536237263286847676436882327536177762242121655513144251300 +540433012152253201644772517187257277883368828477658659796427367852347254442546347411045431414354124 +123440236041532033152645165666552487887293658447738333978367787858534683322123676621510536503333401 +513523462114216537146475772264724378429935336847846348444575554346687726237537112655265045664650022 +152254463440132245722174776775434287876777476649865634494799993998688326736773121545343121065304241 +250224151321236513227671833576566739347663376834388478944395578563955424353347453672417622666244155 +515223301360505722213752783545553644495694566664938776366634393433784855746658113613537562051065554 +554241245610333147211334537367835857366454575934899676895788946595666473766285253431355325406031452 +100314521636253352672223432738853968767459797559486648888395749643933624738538781345525612342532041 +341462234622615555235573736647638675877393747847457878744844899855446378852378466172227414636330202 +345125124225634624432677378235488673587578945954767695474694483979633396738646725252521775306550112 +413420352557414334236458423883348897736674897485778466695756868398399557654674634472471146623221025 +251143521254271126368347667353953675589776644454498798849645686658369675764886773874127564444411365 +221413142252611772737673846553885549867478965666795885777985495763559573589863845361134756354652555 +243065641366675316826485533843887969469458797465896675796454946875597936434758642384462157714462066 +520553502161266638252882759578566955579648578498775589886979787578896857766344736358531725733564135 +424035430516117723442423478595343855468789497988556567558494878666699996375768427758374271236421335 +252313015177661777278432468494855844559656687766987679695575558744879656544795683583262246546564621 +066230444571526644743382974447835556985988567697575656976987588986595376466888728567376162756500435 +466346066733165454546438886894789699679455689766757985688888664759568963398874674733715475614334101 +325014552177542688745486585579599747867559596676796865787567755944786444539663262282351475561402534 +241163303516225237353826934978454869995779765759887659668665685578986859343646684568651136735352562 +450361264326612736655359595445797648876877955597997887987765775498545883744548752574473726565411112 +554605215356217343577344399893857478777797855566878769757669857854854856357865565536552164136515351 +526446641376374684266375494495458695869978975788699978669887967564769488567376982882762633361324360 +131314671231526545866684867949989786895865986986978678997899677764988874736369482822342624761635224 +623254034413371684428636874768586849979569867987768799666579796798469744978968758623884477174221325 +451620621666445477562568544366499474968796999686779676678989576768596484478933764745243527321126622 +454130052341168772645439448775675586969558878998779766779998798589986885955369648636673756137543515 +052463667567627485865289577459895546659595768687867979887989666587865576357344373227477771625616331 +460630633631764266522893997758984967975685969969798677679989558694657986684466928784764257141450213 +624130637644652532575536775488944945689777797679988979876565896659695947897698966634635115577122034 +312106635521463635433584476586795448766769568869779667989675669974989668369378542526764316562641343 +162151455625714643563689555468449898859656786796979798687579985864586658783373657585741615177725655 +522113305657225658843435489885646557577988797667968888669868768989959977879557377455733555272325032 +304510566333111422674633384687874565587979787768979689685867565897484896499888736286384551117254613 +501463041455354857783745556844585568845776985978966685877875599798784456633539465754543114144204310 +033044042511137887672349889484876755578886688778959586777755687684946795536886522746575472552603266 +506221035322726455557224463853355996897775558969955685797667999588974556935495475632746634441041151 +433136161743725565262636565676447685475989796568778598958657768879849778786859753335661722543205305 +265454361556653132263478888989675487694888975877759959776785674478666588788563747772733262666414514 +052441631644734262283652379957577654769979687765967795697668686969788694345962866843241632716655446 +236003436545632166356247457495793686988694668988898785686844466844799668333966735544674562633412352 +521460600027621624875854349394389587767654998655578976768847769685597639387383648267451531624063352 +342222114334344716267243334967869638699594976567878976765649455495834476654882454551336614731204621 +033566313156263553885434364795389663465876465857797479766958478858465479967473865454116236530266023 +312652012603144155543338267594667756579949844587785799854455597889659839838335338227724422251205254 +300006533237512225736566526838475448787546947968579975977675656694697397732838257677271224106345603 +133143326544516157647526755363599347754745759889457964588954897669586537634253625642342472416641244 +154255220262671713413357272553997888597745494647794956965689397737688378433337485552276212554465555 +100240040401314462336283253854484757937369955476776566463546476496874852334852883176722554655020500 +114224164252562264455188387248845988369836588899857935337746669656565572827235667727764740332042104 +210035410165643165676172383567428973379998596753776399638478657839665546428878531777515004325230423 +210124643010215635561443542385838474565467799589387569934754637575772733563765332372676641151131504 +302221012422133674231332744234367852633548864367379437949776375866557752335873376756770315423300102 +332125266633066263556242566636554222886478446383395355568773939525563625678526672212234343222625410 +025325451166410144267526457225543863563576656495967865696633785745578788783545643421165350000423343 +002055431510265615765734547166486528454663847694453963495886284323368575223225414276066312104153401 +355245141260142612771253626632378444477523462347872628387736452238277834561572523626005254261254042 +203545210065351052363237141625823577635472367448676276452678742864237273162112414622640501555430121 +335510044411666505051763342712375473385677658477545568853568734655885763312211631300144242225254322 +420503535251235101224431771412136627846753333256668624663675776462422374227471570160056336045543305 +311442220344522144201454355125161416822475756464544562823568565277363143626236634605524051151344323 +221042430004563020306242752654674353744234455564453257548654537765566764222366445225313642034222311 +410315020300414623613025143245525766347157877736466252545465214236744364172443246502214503112203230 +342304354225251006345461616171626576425171512572743837515273314522745717111151266035240243530314400 +101402542354002125435251304674647534521162334775342133371111747561164265140621412560624450505304430 +011211315525203435452400223603127166746242764515517634515312173434163762164444142306041434112503201 +312201041521122523410120106532007714457744313215145447526753371116262403622123305243410143403304001 +420143230433241404304622552610552657576751252637557577175144676122663421114064036341501055504120312 +321114124142324224151525015553652243236362311234247651266113535462142056365353412114023231401141232 +144010233331150422332523034411415500511027722375253755262514425243333354512150011301540445044301243 +321223141143321313415515024451144622413543514327723454716501265226013550620005111202312303242203422 +220312222242043544521300416060642531526303363025212311016020154415132623214322110011054242222314401 +120043213410140522500232142235034335033562221102412305131143065020436150033340525535242031300403012 +333011210021031104353104420453564321600565055235040060414452305642301521213234000231332120021102311 +213000422032423433105500120521112552552162045443040565055142555024614151234210022135411312102303010 +311113213314300134505043355233124354500023350433035532626242646113034040102425400244311432332422101 diff --git a/2022/src/day08.rs b/2022/src/day08.rs index 745aff3..e6fb413 100644 --- a/2022/src/day08.rs +++ b/2022/src/day08.rs @@ -1,9 +1,81 @@ +use anyhow::Context; use anyhow::Result; -pub fn part1(_input: &[u8]) -> Result { - todo!() +#[inline] +fn stripe<'a>( + values: impl IntoIterator, + visible: impl IntoIterator, +) { + let mut max = 0; + + for (&val, visible) in values.into_iter().zip(visible) { + if val > max { + max = val; + *visible = true; + + if val == b'9' { + return; + } + } + } +} + +pub fn part1(input: &[u8]) -> Result { + let width = input + .iter() + .position(|&b| b == b'\n') + .context("Single row field")?; + let height = input.len() / (width + 1); // Include newlines + + let mut visible = vec![false; width * height]; + + // Horizontal striping + for (y, row) in input.chunks_exact(width + 1).enumerate() { + // First, left to right + stripe(&row[..width], &mut visible[(y * width)..]); + + // Then right to left + stripe( + row[..width].iter().rev(), + visible[(y * width)..(y * width + width)].iter_mut().rev(), + ); + } + + // Vertical striping + for x in 0..width { + // Top to bottom + stripe( + input[x..].iter().step_by(width + 1), + visible[x..].iter_mut().step_by(width), + ); + + // Bottom to top + stripe( + input[x..].iter().step_by(width + 1).rev(), + visible[x..].iter_mut().step_by(width).rev(), + ) + } + + Ok(visible.into_iter().filter(|&b| b).count().to_string()) } pub fn part2(_input: &[u8]) -> Result { todo!() } + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE: &[u8] = include_bytes!("samples/08.txt"); + + #[test] + fn sample_part1() { + assert_eq!(part1(SAMPLE).unwrap(), "21"); + } + + #[test] + fn sample_part2() { + assert_eq!(part2(SAMPLE).unwrap(), "8"); + } +} diff --git a/2022/src/samples/08.txt b/2022/src/samples/08.txt new file mode 100644 index 0000000..16d6fbd --- /dev/null +++ b/2022/src/samples/08.txt @@ -0,0 +1,5 @@ +30373 +25512 +65332 +33549 +35390