From 8f62314da3d8890cb5d368edd9efda7363bd9edc Mon Sep 17 00:00:00 2001 From: deadvey Date: Thu, 12 Dec 2024 22:38:10 +0000 Subject: [PATCH] day 3 in rust --- 2024/day-3-rust/Cargo.lock | 54 +++++++++++++++++++++++++++++ 2024/day-3-rust/Cargo.toml | 9 +++++ 2024/day-3-rust/medium.txt | 7 ++++ 2024/day-3-rust/small.txt | 1 + 2024/day-3-rust/src/main.rs | 68 +++++++++++++++++++++++++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 2024/day-3-rust/Cargo.lock create mode 100644 2024/day-3-rust/Cargo.toml create mode 100644 2024/day-3-rust/medium.txt create mode 100644 2024/day-3-rust/small.txt create mode 100644 2024/day-3-rust/src/main.rs diff --git a/2024/day-3-rust/Cargo.lock b/2024/day-3-rust/Cargo.lock new file mode 100644 index 0000000..197c4c9 --- /dev/null +++ b/2024/day-3-rust/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "day-3" +version = "0.1.0" +dependencies = [ + "regex", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "regex" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" diff --git a/2024/day-3-rust/Cargo.toml b/2024/day-3-rust/Cargo.toml new file mode 100644 index 0000000..48176f7 --- /dev/null +++ b/2024/day-3-rust/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day-3" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1" diff --git a/2024/day-3-rust/medium.txt b/2024/day-3-rust/medium.txt new file mode 100644 index 0000000..b8ffb93 --- /dev/null +++ b/2024/day-3-rust/medium.txt @@ -0,0 +1,7 @@ +(who()where()''~[how()'&do()why()$;mul(323,598)&/-'}{&-/[?-mul(933,97)how()?from();}{+mul(864,562):#<*$>mul(63,747)what()mul(514,101){]$where())~>do(){:mul(53,731)mul(899,858)~~[select()(~mul(402,353)?^&!,who()what()-when()mul(4,41)-&mul(505,942)how()*/%select(667,826);mul(233,284)(&mul(484,956) #/mul(243,698)[;')how()'<%+[mul(153,970)!when()^{^;mul(176,383)@$$~[select(901,794)mul(322,492)from(183,121),-mul(212,356)who();)where()select()#do()>!who()!mul(138,847)&select()mul(128,454)select()what()(&<-mul(650,981) #when(636,522)(who()'-{?mul(149,431);/ !$}<#@mul(15,553)[[>;%mul(389,307)'mul(864,97)#[$why(),<>mul(322,599) ^mul(109,985)who()when()mul(763,870){from(905,178);$/mul(980,975)$why()@don't()#why()#from()from()when(){mul(436,3)[^]/*what()!!mul(300,322)where()!select()*(~why()mul(845,313),[:mul(937,973)how()^when()mul(552,183);from()/ &from()>'!mul(773,549)/where()~from(277,235)'mul(43,275)#>#[?-mul(110,899)~where()when())mul(976,743)%& mul(373,511)what(){{$)?}!mul(565,194)from()%#mul(47,349)(select()%when()&mul(263,392)-#,{+ *(mul(382,243)@:?+&-#%when()from()don't()where()what()(+from()- }where()mul(904,460)@how(768,761)}-why():^mul(450,724)~mul(570,970)+-+/when():mul(203,363)why()[%%@mul(803,322)how()#who(),mul(368,46)select()what()#^*>from()from()+do()^(:)]+mul(585,478)^*:[mul(4,668);&select()'(mul(931,511);:mul(356,478)[mul(933,523)}';when()mul(616,605)why()$]{{select()how()~mul(869,274)mul(15,936)how()/:}mul(297,842)#}%~,select()$@(!mul(961,553)when()why(40,621)>mul(35,851),?+*mul(574,127)when()select()}who()from()/from()mul(107,604)<~@)mul(911,877who()?(when()!mul(496,778)select()when()select()&!$:,}+mul(278,194)%))who()when()*why()#+mul;when()??what()select() -%how()what()mul(873,684)where();where(741,393)+}?!'}mul(684,81)what()what()how()who()from()/<{>mul(286,481),<^mul(788,53)mul(561;)mul(376,887)+]-(when()[who();mul(205,369)!how(425,691)do(){}when() {mul(260,802)],*where()^why()how(){don't()where()}#?*), mul(670,405)^how()where()]mul(645,220)who();< > +select()how()+mul(763,932)+mul(141,632)+'}how(),@#from()'from()mul(211,161)mul(272,971)[@,>mul(340,784)-#why()&:@mul(343,209)when()#/]&mul(408,205)#!,,@;{>}@don't()mul(682,891)^:(>mul(234@when()} don't()) mul(322,500)~>when()*mul(826,896)$!^+->>what()don't()~&select()mul(127,828):,!^!%where(168,484)<~mul(709,801))@*{mul(148,549);from(649,310)+~what():why():?mul(62,198)mul(133,316)select()mul(580,623)who()who();<;[who(388,191)!{mul(819,559)/[mul(572,562)))@,mul(148,103)who(),select()(mul(669,636)-{mul(493,60)))&#^mul(766,322]*mul(290,960)mul(962,649)mul(209,105) +mul(759,17why()&'!](%-mul(389,889),select(){/mul(907,972)why()&(#what()select()+!when()mul(379,398)'-{?mul(275,735)>why()(]%)select()+mul(811,330),%who()#mul(27,53)mul(810,378))how()[don't()^$'*mul(714,951)[>where()}:}%)mul(741,834~][*% where()}&when()mul(84?mul(204,282)*,]+@^+%what()what()mul(442,73from()@select()}?when()~select()do()from()-}:where(){$* {mul(436,174)where(209,751)mul(576,189)[^when()what()why()don't()@when()%<$?mul(49,842)>who()from()-:$mul(309,375)-who() <+#+/when(225,777)mul(298,362)^~how()what()]&^;&;^mul(573,219)[:/mul(714,428)}%mul(534who()@}}]:~'how()('mul(985,19)>( do()%what()&mul(17,24)when()),>from()do()@%!mul(967,50)why()}from()~who()/;!:mul(330,386)>why();]*<%[{from(285,989)mul(746,507),)where(131,831)'>select()!'who()mul(936,547)mul(698,306),mul(810,111)'[[when()mul(859,814when()(,^:$when()*^@*mul(639,6)()%mul(530,398)from()?'+]what()%mul(565~mul(664,297)][{where()$<'mul(521,918)<~'']?do();how()?[;,! who()^mul(993,640)-$;how():from()mul(555,779)where():?mul(802,307)/}&;@where(),],$mul(428,846)mul(914,333))%$?from()mul(132,678)*when(843,580)mul(360,826)*/when()% what();'mul(726,716)+select()'#~},when(587,50)select(871,597){mul(204,647)(from():{*-]$where()>mul(347,78)'@why()why(990,935)mul(708,962)(:^#what(),)+mul(946,615))>>$how()do()<<&mul(27,707){)&what()mul(734,74)+>'mul(889,356)+{why()why()&why()mul(875,327)[ $who();]#&!@mul(887,395)how()/mul(445,826)mul(325,137)why()$#&[+:mul(61,520)~when()]/from(682,457)^;&~,mul(909,277)mul(155,72)what()@<([?#do()&}why():who()mul(636,58)where())[@who(487,170)?mul(195,909)how()#;where()why()-)&[/mul(682,842)!(+}+^<<;mul(303,732)mul(709,34)@' (when()~mul(32,411)@don't()what()select()mul(765,345)mul(124,69)mul(68,95)/mul(967,749)do()who()where()who(){:why(430,451)(*when()where()mul(719,917)**)#mul(77,946),when()mul(273,971)#[what():+>why()]mul(583,407)<%from()mul(360,886)who())where(465,475)mul(398,130)#when()where()?where(),#~;mul(647,114*]who()-'*%!from()do()~';?(?$?mul(750,896)'^!how()+'!:/mul(95,159)who()>from(){;how()<&,!mul(623,492)+?::from()who(){mul(915,36)#what()mul(855,275)mul(546,43)%%!&from()mul(256,937)^%how()where()+#mul(720,847)who()>mul(530,714))]~what()#when()who()where(353,953)mul(948,731)} ?]~&don't())where()when()where()who():%,?-mul(348,179)why()where()^'/mul(858,210)^$what()!+mul(169,479)};from(106,826)/who()mul(44,593how(382,624)>)-'+#mul(822,120)when(509,411)]why()@from()@~/$do()mul(135,70)?select(){don't()]~mul(339,843)mul(89,314)*[-from()do()){>%^#mul(961,883) +#-}*mul(204,272)^@':%>]'/mul(399,973)from()!,>>mul(183,122):?#<({+ )mul(489,296^/where()$how(949,670)where()^}/mul(509,262)&!why()when()mul(42,399)]@how();(mulselect()why()~[*!mul(530,643),#$mul(798,926)select()%/:]'from() ?select()mul(777,171)@how()'when(849,184)do()'-'''mul(609,27)how()^&mul(663,547)}where(891,558)mul;*select(){~$!mul(679,190);<~mul(95,93)where()>mul(347,816)}'[from()+mul(69,7)@,-;^what()mul(679,759)mul(985,484)mul(374,224)+^<}mul(356,351)*(?why()'how()why()when()]>mulwhen() select()!-who() mul(386,988)#%]{ {~mul(403,987)select()from()where()[+/mul(534,4)'from()who()mul(845,463)^~^who())]!:&from()mul(396,175)mul(797,149),mul(373,499)~/mul(908,215)when()who()$-what()where() ![from(874,415)mul(735,220)%from()?;*select()who():'mul(958,295)?from(){[how()}mul(577,440*]mul(248,108)/{mul(845,24) what(708,623)&,mul(962,333):-{mul(228,773) @;'mul(65,412){],]}where()mul(695from()}:mul(802,950)?}&mul(903,520)]select()}@mul(819,46)')who(290,48)}]!+}mul(175,633)from()[-+/select()]#from()mul(502,869)who()who(){who()[$*$mul(625,448)where()&!where() &mul(640,501),&when(322,255){who()mul(393,184)%*(#from()where()mul(855,937)@;(+mul(658,584)+what():'$$mul(510,392)what();%mul(972,965)][*mul(199,39)mul(258,651)#where()when()$mul(629,994)]+&[where(747,799))mul(927,670)$# !{*who()mul(925,133);don't()*mul(430,770))}when()!;mul(691,631)+mul(78,556)]~+{where())mul(119,527)*{>where()mul(108,678)'select()!:$*>mul(178,527)do()&^mul(958,626)-&$[}?mul(51,760)mul(24,628)!mul(193@^{who():^';?'mul(494,429)&who()@;[(from()~mul(556,60)/^[{mul(448,594)+from()/why()mul(133,200)^when()~select()*,how()mul(874,680)'from()>~when()when()>mul(993,110,%!{*mul(964,213)[mul(740,725)*}! -'mul(681,471)$don't()who())< ?*-^^+*#'%mul(354,482)//;select()why()^select()?how()%mul(717,506)from()from()where()mul(729,432)],> ]@mul(817,878)(mul(25,684),select()?mul(9,290)select()~//from()mul(597,993)>:%]}who()<]&[mul(463,497)'(}don't()what()@&+!select(323,684)%how()mul(530when()'why()mul(595,825)&?why()mul(419,41)how()don't()-(mul(682,595)%,;{&:^%-what()don't()^ *@{how()+what()mul(69,125)*?!?'!why(),]what()where()<]mul(38,337) (mul(920,421)who() what()&*]{mul(228,619)where()#?'%^>who()mul(500,245) +{!;';'~)$^#!/usr/bin/perl<@what()&where()-[!]mul(215,331):!^~!&mul(740,257)how(283,279)#mul(86,887)>how(){[([mul(229,645)/+:don't()'(select()how()-{>$mul(319,757)what()^mul(438,563)$where(){select()mul(176,746)] ]%mul(291,179)mul(720,898)}{~;what()]do()[when()/@,):select()!mul(356,326)>?'mul(655,327);,#why()':[ mul(782,645)[:)what()??%mul(328,999)@^ (@>^%from()mul(765,647(,'/mul(523,130)where()why()%where()~why();where()who()mul(536,374)from()mul(240,454)!]how(122,192)from()#)how()/why(572,858)!don't()[mul(626,865)*!$,from()},-mul;:what()mul(926,728) '>}who()^%mul(971,826){(>mul(414,666)}&?&<--<-/mul(869,9)who(706,345)when(211,297)[what()>'-(){how()?]{select()mul(514,69)mul(926,807)when()who()+[don't(),}$where()%##(mul(628,758))from()?mul(269,430)mul(489,967)when()mul(171,814)+ &when()mulfrom()#]((^{mul(104,614)how()&why()^+who()why())(&mul(843,643)>)(;[when(888,120)how(463,116)mul(519,821)<,(why()$when()who(623,832)mul(26,559)]:why()?/]-;{?do() why()-mul(759,848)*who()why()}*where()mul(358,256)who(250,854)don't();#?-~what()how()mul(188,539)what()why()(:how()how()when()(~,mul(10,118):!where()select(){-mul(851,170)+why()[why()-$(from(287,301)mul(53,873)+(&];)![mul(654,539)~how()why()why()who()&>mul(802,188)#}mul(286,24)#-~+~]who()'mul(281,726)why()#{![+when()mul(105,845)+@select()mul(932,93)-:;/';, mul(820,402)select();where()mul(282,739)%what()who()!{%why();:who()mul(37,507)[) &$who()mul(398,244)where()]]<^,*why()from()mul(703,450)?$ mul(568,998)[/mul(241,361)[*>,}%do()what()how()mul(539,814))+^what()@what()!mul(484,815)'select(875,43)don't();{]*]^;who()mul(798,636):~>mul(398,278&mul(468,474)(}@;~mul&%[who()@mul(118,538)!$@who()how()*what()what(),why()^*+why()mul(156,58)[^from():)#<?<{mul(47,431) ,>'mul(131,665)who()how()what(){@mul(888,835)who(593,10)'%how()select(357,595)mul(793,894)+select()(mul(289,300)mul(668,408)}+[,mul(404,186):+~}^mul(580,609)who()(:((+mul(664,287){from()<{mul(627,661) select()mul(961,352))why()from()how()mul(652,317)$mul(423[%where()(![[]]/@mul(613,935)%:[ &?where()why()[:{mul(473,246):[what()^mul(673,250)mul(674,621), ,} ;>when(){-mul(934,227)where())#where()~mul(535!~!how()]how()^mul(16,678)-%?why()what()*-do()}#mul(407,518)?<>'@)&,mul(935,102)+select()@>{>>>~)mul(810,478)mul(13,154)>-]from()@when()mul(359,474)who()-who(311,794)mul(595,571)mul(10,255)what()+${select()who()%'!mul(767,38)[mulhow()~{mul(101,378)')where()}!from()!%mul(81,12) what()+what()::select()mul(851,679):#}~&>who()when()/'mul(341,606)when(){)^who()from()select()mul(462when()]^{what()%{!why()don't()why()^@$mul(480,740)mul(327,864)$why()@&,*how()!{mul(359,622)%#+~*where())##%mul(957,284)[]who()}/,)mul(646,738)mul(588'&~&what()#!~mul(901,808)mul(751,892){>*;,why()how()mul(588,670)when() $from()${!#don't()(select()^&where()mul(430,59)@~why()mul(880,187)how()&!'what()where()how()mul(496,275)+who(){/$!mul(571,229<*!*>mul(843,816)who();what()<#@->'&mul(578,292)/ why()mul(585,157)select()}@>/(when()mul!^who()mul(660,711)><}why()when()mul(392,7)what()$who()how(156,813)'mul(512,78)select()from()*,;mul(139,554)why()!;}&}$from()mul(534,318)(##:]*)what()how()mul(981,987)#select()when(914,450)!:/'mul(664from()^from()?'-mul(287,512)>;?~how()> #mul(763,19)}:mul(875,396)/(-from() !:why()mul(830,397#mul(143,615)mul(565,22)~what()+mul(443,374)how()mul(396,99))who()do()mul(613,198)*,who()]?{(#mul(681,692)(@:[<#;]mul(353,806)mul(416,977)&from();/don't()why(),,&mul(844,896)$how()%:#:(+>who()mul(929,260) %+'when(),~*+mul(549,134)why()[why())!;)when()(from()mul(306,154)'where()what()mul(781,501)['when()why())where()&[select()-mul(256,37)mul(764,192);mul(204,633)mul(980,29)]$mul(85,142why(88,3)~mul(297,384)>mul(445,165):}who(){(: mul(790,74)$?)where(361,397)^what()$$mul(932,137)mul(283,441)$$where()&^~mul(419,42)/$$mul(95,704)what()from()^mul(686,803):from()?what()$mul(657,560)~mul(394,381)why();-+&}where()<}!mul(320,912))+# &,#mul(22,374)^[';!^mul(295,509)who():from()-*mul(844,745)*}$when()where();&^mul(157,945)~how()^/?(mul(98,104)mul(883,360)}from()where(456,72)$???who()[]mul(869,340)from()@/$({@who()'mul(30,429)what();':,:,'select()mul(444,351),mul(568,344))]where()[@)'*!-mul(268,329)who()don't()]select()(select() what()&{'mul(190,17)*mul(795,718)who()^@+;+mul(748,907)>]who());}&mul(645,937){,/how(47,395) mul(826,39)how()}where(791,937)<^{@)mul(286,200)}who()how()&from()'what()$select()!mul(615,866)) why()(/why(901,882)[:mul(815,919)why()mul(713,155) + mul(267,443)&select()#/$ )mulwhy(834,619)mul(468,369):mul(360when(248,679)*when()]don't()(what()<+^{mul(84,136)!mul(865,460)mul(63,689)}who()/!**%/mul#?(,,!when()mul(321,194),?/$who()why(264,50)*-mul(424,308)(/[%~$who()!*?mul(870,366)-;+*^*mul(629,804from(485,259)what(691,680)why() {what())mul(182,814)who()who()mul(45,447)*,from()(+&?$select()*mul(592,101)why()};mul(716] how()when()&%select(393,368)when()*+^mul(360,598),*%~]mul(483,209)}*!@mul(124,485)why()'select()!from()$];mul(827,797)!who()&mul(750,119)mul(266,461)<#+(?{[don't()}what()where()+mul(668,848(*!how()%how()>mul(537,81)-@don't()why()+mul(290,311)'>/! mul(886,782)}>]~);>?mul(800,965){mul(124,560)[mul(54,352)what()!when()what()'-$] mul(485,906)mul(532,777)*mul(899,963)!^> +where(733,202)where())mul(42,658)don't()>what() who()mul(672,366)%-~*who()select()why(654,545)why()mul(393,810)[:[>?mul(581,64)where(){,&'@what()(?;mul(783,54)who()#:mul(554,807)+%&{[^ )^mul(239,651)select(145,543)(>don't()select() $*?who()who()&/]mul(228,402)#*[?when(628,409)what()!where():mul(595,910);,!-&mul(801,570)who()(when()@select()+~mul(357,370),$select()$,$select()!from(703,372)mul(408,165)mul(929,252)}how()#*what()/mul(115,155)what()mul(64,981)what()mul(162,300)# where()do()*from()'}%),how(309,961)$mul(332,232)/ 'from()>@};$]don't()(?:?*what()^#+mul(51,30)mul(79,533)how()select()}?mul(330,382)from(287,801)>/how()~mul(326,971)mul(623,476)+ ',select(632,537)}why(663,480)mul(987,320)[what()mul(626,139)mul(71,973)?)from()*{what() mul(999,806)-from()^{mul(153,622)#do()>why()where(10,408),}{#>%mul(995,509)(mul(728,572)#~(;mul(530,132)*:% select()]mul(499,982)><@when()mul(348,550)mul(362,709))$}mul(520,895) where()-mul(950,23)$%%when())>','mul(328,120)+#mul(934,621){from(63,306) :}mul(402,983)where()*select();*select() io::Result<()> { + let pattern = r"^mul\(\d{1,3},\d{1,3}\)$"; + let file_contents: String = fs::read_to_string("medium.txt")?; + let length_of_string = file_contents.len(); + let re = Regex::new(pattern).unwrap(); + let mut total: u32 = 0; + let mut total2: u32 = 0; + let mut enabled: bool = true; + + for i in 0..length_of_string { + let substring = &file_contents[i..i+1]; + if substring == "d" { + //println!("{},{}",&file_contents[i..i+3],&file_contents[i..i+6]); + if &file_contents[i..i+4] == "do()" { + enabled = true; + } + else if &file_contents[i..i+7] == "don't()" { + enabled = false; + } + } + else if substring == "m" { + let mut closed_bracket_position = 0; + for j in i+3..length_of_string { + let next_closed_bracket = &file_contents[j..j+1]; + if next_closed_bracket == ")" { + closed_bracket_position = j; + break; + } + // check it's reached the end of string + } + let string_to_check = &file_contents[i..closed_bracket_position+1]; + //println!("String to check is {}",&file_contents[i..closed_bracket_position+1]); + if re.is_match(string_to_check) { + //println!("{} is VALID!!!", string_to_check); + let mut comma_position = 5; + for j in 5..string_to_check.len() { + if &string_to_check[j..j+1] == "," { + comma_position = j; + } + } + let number1: &Result = &string_to_check[4..comma_position].parse(); + let number2: &Result = &string_to_check[comma_position+1..string_to_check.len()-1].parse(); + match (number1, number2) { + (Ok(n1), Ok(n2)) => { + let result = n1 * n2; + //println!("Multiplication result: {}", result); + total = total + result; + if enabled == true { + total2 = total2 + result; + } + }, + _ => { + //println!("One or both of the values are invalid."); + } + } + } + } + } + println!("Part 1 total is: {}", total); + println!("Part 2 total is: {}", total2); + Ok(()) +} -- 2.48.1