Compare commits
	
		
			7 commits
		
	
	
		
			
				17740888b5
			
			...
			
				9bd074744b
			
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
							
							
								
							
							
	
	
		
			
		
	
	9bd074744b | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	fd7416456b | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	0ee7c433e3 | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	9a5252b635 | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	3a3f62148b | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	b2d66319e3 | 
						
						
							|||
| 
							
							
								
							
							
	
	
		
			
		
	
	62bba80378 | 
						
						
							
					 8 changed files with 1315 additions and 14 deletions
				
			
		| 
						 | 
					@ -12,4 +12,7 @@ rpath = true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
[dependencies]
 | 
					[dependencies]
 | 
				
			||||||
nom = "7.1.1"
 | 
					nom = "7.1.1"
 | 
				
			||||||
itertools = "0.10.5"
 | 
					itertools = "0.11.0"
 | 
				
			||||||
 | 
					camino = "1.1.6"
 | 
				
			||||||
 | 
					id_tree = "1.8.0"
 | 
				
			||||||
 | 
					color-eyre = "0.6.2"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										1027
									
								
								prob7_part2.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1027
									
								
								prob7_part2.txt
									
										
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							
							
								
								
									
										99
									
								
								prob8_part1.txt
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										99
									
								
								prob8_part1.txt
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,99 @@
 | 
				
			||||||
 | 
					101232232424431123432342554040352101405502025646520640026102435415402415524241404341001022013221221
 | 
				
			||||||
 | 
					322003200404313034023203145443134010555246652210042156042654635314322525041550121322403330300102030
 | 
				
			||||||
 | 
					300223331202022110511510154103110142442012332003514242206354525511353252421542430110200442102312123
 | 
				
			||||||
 | 
					323133401312301310040251134225352136126616004434252462045055305202261355433025233042244241414400023
 | 
				
			||||||
 | 
					101104403040123044220034201231621525016350121330311611014454116414515052210415423202213344444213032
 | 
				
			||||||
 | 
					023343200312321341111312312116004553510400124564003503525336352206534312634553034341344231103320121
 | 
				
			||||||
 | 
					232432314323445335412313341660535056605316245521312422204421411026063010063200110052210224330113302
 | 
				
			||||||
 | 
					312104103023300150511003256056530232416620641476331566726505005253550226461151520445555123411440334
 | 
				
			||||||
 | 
					212320403040021521143113451320122514052261721742217616232666314261562006620634125252332350322233244
 | 
				
			||||||
 | 
					121203010304521210411013440333264052674653423316314375512175574325262425522003540134541121330431412
 | 
				
			||||||
 | 
					242204210504314451204236552066106466756545677241223651236433541213145114512054256134345025222414332
 | 
				
			||||||
 | 
					142041442305525524414554456630445566671622363554565311414164346521375126616036343225552430320142242
 | 
				
			||||||
 | 
					444343225200452531125463161044526512711315644346716774456167633775552325212323502414500125304101442
 | 
				
			||||||
 | 
					310331200433511524004324523024376145644272331714275745132552527332621757111641110604243400150102301
 | 
				
			||||||
 | 
					324000410032344243443213031215347666416142612722352331527416524446272144632054626460610411343015214
 | 
				
			||||||
 | 
					320123043045014453006030311336322753322266368648765847556774576424774346643352662320364111534525122
 | 
				
			||||||
 | 
					442020510341512162315013222156456642565478638555682735664832583554422245165431250246236222000222413
 | 
				
			||||||
 | 
					414115045035446663521337425454354252666867442648382483378248585426324451353146435112651062444320153
 | 
				
			||||||
 | 
					404031553354665600421273415333424442557623485578324525478673862565714171753646445146504611043544310
 | 
				
			||||||
 | 
					420221121313524320031226342733736342778854854745272737436344833444332262273475742543435414301514022
 | 
				
			||||||
 | 
					102433434104221136261652367123828833742266365587682657232782236687733375147446456435013164623033453
 | 
				
			||||||
 | 
					254052000640223505333752451714435245653688563475537427586864344637533256524566641541066561013340115
 | 
				
			||||||
 | 
					402424554664414061612366662262546262344862767983768964383722283664424826344415366464635564003035522
 | 
				
			||||||
 | 
					453430306616221255375164464844337767643264664566433699446375333477788728421713267327336401105425333
 | 
				
			||||||
 | 
					424340460333633611416743647223584543446663447884569869575943675246265858843135752722203060601314510
 | 
				
			||||||
 | 
					100104404661641521713564623486853464898563575445444677875368568656336855687665453132126163413415413
 | 
				
			||||||
 | 
					215302243412504531566112426534574499379653449589737746345943794657936355232887343422456306315453552
 | 
				
			||||||
 | 
					510025400262416244231532674726546437535536634838644683689356756484986473434524723553734023111254223
 | 
				
			||||||
 | 
					200204056365027317421364428852638397454355393937474567465453855598477485557424664756156234516125002
 | 
				
			||||||
 | 
					315311346005136326466555385687277868747539849498675894853944993694973352587363421743725352145116525
 | 
				
			||||||
 | 
					003432325612552157642734768322776884596464869575664456575756766387847762385773543511142455502615010
 | 
				
			||||||
 | 
					323005315540227472658648485349375446787359896487975999967994878588768333826274268465567534243515604
 | 
				
			||||||
 | 
					440456221306764751672857476368954437466478794954588479785445855488353476362467345856473677540155641
 | 
				
			||||||
 | 
					145626220661445722124873768476448493998999449996678498948886598577843394688625385873326644342243461
 | 
				
			||||||
 | 
					445562611272117173322737877366885654845578785685455598488999678884466965543672555556173431656206420
 | 
				
			||||||
 | 
					025130063437366574263434683849438959768954744996857595658945978854575385745862424366457372746221315
 | 
				
			||||||
 | 
					330056343042353748468655383756666954488668996955869957549949945974898849943638748754612625120241011
 | 
				
			||||||
 | 
					432440053115343158276372498785736949588874688959799775598548957878884775394762348284137737673641642
 | 
				
			||||||
 | 
					102056040435747284853655648443579469664944568788855866779976889755858957959735324265746533251062403
 | 
				
			||||||
 | 
					225266001744262443233334974839654865799445785659659675575666575758865948754463425885535443736632404
 | 
				
			||||||
 | 
					110654201772435145572468544985578676579996656795879868576797847675977856844578738333816426565246622
 | 
				
			||||||
 | 
					433122252566473253723864943786447484655995769756667897965855958658757798373853334626865115523210441
 | 
				
			||||||
 | 
					556541053615336747283763545495655688878588665885858779779899597977449644485399674475255622322020110
 | 
				
			||||||
 | 
					011246513266431242864686953687878987686577868895997988788689566679898456889984483487464125476243224
 | 
				
			||||||
 | 
					146430654214211787457337933537555866765688875799887976988578679556659655564463526386463716114354031
 | 
				
			||||||
 | 
					301355426474121386642848739759597465855588776978689979798756956985699554573779455545642175346410200
 | 
				
			||||||
 | 
					665401551644225425227738385396554475758768868798997798968696775989794666575779876332737347375414225
 | 
				
			||||||
 | 
					512264373337161748463436934959669586879679979679896989676576695988989664437878448276827532543745623
 | 
				
			||||||
 | 
					414644336467578472474486349936485857567669856689768668976766988984549484863488738455255412422624432
 | 
				
			||||||
 | 
					036441527146156483775836379836679964765795668878769977878799767597698768864788657252857655434752204
 | 
				
			||||||
 | 
					444666167464342238764266465439498755555659877886888899999856978994556568955445528325646125672164600
 | 
				
			||||||
 | 
					554433027362778886523875976836986988896695897689678886787996679658974797569374338344624672757631301
 | 
				
			||||||
 | 
					310042272613272624885896949859765467876555996976999696789988967965466684754895746762223775172234330
 | 
				
			||||||
 | 
					216343113411562425626633578339674859855875577977769879987785777674498658748899947272432652777416624
 | 
				
			||||||
 | 
					644655617213144523726243646388856494887979798868676998696768878865895676484383655444254627274131426
 | 
				
			||||||
 | 
					563636621655445248723867836434648597896597866798698876698598597697577776937367826877324161661354535
 | 
				
			||||||
 | 
					341056514244726338453499673897657855866875759776669996869785685895756748635866544776371413425025432
 | 
				
			||||||
 | 
					044435013274135828365337573975868886748866596788969877955955756895957574564469583387354142524005261
 | 
				
			||||||
 | 
					410342122777647883362439734978585444678669878589755875966798767797995783366779884352756543534151243
 | 
				
			||||||
 | 
					441043541147123324228379874699597946948555885687569696976589658895855548467778233287211424145465240
 | 
				
			||||||
 | 
					013520313145473358567557484588597487658658866969675558679557775795544577887779883542724441435601554
 | 
				
			||||||
 | 
					413424243572773133387782457533847759697988966666777979879595749686579798343746884752342364430012243
 | 
				
			||||||
 | 
					326240460425117754473464599468874499568546888968889968769655795459984446735672755758632161131203442
 | 
				
			||||||
 | 
					300131303151234667345248566663377796947878745698979798686969577865488637644574724367556777536526223
 | 
				
			||||||
 | 
					201413522556166618225635288589984557686665556767657595967679955648876746974783378576524471465423550
 | 
				
			||||||
 | 
					132203011242354537628633669369699337985447975976769776866447886769358896665782776877447544703406253
 | 
				
			||||||
 | 
					133532343456535111383888625455386597867455655599757897755558666966586346883426636652263242705454404
 | 
				
			||||||
 | 
					515656631143165354267432455336475494569987585578555494867798784856975634394283638544164731533012364
 | 
				
			||||||
 | 
					140416432445724172247278853455798367884868796498777454967657748875548789832362674732676672056032431
 | 
				
			||||||
 | 
					330330152414564665274243438433876363573585996985888846846845663567545668252624564762246173301325502
 | 
				
			||||||
 | 
					302226105363143741352735557782868788733464859494486947884994938599357344542482523627145432560642353
 | 
				
			||||||
 | 
					145422126105171466242567724557544489934658889494656859885599738537534845443644581256761746362221023
 | 
				
			||||||
 | 
					441212145032074213275667582428345868377677696997785565377585765567746584667226657214272426340532331
 | 
				
			||||||
 | 
					101452412564254231314347763234647978536934546886949376683847693599962882662556735366332605253254243
 | 
				
			||||||
 | 
					242134245465654443763242368746262775635568895968649374696544643593776484646237623121155311612120205
 | 
				
			||||||
 | 
					224324556334241543152423764688727563658665947774377475559746775387478253676754241443542553641515133
 | 
				
			||||||
 | 
					112040215053533166723764146688552662254436968389648683363668663468622524888354726754315425216123100
 | 
				
			||||||
 | 
					021125312165155354236637163434868472666833569875463833389786585853644277477624254454065142204421252
 | 
				
			||||||
 | 
					524302450104653624156271174347323843256576684986975339949343684428633575452572646373611442213410412
 | 
				
			||||||
 | 
					013000504615163043526333634318868574767387865682722275245644673556658444321441613432604542203511551
 | 
				
			||||||
 | 
					242524501014131565575336754147245826628343573377352338767422674636538742125631345462033033403235255
 | 
				
			||||||
 | 
					304401342145144243415545241676576556264543764465565724532645542743372525221461725562353265045051132
 | 
				
			||||||
 | 
					234302031320344616366225577677124763333456446683735866667852458578422752674576640145530645050512154
 | 
				
			||||||
 | 
					132421020111321255530526663734272513535262623562488642564636574385245112665623132642441334102314011
 | 
				
			||||||
 | 
					102255335052511135645343464117315257162723258847453624673355647345665737125672220566436213014134441
 | 
				
			||||||
 | 
					312245204230352351210405226233122343511172545755556267356584643164226213732752212035632152433154320
 | 
				
			||||||
 | 
					433104142453501532612362446717232767156326652752737733565427127523161253271344216136060252512442041
 | 
				
			||||||
 | 
					212230512443134051666444123274474475364671426411226276576372452514665156262633036430153240020401401
 | 
				
			||||||
 | 
					300411330411451231141436052664142373444325735263226333375416444576253146002526306114152241250403310
 | 
				
			||||||
 | 
					101032115301513352525160453141002773563414647477456721366627231126771542123126102455031250033223313
 | 
				
			||||||
 | 
					324233212142235532140166110023400026333355425117234444773232146225326253461050325545343105500423104
 | 
				
			||||||
 | 
					033431121034120033510616215166546146337352752676632624643526424216650102540533503153134132000420142
 | 
				
			||||||
 | 
					240121103430140123513513462300432156652122676414274512336151334241532064154406340323144200421003324
 | 
				
			||||||
 | 
					233142431112012541421523134462664026316416350553765272706623513350020256351263445534113304131112033
 | 
				
			||||||
 | 
					323203003131024144404243206121423310355043066662201116504344265455134633403341445324024304010343200
 | 
				
			||||||
 | 
					133044014403300041520020101412541120000145660632333004501603416535515112534533414430000213113231220
 | 
				
			||||||
 | 
					020200134443230224532201430335140300110631641143466045463552301460233403451514045510012313003444012
 | 
				
			||||||
 | 
					003320111443312431412215134024143654125511235514301244523512535041410415313130552501342112344000332
 | 
				
			||||||
 | 
					223322121002242440120225413345342544646200604640400343653451404544533034511344250023302010222012033
 | 
				
			||||||
| 
						 | 
					@ -5,3 +5,5 @@ pub mod prob4;
 | 
				
			||||||
pub mod prob5;
 | 
					pub mod prob5;
 | 
				
			||||||
pub mod prob6;
 | 
					pub mod prob6;
 | 
				
			||||||
pub mod prob7;
 | 
					pub mod prob7;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub mod prob8;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										16
									
								
								src/main.rs
									
										
									
									
									
								
							
							
						
						
									
										16
									
								
								src/main.rs
									
										
									
									
									
								
							| 
						 | 
					@ -1,6 +1,8 @@
 | 
				
			||||||
use advent_of_code::{prob1, prob2, prob3, prob4, prob5, prob6};
 | 
					use advent_of_code::{prob1, prob2, prob3, prob4, prob5, prob6, prob7};
 | 
				
			||||||
 | 
					use color_eyre::eyre::Result;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn main() {
 | 
					fn main() -> Result<()> {
 | 
				
			||||||
 | 
					    color_eyre::install()?;
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        let file_path = "prob1.txt";
 | 
					        let file_path = "prob1.txt";
 | 
				
			||||||
        let max_part1 = prob1::solve_part1(file_path);
 | 
					        let max_part1 = prob1::solve_part1(file_path);
 | 
				
			||||||
| 
						 | 
					@ -53,4 +55,14 @@ fn main() {
 | 
				
			||||||
        let solution = prob6::solve_part2(include_str!("../prob6_part1.txt"));
 | 
					        let solution = prob6::solve_part2(include_str!("../prob6_part1.txt"));
 | 
				
			||||||
        println!("Solution for problem 6 part 2: {solution}");
 | 
					        println!("Solution for problem 6 part 2: {solution}");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        let _file_path = "prob7_part1.txt";
 | 
				
			||||||
 | 
					        let solution = prob7::solve_part1(include_str!("../prob7_part1.txt"))?;
 | 
				
			||||||
 | 
					        println!("Solution for problem 7 part 1: {solution}");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        let solution = prob7::solve_part2(include_str!("../prob7_part2.txt"))?;
 | 
				
			||||||
 | 
					        println!("Solution for problem 7 part 2: {solution}");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    Ok(())
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										180
									
								
								src/prob7.rs
									
										
									
									
									
								
							
							
						
						
									
										180
									
								
								src/prob7.rs
									
										
									
									
									
								
							| 
						 | 
					@ -1,17 +1,175 @@
 | 
				
			||||||
use nom::{branch::alt, bytes::complete::tag, combinator::map, IResult};
 | 
					use camino::Utf8PathBuf;
 | 
				
			||||||
 | 
					use id_tree::{InsertBehavior, Node, Tree};
 | 
				
			||||||
 | 
					use nom::{
 | 
				
			||||||
 | 
					    branch::alt,
 | 
				
			||||||
 | 
					    bytes::complete::{tag, take_while1},
 | 
				
			||||||
 | 
					    combinator::{all_consuming, map},
 | 
				
			||||||
 | 
					    sequence::{preceded, separated_pair},
 | 
				
			||||||
 | 
					    Finish, IResult,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::prob7::Instructions::{Cd, Dir, Ls};
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					struct Ls;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum Instructions {
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					struct Cd(Utf8PathBuf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					enum Entry {
 | 
				
			||||||
 | 
					    Dir(Utf8PathBuf),
 | 
				
			||||||
 | 
					    File(u64, Utf8PathBuf),
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug)]
 | 
				
			||||||
 | 
					enum Command {
 | 
				
			||||||
    Ls,
 | 
					    Ls,
 | 
				
			||||||
    Cd,
 | 
					    Cd(Utf8PathBuf),
 | 
				
			||||||
    Dir,
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn parse_instruction(i: &str) -> IResult<&str, Instructions> {
 | 
					#[derive(Debug)]
 | 
				
			||||||
    alt((
 | 
					enum Line {
 | 
				
			||||||
        map(tag("ls"), |_| Ls),
 | 
					    Command(Command),
 | 
				
			||||||
        map(tag("cd"), |_| Cd),
 | 
					    Entry(Entry),
 | 
				
			||||||
        map(tag("dir"), |_| Dir),
 | 
					}
 | 
				
			||||||
    ))(i)
 | 
					
 | 
				
			||||||
 | 
					impl From<Ls> for Command {
 | 
				
			||||||
 | 
					    fn from(_ls: Ls) -> Self {
 | 
				
			||||||
 | 
					        Command::Ls
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl From<Cd> for Command {
 | 
				
			||||||
 | 
					    fn from(cd: Cd) -> Self {
 | 
				
			||||||
 | 
					        Command::Cd(cd.0)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[derive(Debug, Default)]
 | 
				
			||||||
 | 
					struct FsEntry {
 | 
				
			||||||
 | 
					    path: Utf8PathBuf,
 | 
				
			||||||
 | 
					    size: u64,
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn parse_entry(input: &str) -> IResult<&str, Entry> {
 | 
				
			||||||
 | 
					    let parse_file = map(
 | 
				
			||||||
 | 
					        separated_pair(nom::character::complete::u64, tag(" "), parse_path),
 | 
				
			||||||
 | 
					        |(size, path)| Entry::File(size, path),
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    let parse_dir = map(preceded(tag("dir "), parse_path), Entry::Dir);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    alt((parse_file, parse_dir))(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn parse_path(input: &str) -> IResult<&str, Utf8PathBuf> {
 | 
				
			||||||
 | 
					    map(
 | 
				
			||||||
 | 
					        take_while1(|c: char| "abcdefghijklmnopqrstuvwxyz./".contains(c)),
 | 
				
			||||||
 | 
					        Into::into,
 | 
				
			||||||
 | 
					    )(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn parse_ls(input: &str) -> IResult<&str, Ls> {
 | 
				
			||||||
 | 
					    map(tag("ls"), |_| Ls)(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					fn parse_cd(input: &str) -> IResult<&str, Cd> {
 | 
				
			||||||
 | 
					    map(preceded(tag("cd "), parse_path), Cd)(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn parse_command(line: &str) -> IResult<&str, Command> {
 | 
				
			||||||
 | 
					    let (input, _) = tag("$ ")(line)?;
 | 
				
			||||||
 | 
					    alt((map(parse_ls, Into::into), map(parse_cd, Into::into)))(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn parse_line(input: &str) -> IResult<&str, Line> {
 | 
				
			||||||
 | 
					    alt((
 | 
				
			||||||
 | 
					        map(parse_command, Line::Command),
 | 
				
			||||||
 | 
					        map(parse_entry, Line::Entry),
 | 
				
			||||||
 | 
					    ))(input)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn total_size(tree: &Tree<FsEntry>, node: &Node<FsEntry>) -> color_eyre::Result<u64> {
 | 
				
			||||||
 | 
					    let mut total = node.data().size;
 | 
				
			||||||
 | 
					    for child in node.children() {
 | 
				
			||||||
 | 
					        total += total_size(tree, tree.get(child)?)?;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    Ok(total)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn generate_tree(content: &str) -> color_eyre::Result<Tree<FsEntry>> {
 | 
				
			||||||
 | 
					    let lines = content.lines().map(|line| {
 | 
				
			||||||
 | 
					        all_consuming(parse_line)(line)
 | 
				
			||||||
 | 
					            .finish()
 | 
				
			||||||
 | 
					            .unwrap_or_else(|err| panic!("{err}"))
 | 
				
			||||||
 | 
					            .1
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let mut tree = Tree::<FsEntry>::new();
 | 
				
			||||||
 | 
					    let root = tree.insert(
 | 
				
			||||||
 | 
					        Node::new(FsEntry {
 | 
				
			||||||
 | 
					            path: "/".into(),
 | 
				
			||||||
 | 
					            size: 0,
 | 
				
			||||||
 | 
					        }),
 | 
				
			||||||
 | 
					        InsertBehavior::AsRoot,
 | 
				
			||||||
 | 
					    )?;
 | 
				
			||||||
 | 
					    let mut curr = root;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for line in lines {
 | 
				
			||||||
 | 
					        match line {
 | 
				
			||||||
 | 
					            Line::Command(cmd) => match cmd {
 | 
				
			||||||
 | 
					                Command::Ls => {}
 | 
				
			||||||
 | 
					                Command::Cd(path) => match path.as_str() {
 | 
				
			||||||
 | 
					                    "/" => {}
 | 
				
			||||||
 | 
					                    ".." => {
 | 
				
			||||||
 | 
					                        curr = tree.get(&curr)?.parent().unwrap().clone();
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                    _ => {
 | 
				
			||||||
 | 
					                        curr = tree.insert(
 | 
				
			||||||
 | 
					                            Node::new(FsEntry {
 | 
				
			||||||
 | 
					                                path: path.clone(),
 | 
				
			||||||
 | 
					                                size: 0,
 | 
				
			||||||
 | 
					                            }),
 | 
				
			||||||
 | 
					                            InsertBehavior::UnderNode(&curr),
 | 
				
			||||||
 | 
					                        )?;
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            Line::Entry(entry) => match entry {
 | 
				
			||||||
 | 
					                Entry::Dir(_) => {}
 | 
				
			||||||
 | 
					                Entry::File(size, name) => {
 | 
				
			||||||
 | 
					                    tree.insert(
 | 
				
			||||||
 | 
					                        Node::new(FsEntry { path: name, size }),
 | 
				
			||||||
 | 
					                        InsertBehavior::UnderNode(&curr),
 | 
				
			||||||
 | 
					                    )?;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    Ok(tree)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn solve_part1(content: &str) -> color_eyre::Result<u64> {
 | 
				
			||||||
 | 
					    let tree = generate_tree(content)?;
 | 
				
			||||||
 | 
					    let sum = tree
 | 
				
			||||||
 | 
					        .traverse_pre_order(tree.root_node_id().unwrap())?
 | 
				
			||||||
 | 
					        .filter(|n| !n.children().is_empty())
 | 
				
			||||||
 | 
					        .map(|n| total_size(&tree, n).unwrap())
 | 
				
			||||||
 | 
					        .filter(|&s| s <= 100_000)
 | 
				
			||||||
 | 
					        .sum::<u64>();
 | 
				
			||||||
 | 
					    Ok(sum)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn solve_part2(content: &str) -> color_eyre::Result<u64> {
 | 
				
			||||||
 | 
					    let total_space = 70000000_u64;
 | 
				
			||||||
 | 
					    let tree = generate_tree(content)?;
 | 
				
			||||||
 | 
					    let used_space = total_size(&tree, tree.get(tree.root_node_id().unwrap())?)?;
 | 
				
			||||||
 | 
					    let free_space = total_space.checked_sub(used_space).unwrap();
 | 
				
			||||||
 | 
					    let needed_free_space = 30000000_u64;
 | 
				
			||||||
 | 
					    let minimum_space_to_free = needed_free_space.checked_sub(free_space).unwrap();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let size_to_remove = tree
 | 
				
			||||||
 | 
					        .traverse_pre_order(tree.root_node_id().unwrap())?
 | 
				
			||||||
 | 
					        .filter(|n| !n.children().is_empty())
 | 
				
			||||||
 | 
					        .map(|n| total_size(&tree, n).unwrap())
 | 
				
			||||||
 | 
					        .filter(|&s| s >= minimum_space_to_free)
 | 
				
			||||||
 | 
					        .min().unwrap();
 | 
				
			||||||
 | 
					    Ok(size_to_remove)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										0
									
								
								src/prob8.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								src/prob8.rs
									
										
									
									
									
										Normal file
									
								
							
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue