Day 8
This commit is contained in:
		
							parent
							
								
									b1c5d05eb8
								
							
						
					
					
						commit
						cdd1f5eb57
					
				
					 3 changed files with 233 additions and 1 deletions
				
			
		
							
								
								
									
										99
									
								
								res/08/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								res/08/input.txt
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,99 @@
 | 
			
		|||
131210223122034201100050105424523044130411422315401641602244345313413050154142511210232230010113310
 | 
			
		||||
311013023144242433041451004544121455454620031355066511426335055503430442412031400134421222323200231
 | 
			
		||||
000000231023234100542524312250523355415365261251616530425625664621215121033040340113311421124403120
 | 
			
		||||
112300444001303041405153523535126435661025243650554326352462442340031244522333142343310234422042101
 | 
			
		||||
011212224431121423533535502254210263641435632425025530513616353113250430340323201235001322302202112
 | 
			
		||||
202112313203021131220031503364123020251021060003210224226552235425544614111231232422312141400320413
 | 
			
		||||
200444200302403233024104523503202601044223254222154626546510545561110210263213430521241312421430303
 | 
			
		||||
004102313211311304304014024303123532011445320262467355722501012602644242025103522053013251103111002
 | 
			
		||||
304431411242103200225521661431265142636476475774766762762162363612154012423154144521032044323220414
 | 
			
		||||
431010040140112455335030032360262556456443775453334565671317716550430136651202313511214502241202330
 | 
			
		||||
043110240102454224516544224630616266622151633641416574141573472553733643035156556442540112231022330
 | 
			
		||||
344001014540504225063305264264244355672617755344543332512133532145451255143634431525512440433123411
 | 
			
		||||
113143353104203351103602240424523453451134364517415112721516225167142625124311653430211020140140033
 | 
			
		||||
342424105441510440330522410035715716774467613477463165551222537127247464353412121551250032130221402
 | 
			
		||||
410022504110015140563646535637544514224772163251875687177773617131666312426516653364620212210230313
 | 
			
		||||
211041303415212543246450027775344644111513854625668347224721545553312454276112266113006550101452023
 | 
			
		||||
122004120501250326115204667575557541115833886847258462846234567626374111773571244513654010011342043
 | 
			
		||||
301350253554662030333624245656331336684563833452867366254385562353677252735134322645443400000505332
 | 
			
		||||
245553522153032254411763514675764155633476526675325578425443428557445775637465473664541400534333232
 | 
			
		||||
322442130433125464663414766715267753434324555463784727253433653632683735371446657522664436422521413
 | 
			
		||||
331053445014305402543221122137356667287622582365352535357653444332458554753476126241266505611211314
 | 
			
		||||
132305431204423645643332442378265875383382785757384646688636277356473225215656144464631013404513545
 | 
			
		||||
053434505352362212362322254526384368455578549563664455533528348238256867477226167554313140041401143
 | 
			
		||||
203253201234236243775741122764852566323853963388779975599397567878847548722235622474163122662031025
 | 
			
		||||
104312526333250166615172668583238338723855457693768963653558894638325372838152126473716643303600351
 | 
			
		||||
300033325433641325623235156626436335535335388847464645964634799646746246766834574361166125456122351
 | 
			
		||||
120451143220054517417432222372537276354686677646995668437869779369928258788534513761754512104013333
 | 
			
		||||
533230405632016226763328844653255879987575353774339563955877645966846826356373371671142605624114450
 | 
			
		||||
344214406630075321522734573335883369387497369848437466643758736756744863635423414516662160242011015
 | 
			
		||||
153451326406137727666257774235355975796748378956899755978495944398847342278543532717554136340636511
 | 
			
		||||
024120564062654627732368525228793638475545556576899665765756673653534943354538526752326643260141204
 | 
			
		||||
412203404021155532277533544576338345978848669679757658576464434458866475384364264335343563643142455
 | 
			
		||||
524002156645642574176682747734737558688944465667846776898785795455858947565382387667331113015066451
 | 
			
		||||
113326321336515741227873667767747863748957949856994566445966947557698763647356484312144362112035661
 | 
			
		||||
434431122332456363585644236338986386449886459969898794477784876859584934744448487422233247433555125
 | 
			
		||||
554200316641747712525646475695585944855767864476587785657648489858488643964822385635771521136623046
 | 
			
		||||
052645555643364326224256887465763984499555456786655996459854588687984483443425342327437117204051333
 | 
			
		||||
324343644132162478234438235893544695988896887697765955598567945698759463346462838746232654576440215
 | 
			
		||||
462521542732624633374484345633767949856985955975898956868594588984946798839592836667471162150441636
 | 
			
		||||
304026636645461478734587878689834446685557768558895888866986985488496437548446368538251171154630512
 | 
			
		||||
101552533162346275323229586354898455686576855758557778659698597964984437888644223388226555216163411
 | 
			
		||||
226105531714231626224846935734356584745868996975697659897867984899744976774696753265736467266365363
 | 
			
		||||
545055625455625875447559864667957979647968997958989658678889877465465468549376486535366137522210053
 | 
			
		||||
232635236714655674743537653457749846596956975956766868889657789549747795675485857275755452342405631
 | 
			
		||||
336205405746366588226663467669498957868656755776686987887868569798659876543375846338247764415355462
 | 
			
		||||
155141051431316677636748574867789558675986588998679878868759877859999565576663475662435765647005340
 | 
			
		||||
232654031116753272825747668968554669668677998669769697669576866955596675964889624528726432263222616
 | 
			
		||||
046125447675236746223768668449879754576588858988996889696887777774597744733788336362244612637553514
 | 
			
		||||
332536552224754343576649855595666484566675788668896786768988988769569579999834385254427632624650030
 | 
			
		||||
460234671543372887673258495575749597595556689798666968989999596584956587675793948224673776637134104
 | 
			
		||||
542551244113357255333348985978787695699886688996687688989678979668986898375478542858365611723122044
 | 
			
		||||
436025232763148825472748645398546496988875698898869977888888759686766658866855938347566311743605100
 | 
			
		||||
041352435563335243486289834394558775588869568696989689688668679659674985985749374752633143335411605
 | 
			
		||||
645651613323411838844847964446478646777866888967786698987886597984667558653798334378662326523323316
 | 
			
		||||
453251236357514443523673467349998744667597789678969978697885696567487655374593857785247555435556045
 | 
			
		||||
125626067513221885878857759866589459898567986669876767888879686774468986669574866876872256777244651
 | 
			
		||||
061632634136353483585356796789964457467578899876878869685969688887694989435735523286834312755525066
 | 
			
		||||
340540044551242754354545465975657758685657898679669667967695977988789866474643553377742333237366424
 | 
			
		||||
263036352242131347425624853533685546887657969789867597887875857754448643936644634734681251565046213
 | 
			
		||||
521221014252416228623283758687657746455765599789696566599868586857544488568349257532612223352106620
 | 
			
		||||
551056515565333357773577346794385876469668655769579779597857875875996655775847823478875155335563305
 | 
			
		||||
033061351361131165452262574439975544867848695885675877665879558695985696596744556573716376424303526
 | 
			
		||||
051413453164367157546735796993358565449465886965769896875794786968869649574645557564453516434340036
 | 
			
		||||
522012306661675468522365554866664944774645598595765956997799788597559834845664822564673263773400310
 | 
			
		||||
303552112356652526736235234953975468449454679549576957655985777498534633999648453476132767604136505
 | 
			
		||||
430122433512263557743477259647593668744879856557445598497869544969574539864264546361414462664221411
 | 
			
		||||
454611134331624353887744383937939357467695869797758446748645646757337474547364356834125123550540011
 | 
			
		||||
205311422445771735787735535567487687866565989949866754567798798789335594675342524621173252621030620
 | 
			
		||||
104546314105111141478387738596677754347769889645474649778444486556868546667547447347454232412330210
 | 
			
		||||
211510511334467714212764237753489789395879446757548685444899995947477635333458277512623242566130511
 | 
			
		||||
424514361505346363172223743443943549786984455999479779995888784953977545326775458231326323010233504
 | 
			
		||||
533506406351652124743874362238244995393575576558499944857439678937793742537382237144513564254651535
 | 
			
		||||
353553630060426172733685862388329358887865467969479496565936757634945866336662814247313410506123210
 | 
			
		||||
513043225146416126342738452268474836363669357555484378683836437894548678424787336776674656521041540
 | 
			
		||||
244212015566607562363563332245262634763988943955763374435446957788627758456361246547173556634433112
 | 
			
		||||
120250204244540161453135477576577584679685399997348333875938895345463364665351151547550433420451010
 | 
			
		||||
255032316662364526536463566477643566873988833997886594683996985547474224255612136321352552052105402
 | 
			
		||||
104213510451031021331446321843388442357338856338535749438457984676336446257657625355006562000544113
 | 
			
		||||
541445315342554104332147175678875788872263746578533386785725742286747757363143223363002242403240300
 | 
			
		||||
250054145114114250444475636452277673763367766534832262225326442862356536367112741723425324561325551
 | 
			
		||||
333553132316232331166236712324478868545457256543258752567822364684454281563652476110466630243545012
 | 
			
		||||
105553252451444255251516625337126655486536656548634366772767853646345616517533725655446161145113520
 | 
			
		||||
025500320531252635012635251245524534532874583455824435258654245466851271721413732103556450032134212
 | 
			
		||||
122200331203513436340117775373456645476436746876327282863778274541722364747346544343564664345130314
 | 
			
		||||
441320433033461423032344434231635142515424736857357883276527554723561743647654233055512312302104530
 | 
			
		||||
204042200400054122313353457757245116453332283484837768668273767612521361442530615235236222154004311
 | 
			
		||||
221131430533350565006640022453717233151527675542578563343653736225637243741111121060442013041415121
 | 
			
		||||
143234105412111133566044252147564714332714531116676347152755116543123447451004602504145444453253433
 | 
			
		||||
320324355203321243353463365121412173726252341762654343152225445674136341343221112445214335524423100
 | 
			
		||||
202300215012154413506542142320154422415625511216416742722732625443456234050601116134422203301324102
 | 
			
		||||
101234204150414100204253404415020535764151322156246545763562327134761563456554466111402435010443123
 | 
			
		||||
431241014215100150231323511544033066474456542763733572523364653251210236631153343300102203501444131
 | 
			
		||||
042113314231102110430451540516134616441273312433773513644616643153262013464225504105041102141121134
 | 
			
		||||
011110441120300210525243055356613232510501653115647342112643545365615664665363430033144102220342002
 | 
			
		||||
022302231313301100120453535411263110120014430464665351336004360420564432406415043521343230142324012
 | 
			
		||||
113120323401112034331301531555663250242545635553155553026665545501304141205024555331305404432131422
 | 
			
		||||
012114144232442305005303300503446534353541113202335113010041055013513101112435052423113214420112332
 | 
			
		||||
230302124240141434221315223103536661161244640554266246312142632216301114101010004305311113412410032
 | 
			
		||||
023121121202444024012413530551503240563056005135401560566653254102253143503152511431441042423320132
 | 
			
		||||
							
								
								
									
										114
									
								
								src/days/d08.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										114
									
								
								src/days/d08.rs
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,114 @@
 | 
			
		|||
use std::fs;
 | 
			
		||||
 | 
			
		||||
pub fn solve() {
 | 
			
		||||
    let path = "res/08/input.txt";
 | 
			
		||||
 | 
			
		||||
    let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
 | 
			
		||||
 | 
			
		||||
    let cont_arr = contents.lines().collect::<Vec<&str>>();
 | 
			
		||||
 | 
			
		||||
    // parse trees as 2D i32 Vec
 | 
			
		||||
    let trees: Vec<Vec<i32>> = cont_arr.iter()
 | 
			
		||||
        .filter(|line| **line != "")
 | 
			
		||||
        .map(|line| 
 | 
			
		||||
            line.chars().collect::<Vec<char>>()
 | 
			
		||||
            .iter().map(|t| return t.to_digit(10).unwrap() as i32)
 | 
			
		||||
            .collect()
 | 
			
		||||
        ).collect();
 | 
			
		||||
 | 
			
		||||
    let width = trees[0].len();
 | 
			
		||||
    let height = trees.len();
 | 
			
		||||
 | 
			
		||||
    // task 1
 | 
			
		||||
    // initialize visibilities with 0
 | 
			
		||||
    let mut visibilities: Vec<Vec<i32>> = (0..height).map(|_| {
 | 
			
		||||
        (0..width).map(|_| 0).collect()
 | 
			
		||||
    }).collect();
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // mark visible trees with 1
 | 
			
		||||
    // left to right & right to left
 | 
			
		||||
    for i in 0..height {
 | 
			
		||||
        let mut last_max_left = -1;
 | 
			
		||||
        let mut last_max_right = -1;
 | 
			
		||||
 | 
			
		||||
        for j in 0..width {
 | 
			
		||||
            if trees[i][j] > last_max_left {
 | 
			
		||||
                visibilities[i][j] = 1;
 | 
			
		||||
                last_max_left = trees[i][j];
 | 
			
		||||
            }
 | 
			
		||||
            if trees[i][width-j-1] > last_max_right {
 | 
			
		||||
                visibilities[i][width-j-1] = 1;
 | 
			
		||||
                last_max_right = trees[i][width-j-1];
 | 
			
		||||
            } 
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // top to bot & bot to top
 | 
			
		||||
    for i in 0..width {
 | 
			
		||||
        let mut last_max_top = -1;
 | 
			
		||||
        let mut last_max_bot = -1;
 | 
			
		||||
 | 
			
		||||
        for j in 0..height {
 | 
			
		||||
            if trees[j][i] > last_max_top {
 | 
			
		||||
                visibilities[j][i] = 1;
 | 
			
		||||
                last_max_top = trees[j][i];
 | 
			
		||||
            }
 | 
			
		||||
            if trees[width-j-1][i] > last_max_bot {
 | 
			
		||||
                visibilities[height-j-1][i] = 1;
 | 
			
		||||
                last_max_bot = trees[height-j-1][i];
 | 
			
		||||
            } 
 | 
			
		||||
            
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // sum all visible trees
 | 
			
		||||
    let res = visibilities.iter()
 | 
			
		||||
        .map(|line| line.iter().sum::<i32>())
 | 
			
		||||
        .sum::<i32>();
 | 
			
		||||
 | 
			
		||||
    println!("Result 1: {}", res);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    // task 2
 | 
			
		||||
    let scenic_scores: Vec<Vec<i32>> = (0..height).map(|i| {
 | 
			
		||||
        (0..width).map(|j| {
 | 
			
		||||
            // calculate view range in each direction
 | 
			
		||||
            let mut to_top = 0;
 | 
			
		||||
            for k in (0..i).rev() {
 | 
			
		||||
                to_top += 1;
 | 
			
		||||
                if trees[k][j] >= trees[i][j] { break; }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let mut to_bot = 0;
 | 
			
		||||
            for k in i+1..height {
 | 
			
		||||
                to_bot += 1;
 | 
			
		||||
                if trees[k][j] >= trees[i][j] { break; }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let mut to_left = 0;
 | 
			
		||||
            for k in (0..j).rev() {
 | 
			
		||||
                to_left += 1;
 | 
			
		||||
                if trees[i][k] >= trees[i][j] { break; }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            let mut to_right = 0;
 | 
			
		||||
            for k in j+1..width {
 | 
			
		||||
                to_right += 1;
 | 
			
		||||
                if trees[i][k] >= trees[i][j] { break; }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            to_top*to_bot*to_left*to_right
 | 
			
		||||
        }).collect()
 | 
			
		||||
    }).collect();
 | 
			
		||||
    
 | 
			
		||||
    // find max scenic score
 | 
			
		||||
    let res = scenic_scores.iter()
 | 
			
		||||
        .map(|line| line.iter().max().unwrap())
 | 
			
		||||
        .max().unwrap();
 | 
			
		||||
    
 | 
			
		||||
    println!("Result 2: {}", res)
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								src/main.rs
									
									
									
									
									
								
							| 
						 | 
				
			
			@ -1,5 +1,24 @@
 | 
			
		|||
pub mod days;
 | 
			
		||||
 | 
			
		||||
fn main() {
 | 
			
		||||
    days::d07::solve();
 | 
			
		||||
    _all_days()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn _all_days() {
 | 
			
		||||
    println!("Day 1");
 | 
			
		||||
    days::d01::solve();
 | 
			
		||||
    println!("\nDay 2");
 | 
			
		||||
    days::d02::solve();
 | 
			
		||||
    println!("\nDay 3");
 | 
			
		||||
    days::d03::solve();
 | 
			
		||||
    println!("\nDay 4");
 | 
			
		||||
    days::d04::solve();
 | 
			
		||||
    println!("\nDay 5");
 | 
			
		||||
    days::d05::solve();
 | 
			
		||||
    println!("\nDay 6");
 | 
			
		||||
    days::d06::solve();
 | 
			
		||||
    println!("\nDay 7");
 | 
			
		||||
    days::d07::solve();
 | 
			
		||||
    println!("\nDay 8");
 | 
			
		||||
    days::d08::solve();
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue