diff --git a/README.md b/README.md index c959c63..e7e95be 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,4 @@ Below are a list of the programming languages used to solve each day: - Day 1 - Haskell - Day 2 - Haskell +- Day 3 - Rust diff --git a/day3/Cargo.lock b/day3/Cargo.lock new file mode 100644 index 0000000..4104d70 --- /dev/null +++ b/day3/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day3" +version = "0.1.0" diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..6b20f5c --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/day3/README.md b/day3/README.md new file mode 100644 index 0000000..2b5cb36 --- /dev/null +++ b/day3/README.md @@ -0,0 +1,17 @@ +# Day 3 + +## Instructions + +From the root directory of the crate (where this README is) just execute: + +``` +cargo run +``` + +The solution will be printed to the terminal output. + +## Notes: + +Back to Rust since I didn't have as much time today to mess around with Haskell. +Although Haskell has been slightly interesting, it's nice to be back in more familiar territory. + diff --git a/day3/input.txt b/day3/input.txt new file mode 100644 index 0000000..4c5bfd9 --- /dev/null +++ b/day3/input.txt @@ -0,0 +1,7 @@ +!how()where()*mul(527,340)?why()^?mul(168,100)!^#mul(390,111)>'when()'select())((from()how()mul(412,526)/@what()from()when()-mul(500,980)#&[}]mul(887,503)%,$mul(699,805)what()select())>mul(292,305),'-{,@,how()who()mul(213,633)when()?@who()mul(652,4)*&'what()mul(177,237)&who()why()(@{+;why() &mul(193,679),)&mul(565,339)?}what()!who(){mul(705,313)mul(294,50)+mul(201,534) )~how()mul(659,49)!mul(871,788)select()where()from()mul(346,618) *&@'why()&}#mul(483,765)!#select()^*+[what()when()mul(448,525)]>:where()&^who()&mul(611,331):!)*/]}mul(37,42)>mul(538,106)when()why()mul(36,120)~where()-{+from()$'mul(950,456))mul(28,237)what()when(479,124)mul(697,976)):**/]&#how()#mul(424,841)why()'!;~mul(108,859)~%{?!mul(40,521)do()[^when()/%$+ [where()mul(187,953)who()where()select()(select()what()mul(984,77)<@)(/where()from()~;mul(163,398)/who()who()~mul(445,94))what()what()][how(),mul(115,245)--}/#how()what()!mul(366,981)'$why()do()what(565,96)who()mul(128,999):mul(858,772)[,,mul(796,864)why()what()^(mul(136,980)when()?!when()}mul(142,552)(mul(354,328)[(@how() (/~#mul(159,715)where()&mul(567,560)from()$^'$&&+!when()mul(582,489);where();select(),mul(306,841)? ,)how()mul(274,549)@~what()~?*mul(448,441);&<'select()when()when()#select()from()mul(404,209)^/ @who()who()'mul(400,15)';^&&when():{mul(199,543)[from()~+mul(452,787)([select()>/]$!/mul(840,165)when()>^why()~>when()(mul(830,125)from():mul(433,238);%why(){';$mul(494,781),-mul(139,401)&&&+who()>/^select()]do()mul(116,181)+$*[:who() do()how();mul(514,117)$who()*)~%mul(859,857)}mul(837,844)>{@-what())-@@mul(574,73)]<[[why(608,495)how()who()when()mul(937,176)(%mul(471,325){'+)?:}mul(695,347)mul(56,888)where()##;#>who()+mul(157,650)mul(847,72):,[(how(144,703):mul(294,627)})>@mul(74,230)@)+what()mulhow()><<,$!(;why()mul(635,532)-select()mul(171,737)?select()[why()&^mul(587,686)$how()from())when()>;where()&mul(600,336)}@&{mul(651,690)from()?->&%mul(454,745)mul(387,378)mul(142,934)]mul(820,891)@#why()&how();;why()mul(337,723)&]from()when(894,39) ,>( mul/%(mul(271,558)#}']what()$-<^(mul(234 [what()when()select()($)+!don't()/ %mul(927,106)why()<%{{&~'%why()mul(617?from()select()from()}$mul(915,977)& &mul(477,287){# mul(798,930)]mul(111select()*^@@/what()mul(518,86):('({&~{why()mul(991,855)*%^who()who()$/:{$mul(827,700)-'(mul(377,585)#&mul(171,537)when()@/>{< ,*:mul(349,478)!&^{-/mul(448,750)mul(868,546)mul(893,203) +;?++how()$mul(218,402){mul(52,837)&{what()#$% {>%when()mul(997,771)/select()!mul(88,621)when();mul(120,538)&what(592,500)#:*,how()#mul(745,304)$;:&*mul(276,817)/select()'how()'{when()mul(422,988)mul(386,816)what(87,832)-${mul(43,620)mul(271,338)+;why()why()-#*don't()where():],]what()-mul(4,311)' why()?don't()/~from()~select()<[why()mul(37,622)select()^]select()#where()<{mul(910,54,don't(),#^+{mul(153,137))[mul(348,234)::+{mul(710,502)'}'do()~where(722,741)why()!]mul(810,172)@[{from()where())why(230,249)*^+mul(439,248)(?/#how():mul(559,154)!+from(26,260),)!'mul(67,900){>where()select()#~who()#%what()mul(194,100){<[,'(;-mul(421,329'what()@$$?when()<',mul(410,915)]who(672,22)-:*mul(722,311) select()how()~how()!*'mul(114,108)%($$#what(728,367)do()from(),*mul(159,172)>%]mul(358,684)who()&~]when()$mul(791,993)who()++$why()how()&$mul(913,73)-!mul(53,114))([{ select()@]from()(mul(89,340);^-$>?from() mul(184,541)+-%:(# mul(43,872)? # -[mul(984,77)&<+]when()(#;!mul(22,759)how(){select()mul(264,832)>/$mul(78,759)]mul(645,838)select()<$& mul(665,217)mul(332,96)where()##mul(410,119)mul(614,640)}-%who()/why()what()mul(514,395)!<+{:%#how()+#mul(169,935)/+who()where()select()*mul(298,793)&@how()?!(who(191,903)when()?mul(492,499)) % $^{when(779,320)select()mul(631,153) ';[from()/^-+mul(669,90)what();^/[mul(275,626)why()&&where()mul(647,52)[ /(;[{[mul(522,127)-)mul(477,645)what(998,194)%:{when()[%%mul(32,779)/from()why();mul(665how()'mul(856,294)%how()*$how(372,599)when()<@ don't()mul(46,719)^where()>+: }(when()@mul:from(393,669)!+;how()[%)how()mul(317,228):!who()/mul(998,368); }mul(156,743)@;'^;$<#[mul(317,438)how()[{)#-why()(+?mul(230,66)!?{:@select()}/mul-:when()@$&mul(575,563)!what(){&$}^^%mul(821,423);@@select(815,842))/?>[mul(838,244)where()/[>mul(299,138){%(-mul(476,161)mul(286,551)~;from()%${mul(479,12)%&&;how()mul(867,436)];mul(742,138)select(205,272)where()from(628,468)mul(284,644)@when()<~^mul(254,513)who()@where()-[! mul(458,892)how()'['{mul(875,437)(#^from()mul(431>/}<{#how()mul(453,206) +where()]mul(385,96)^~){what()mul(879,45)/-){mul(115,393)!mul(257,133)&<;'mul(35,453);(mul(213,735)*+don't()~['select()?mul(761,741)]:)&from()?mul(648,940)+what()?mul(297,466);what()!mul(232,28)+/-who()%;where()?)mul(305,615)(*$^who()+&!select()mul(538,332)!@how() <${how(){?mul(357,604~when()what()why()~mul(543,859)when()do() mul(129,114),mul(182,648)#]/how()%$mul(974,606) #@)mul(841,177)how()[,from()-]^/mul(91,282)who()}where()+when()&$;why()mul*when()#mul(480,245):what()from()$~mul(885,960)@*what()]mul(656,115) select()select()how(737,324)~* ?mul(108,370);?$(!mul(428,835)+why()>/)mul(863,986)%&mul(840,769when(),{mul(463,323):?:mul(521,574)/when()(mul(362,622)~+'(-mul(904>$@]>?select()mul(511,354))mul(873,772),[,mul(492,786)^mul(113,58),(who()when()who()&mul(949,323@when()@)~?mul(913,84)when()-}?$+mul(82,683)where()how()->>who()]don't()!{mul(520,514)~+>mul(570,919);from()@!-*+]+where()mul(14,350)mul(577,453))/when()@?{select()%@mul(904,10)%&:>/,mul(632,748)&/#mul(919,899)@>don't()when()?]mul(183*how()select()~what()~+ #mul(301,159)from()(do()#@[who()>{(what(258,705)mul(844,738)mul(412,928)why()&how()}#-}~when()mul(949,165)mul(607,272)~who(931,656)do()&how();mul(594,752){~mul(695,972)<{}#mul(270,571)/,*from(447,32)'&>[mul(924,945),-!mul(539,419what()+<*what(885,804)<$~}~*mul(144,590)what()^what()mul(989,595)who(),from(196,894)when(655,793)'*mul(375,733)how()-when()^*mul(272,881)why()mul(570,456)@@how()why())*mul(357&mul(844,880)mul(881,330)]select()~mul(236,135)'where(){why()+{mul(520,254)(:do()mul(645,835[&~from()why() what(){^select()]mul(892,641)@mul(814,94)& ,*how()who()why()&mul(61,560)@$%'mul(599,707)what(439,650)$-[():$!mul(73when()[#don't()mul(538,792)> #from()why()/mul(460,218)mul(319,652))mul(290,239)%*how()why()(>@where()mul(591,728)@#select()when() )mul(184,663)!]%}}mul(747,180)when()/~why()<,!select()mul(252,522)%*mul(690,254)+'!from()mul?(where()where()&)]when();mul(118,112)*?}where(544,721)#mul(721,220)@mul(190,918)+>how()#!/usr/bin/perl)where()]mul(548,120)where()mul(150,935) +;?!where()'who()$select()why()&mul(55,883){:+mul(220,196)where()-('from()?select()who()mul(504,565))#mul(745,347)<++/mul(635,120)]~mul(791,406)-mul(724,781),when(144,428) [[mul(718,565)who()why() %mul(328,251)who()+]&mul(707,742)!how()]{from()]'--(mul(96,818)@-(-$how()mul(767,105)^-mul(250,160):}where()mul(966,869)mul(878,876)!;),mul(874,72)why()+#who() )why()where()mul(896,845))?'#mul(585,579)$?~from()]!mulwho()%~select(757,508)'&^?mul(244,631)'how()$}mul(995,939)&why()@from())(+(do()^who()what()$mul(166,376)when(866,717)mul(419,53))]%>}{&{%select()do()from()&/*!'why(443,390))mul(397,885)when()how()who()what()mul(99,988)?,what()&mul(836,347)mul(692 mul(724,602)(mul(250,6)*how()how()'!how()mul(639,245)]mul(81,785)&+~mul(101,124)mul(984,435)what()[,}*mul(347,607)select();&where()mul(80,544){#~&mul(934,254):#+!who()<[mul(357,513)?:>what()~?when()({mul(937,208)]>?who()'when()select()'how()'mul(608,880)do();%:>select();]what();mul(978,885)!+select(195,899)~@*+mul(668when()'how(){>!#~}mul(449,440) !:]mul(908,330)mul(425,622)why()>[where()^**;,mul(373,419)from()%&'how()^,,^who()don't()@?&%select(491,38)mul(162+]*!:#^/ *+mul(390,994)select()#mul(244,23)@from()';where()mul (}when() )mul(16,801)/~^when()+)&-;when()mul(987,488)~@)mul(132,925)mul(959,924)@select(351,211)how()(-^}[mul(398,876)>,mul(492,208)how()what()?{who()where()'+mul(407,996)-)?where():mul(911,656);}/when()&mul(683,608)/why(574,363)mul(771,125)how()who()-~-#*?{&mul(832,987)mul(443*}+$mul(609,538)mul(949,408)how()mul(322,393)!$/&where()'mul(599,249)}#@ mul(858,751)why()+#what()>/-:how()/how()?don't()when()when(){~/]mul(253,448)^^))mul(84>mul(85,415) ]})+why(40,365)}how()mul(11,381)how()%%who()where()why()mul(906,336)how()~where()-who():]where()select(239,576)why()mul(912,508)mul(24 ~^from(718,313)-@mul(517,16)@%mul(55,215) !what();*^/@mul(880,672)where(522,916)mul(804who()what()[(where(286,361)@}^$:mul(51,378)({${,[from()}mul(543,793)}(}:[select()]mul(772,432)mul(253,865)(from()::?mul(150,306)+< when()]when(78,385)]what()mul(681,911)}>what()mul(385,561)mul(97,729)$@mul(293,50):-@-mul(471,695)}where()-:^:}mul(566,792)&++[how()#mul(287,219)why()!mul(373,971)when()how()mul(154,605) ^mul(93,56)!@)@from()who(416,376)%$'[mul(472,481)^')select()when() {&mul(303,563)[;;@}~mul(167,624)mul(550,339{mul(626,174)'$;when()-[mul(11,151)how()select()&-select())when()mul(951,26)mul(988[[/select()what(195,189)-mul(991,661)([}from()~:[why()^!mul(558,565)select()+*}mul(525,81) +?select()mul(457,497)from()who()[how()who()select(){:(>mul(273,631):)' )mul(433,659)/select()$ ??mul(996,214);'&>:(#!don't();from()mul(806,472)$;why() what()^,?don't()+-#what(220,900)]-from()mul(672,653)where()why()select(){why()*mul(394,630)mul(214,535)}(from()'from()!mul(304,752)who()'{from()from()%;@from(471,425)from()mul(431,414)?from(536,612)*don't()[mul(494,757),{@mul(864,136)why()%$ mul(901,770)(?how()! !mul(389,250)>:mul(107,240)how()+how(),mul(402,117)^:#,why(432,943)~mul(764,768)%/) mul(486,61)[from()% ~^}how(){mul(436,252))'mul(314,683)what()$@&)mul^what()(how()mul(356,528)+?] %)#mul(945,995);when(336,103)mul(904,121)'),mulwhy()why();why()@<*%+mul(173,177)mul(552,612);,select()%@mul(824,863)@from()mul(151,472)select()when()^}mul(381,33)mul(987,66)mul)+from()]]'mul(484,944),} mul(79,219)mul(645,343)+#select()#what()#>#mul(962,402)^,mul(170,849)+$/[],{ how()*mul!select()#how()do() ~[!{mul(132,291)from()when()^{mul(876,121);@&>?mul(14,405)select()}'why()^!mul(701,908)^]+what()how()%when(),{mul(163,570)why(),where(802,334)from()^mul(203,784)^select()who()^*@when()~/)mul(457,821)how()*!how()%#>[mul(911,205)*}];/]*!select()mul(209,380)why():*@*where()what()]who()mul(158,681)select()from()mul(913,623)select()?,>mul(692,963)>}mul(641,525)why()mul(107,645)from()(+mul(948,423)mul(497,166)from()%'mul(972,376)when()where()[[don't()~}/what()$*what()mul(205,460),-%:-mul(212,248)when()select()mul(511,961)who()who(694,88)>mul(725,998(<]mul(881,682)select()select()mul(392,740)$>)mul(794,79),;$mul/:};#(>/)mul(60,633)['!~how())who(){$mul(757,172)%how()what()how(),mul(19,116)-'?who();where()when()who():mul(201!]how()-'{#where()<+what()mul(848,314)->from()mul(366,508;^[%why(10,776){~mul(403,696)(~{))why()/mul(61,507),+ }mul(771,565)-}@)mul(726,28)*;when()-$!:,[&mul(672,226)}?]*do()when()[mul(749,250)(mul(514,431)~*when()%what()'how(204,98)how()where()mul(529[]}~mul(204,564)what()++<]}&select()mul(459,244)what()mul(687,827)?when()<~,]%$+<<*mul(403,7)how()what()^mul(433,321)}%do()where(436,974)^/,+what()who() mul(338,162):(mul(876,575)%why()!+;/^why()mul(588,29)(how()select(350,689)+'^-:-&mul(694,2)what()mul(997,566)from()!]%'?;<-why()mul(540,192)^from()*<):~do()what()from()/{/mul(236,520)#>when()*#)(mul(243,137)mul(483,202)('who()do()when(){>$$what()&)']mul(763,83)>who(744,412)*^who()*) don't()mul(948,448)(:mul(867,155)^^***,+}-^mul(510,656),,;:[^[!mul(37,392where()[mul(67,386)'(^!,~mul+~(+ how(321,247)}$*mul(632,944) +{how()mul,how()when()+when()}what()mul(900,270)^:,where()mul(489,842){!what()when()#!mul(243,646)!where()*when();who()mul(67,231)select()*select()do()how()$(**,+how();mul(472,391)select()@'how(630,875)who()>mul(83,172);+$where()+~~mul(163,367)mul(10,776) ({select()select()~*[mul(216,443)-)select()::(-?@select()mul(938,86)mul(238,475)where() }<(don't() +>;mul(491,189)~[/#;mul(687,66)$+]'-don't()+when()&+$;-]mul(605,671)?;mul(8,465)when()<~:^(what(549,164)[mul(253,71)([?where()from()why()?when()$mul(885,722)~~>what()select()]@%>mul(12,734)(/)[where()select(649,213)from(945,849)(*%mul(948/when()mul(490,837)(@where()'what()}what()why()mul(906,59)when()how()#select(),:}do():#mulwhat(918,618);mul(290,290)mul(894,51)?mul(230,518)*,;select()mul(366,247)[]from(946,647)&!^where()where()how(),mul(94,114)mul(275,889)when(){$#[mul(892,356)&[-mul(303,485)*select()how()''mul(738,220)!@mul(371,794)where()mul(380,261)mul(210,796): *{$]why()how()?mul(615,74)select()from()do()from()*how()/'mul(507,36)what()when()#]?#{-mul(398,229)select()}mul(844,299)!<;#^%]+,when()mul(4,527)#when()##!%]when()(mul(708,803)}/when()why(514,182)({mul(336,134);,[}/:mul(899,874)select()how()]#mul(548,253)>^~mul(294,767)#^* mul(444,302)where()how()]{]select()!where()!mul(906,663)mul(656,596){[ & ;^+%-mul(108,609)who()+(why()/how()(@don't()'{-from() mul(364,848),^$ ?] +:mul(889,175)mul(660,583)*!@mul(423,213)mul(311,390)%how()?!{mul(738,520)mul(97,84)?mul(423,763)<'?mul(15,141)mul(300,197)>select()%when()mul(640,732)^}mul(919,588)(}how(),-]how(),select()!select()what()from()mul(231,907)where(799,691)-,:)@]mul(336,972)) -> std::fmt::Result { + match self { + ParsingError::EmptyInstructions => write!(f, "No instructions present to process"), + ParsingError::ParserFailure => write!(f, "Failed to parse segment"), + } + } +} + +impl Error for ParsingError {} + +/// Main execution function to analyze memory for uncorrupted mul commands +pub fn analyze_memory(file_path: &str) -> Result<(), Box> { + let instructions = read_to_string(file_path)?; + let part1_sol = process_mul_instructions(&instructions)?; + println!("Solution to part1 is \n{part1_sol}"); + + let part2_sol = process_do_dont_instructions(&instructions)?; + println!("Solution to part2 is \n{part2_sol}"); + + Ok(()) +} + +///Process mul instructions +pub fn process_mul_instructions(instructions: &str) -> Result> { + let segments = segment_instructions(instructions, 'm')?; + Ok(segments + .iter() + .map(|segment| calculate_segment_value(segment).unwrap_or(0)) + .sum::()) +} +/// Execution code for solving part 2 of the problem +pub fn process_do_dont_instructions(instructions: &str) -> Result> { + let segments = segment_instructions(instructions, 'd')?; + let mut last_was_do = true; + let mut sum = 0; + for segment in segments.iter() { + if segment.starts_with("don't()") { + last_was_do = false; + } else if segment.starts_with("do()") { + last_was_do = true; + sum += process_mul_instructions(segment).unwrap_or(0); + } else if last_was_do { + sum += process_mul_instructions(segment).unwrap_or(0); + } + } + Ok(sum) +} + +/// Break up the total memory into separated strings for further processing +pub fn segment_instructions( + instructions: &str, + filter_char: char, +) -> Result, Box> { + let char_indices: Vec = instructions + .chars() + .enumerate() + .filter_map(|(i, char)| if char == filter_char { Some(i) } else { None }) + .collect(); + + // Include the first chunk by default + let mut chunks: Vec = [0] + .iter() + .chain(char_indices.iter()) + .zip(char_indices.iter()) + .map(|(idx1, idx2)| { + String::from_utf8_lossy(&instructions.as_bytes()[*idx1..*idx2]).to_string() + }) + .collect(); + + chunks.push( + String::from_utf8_lossy( + &instructions.as_bytes() + [*char_indices.last().ok_or(ParsingError::EmptyInstructions)?..], + ) + .to_string(), + ); + Ok(chunks) +} + +/// Analyze each segment to see if it's valid, if it is valid then return the value of the +/// multiplication. If it isn't valid we return an error. +/// We could use Regex here but I wanted to try a more manual approach to keep with the spirit of +/// minimizing external libraries +pub fn calculate_segment_value(segment: &str) -> Result> { + // Verify string starts correctly + if !segment.starts_with("mul(") { + Err(Box::new(ParsingError::ParserFailure)) + } else { + let mut segment = segment.to_owned(); + segment.drain(..4); + let comma_split: Vec<&str> = segment.split(",").collect(); + let parens_split: Vec<&str> = comma_split + .get(1) + .ok_or(ParsingError::ParserFailure)? + .split(")") + .collect(); + + if parens_split.len() < 2 { + return Err(Box::new(ParsingError::ParserFailure)); + }; + + if let (Ok(left), Ok(right)) = ( + comma_split[0].parse::(), + parens_split[0].parse::(), + ) { + Ok(left * right) + } else { + Err(Box::new(ParsingError::ParserFailure)) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + pub fn test_segmentation() { + let test_string = "lkjqermulemtymart"; + let segments = segment_instructions(test_string, 'm').unwrap(); + assert_eq!(segments[0], "lkjqer"); + assert_eq!(segments[1], "mule"); + assert_eq!(segments[2], "mty"); + assert_eq!(segments[3], "mart"); + } + + #[test] + pub fn test_segment_value() { + assert_eq!( + calculate_segment_value("mul(10,14)aeqejrqlkd9()").unwrap(), + 140 + ); + assert_eq!( + calculate_segment_value("mul(1a0,14)aeqejrqlkd9()").unwrap_or(0), + 0 + ); + assert_eq!( + calculate_segment_value("mul(10,14aeqejrqlkd9()").unwrap_or(0), + 0 + ); + + assert_eq!( + calculate_segment_value("mul(910,54,don't(),#^+{").unwrap_or(0), + 0 + ); + } + + #[test] + pub fn test_do_dont() { + let instructions = + "mul(1,2)don't()mul(2,3)ekjkldomul(2,3)do()mul(211,adf2),))),)do()mul(211,12)"; + assert_eq!(process_do_dont_instructions(instructions).unwrap(), 2534); + } +} diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..24a82db --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,7 @@ +use day3::analyze_memory; + +fn main() { + if let Err(err) = analyze_memory("input.txt") { + println!("Failed to analyze memory because of {err}"); + } +}