From 993c9af394a24dc03bead86f00eb772f49326a65 Mon Sep 17 00:00:00 2001 From: Bram Date: Tue, 3 Dec 2024 11:39:42 +0000 Subject: [PATCH] Add Rust day 3 --- Cargo.toml | 1 + inputs/03.txt | 6 ++++++ src/day_03/mod.rs | 2 ++ src/day_03/part_1.rs | 12 ++++++++++++ src/day_03/part_2.rs | 30 ++++++++++++++++++++++++++++++ src/lib.rs | 7 +++++++ src/main.rs | 1 + 7 files changed, 59 insertions(+) create mode 100644 inputs/03.txt create mode 100644 src/day_03/mod.rs create mode 100644 src/day_03/part_1.rs create mode 100644 src/day_03/part_2.rs diff --git a/Cargo.toml b/Cargo.toml index d7b7677..54fddd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] +regex="1.11.1" diff --git a/inputs/03.txt b/inputs/03.txt new file mode 100644 index 0000000..3dd292e --- /dev/null +++ b/inputs/03.txt @@ -0,0 +1,6 @@ +select(){,(where()+-mul(514,727);:]]what():^*from(764,547) mul(550,305)$^^%>select(587,376)mul(94,564)select()when(633,175)[[mul(302,490)$-}&**+mul(335,535)@select()>select()mul(241,166)when(800,629)mul(758,530)',^ &!~%/#mul(938,70)?&!from();mul(412,287)}/mul(230,620)(~select()what()select()'@mul(410,577)$mul(720,516)-mul(373,78)when()!^:@{)mul(793,973)who()when()mul(85,246)>what()}[#[@!^/mul(396,22)@when()-;:':>}mul(515,670)<[mul(38,415)?~select()why()^when()*#+($do()']select()>:^/when()^mul(440,923)@mul(48,816)mul(370,665)@*>who()^,when()mul(155,426) mul(132,914)^%select()&when()/from()(+}mul(296,176)mul(361,479)%{;!,'who()where():mul(506,495)#who()&%< ^mul(481,87))&!;mul(541,563)%don't(){}'-),from()$mul(495,427)^when()mul(640,499)select()}:] )%+()mul(967,918)?when()select()<{/who()what()mul(505,225)(mul(90,482)>!from()when()#do()mul(620,841):who()!mul(719,850)where()-)mul(931,185)?;select()]@,why()%]mul(39,103)mul(687,103)$%];how(337,314)[}$-when()mul(282,494):;!where(188,779);who()mul(342,554)<^who()(why()where()who(345,491)%mul(407,74)why()&}'%'mul(324,781)mul(90,925):from()mul(828,16)mul(438,549){what()>$why()*what(35,607)mulwho()mul(91,548)>,what()select()]&how()@mul(384,93!select(),where()$mul(835,662)@{don't():#<^/-+mul(489,462)>>where(){why()where()---when()mul(53,461)/~?mul(273,777)mul(119,879)+@'when()-mul(585,949)?['when()from(946,632)mul(22,105)>-:how())from();mul(439,876)?mulwho()@who()when()<@{-mul(111,687)$&mul(432,628)%*,{when()mul(287,508)'-select()}don't()/#~-@from()[:#mul(111,655)%: &;mul(518,391)don't()&mul(197,749)@who()*mul(935,21)+> who()why()mul(299,881)who()]@+%/from()[?do();{+[why()$select()#mul(264,731)]'select()what();+who()mul(713,161)where(931,649))mul(360,529)}where()!;]how()how()mul(619,728)how()<%}+}mul(420,324);{!from();mul;)>mul(144,181)who()}?~where():who()${ mul(261,691),why() /+mul(189,450) ++when()%^]]/when()~@mul(533,22)mul(231,586)mul(107,724)!@mul(504who()@}when(291,528)mul(691,859)/}#mul(726,544)from()[>>what()^?^from(762,977)mul(67,280)(who()^mul(586,844)}how()how()+where(),mul(886,320)#from(691,802)mul(376,296)'why()[@}how()#why(530,714)?>mul(791,363)?what()$${@why()>+mul(133,626))why()&}what(){%(mul(474,459)what()where()(^mul(311,372)mul(243,118)where()@mul(302,760):{who()-'{+mul(145,293)}why();how());$]mul(652,761%? ;]:@mul(259,51)@>'mul(474,995){don't():}*mul(309,511)?select();(mul(553,164)]how(){when()mul(556,560/~when(464,361),^<#[/&do()when()&who()*mul(22,727)from()):who()when()}mul(367,34/when()where()mul(226,816)mul(421,33'(,(#, ?who()do()%;+-:-mul(99,868)#{[why()who(504,690)?mul(119,337)+,mul(250,122))%#%mul(708,536)>select();from()mul(480,332)}&what(423,278)mul(891,459)}mul(852,128)mul(418,844)~select()[{when()(mul(69,312);!)where(419,852)<:*@-don't()what()!$-;{mul(91,636)-when()select()~from()(&mul(894,19)how()]@,[do()#,what()'@mul(116,57)why()from(),@-?mul(651,74)]what(704,54)how()<$mul(169,494) )from()$~-mul(44,491)$mul(29,368)'why()+-+mul(961,385)>who()select(137,499)mul(586,30)$%when(765,538)?why()(mul(378,568)}select()[:[%?$from()don't();mul(375,803)#']/@/^}*where(184,703)mul(21,508)>;+,mul(921,355)};mul(594,553@%])mul(793,439)]from(599,582)mul(982,538)}//^;how()~)'from()don't()~&who()<+#+mul(386,116)>/~who()mul(373,106)+#!do()(&-select(),+~;~mul(59,223)mul(233,653)mul(547,404)?where()~%don't()}}select() from()@] from()mul(150,196) ]}(>mul(514,824)where()+mul(332,418) +/>'^')what()mul(590,783)?$when()mul(55,422)from()~]select(198,113)from():}don't()mul(436,985)^:select()from()@//don't()/mul(675,976)$from(),!&]#*&don't()*[#how()~/*mul(172,297)<-{,select()};mul(866,305)$mul(938,509)+>mul(100,651)%where()how(223,719)/){!!;(mul(163,66)+''when()mul(217,127)^},(why()how()what()]who()mul(39,897)>/(mul(966,889)-]'}{,+,!$$why()from()mul(338,824)where()/&;mul(230,684)@'where(354,854)- )%when()mul(288,730){from()){}where()mul(124^}'(&don't()mul(688,399)from()++[how()why()mul(136,861who()!?#select()!]where()!do()&how()where()when()mul(968,380)'-$select()why()&:;mul(906>select()^~who())>:*mul(433,704)&why()#who()%@;what()>;mul(848,947) from(497,742)?select()~mul(590,692)why()* ,(<<)where()*mul(102,487)when()]where()>*when()!mul(406,710)mul(962,153)who()who()what()+&from()mul(419,854)$/mul(201,493)from()-)select()'^:[!;mul(779,883)why()why()}?why()(don't()what()]%~{mul(966,261)how()[when())^when()]mul(513,694)/*:when()select()who()'mul$?++~?:$mul(657,498)$ &why()mul(310,762)^+what()~,what()why()why()#}mul(320,448)%mul(886,159)(]what()?'@why():[mul(415@who()$what(){select()who())mul(912,558)select()where()%'why():mul(784,109)-&:-]mul(354,211):*#~mul(637,881),$from()mul(947,709)what():,'mul(284,267)&from()mul(259,860)/^],&(when()when()^mul@select() how()!mul(462,151)~,)where()mul(7,166)**what()<~mul(137,598)^]~>mul(561,283)*@ ;select()mul(262,658)who()<,-what()/-why()mul'$;;-]#why()>mul(510,757)$what()who()>mul(139,401)where()^#&/*mul(332,919)>?how()]when()/mul(218,127)[+mul(504,583)why()$>^mul(649,222)@?don't()from()&what())&'^mul(878,444),,mul(340,684)when()'why()when()?why()mul(852,617)mul(569,36)~(select():>~&/when()@do()why()from()>who()where()mul(93,146)(;why(863,117)mul(681,428);#mul)$from()how())?{] mul(793,537)where() @~?++?when()why()mul(163,958)select()}who()mul(540,501)$when()#who()/mul(331,537)[>~why()what(395,317)[)[mul(928,496):mul(127,943)$>mul(18,669)?&mul(443,860)when()!;;&select()*mul(624,669)[}:,*>]mul(941,338)?%where()((how();<~when()mul(483,529){)/'+'~$mul(254,159)when():when()'*!??%mul(731,125)when(670,595)]^^from()'?]mul(912,606) ):[^do()#{;?-mul(851,526)>'!'mul(689,174) >?{)how()/;what()mul(919,455)]mul(320,219){#+) {mul(267,831)((from()&:[-from()mul(84,102)mul(291,186)/+}*mulhow()?who()-*mul(973,390) + (~{$where()why(250,398)/how()&#!/usr/bin/perlwhy()-{{#^%>mul(772,422) 'how()}&)mul(184,89)$what()mul(376,816)when(),@#mul(949,642)'/+select() [mul(399,146)[~who(){;~ mul(526,131)when()mul(787,620)@~+?from()@-mul(310,482)}:$?what() ,mul(360,720)/why()why()#)^mul(561,462)+@:-{who()mul(403,894)from(),}{(('?!mul(555,514)?-:#&]$-mul(18,725)??mul(901,195)how()mul(814,623)$;~)select()/mul(4,986)#,{-]{!*~mul(833,296)/?mul(496,21why():@/#[what();mul(162,231))>~do()select();+*^?}?mul(38,18)?*who()&why()/who(479,558)from()%,mul(225,613)mul(494,841)'who()%@+{>&~mul(586,498) select()*!/]};-; /what()where()mul(614,161){select()where()+!what()/-mul(927,228)mul(432,16)when()?&+'^mul(539,745)]^when()what()mul(970,413)+<&:don't()$%mul(533,997))-mul(574,681)mul(978,225)&![who()why()(>mul(453,460)why()*+how()~!why()where()*-mul(840,804)>+mul(570,880)/>how()'-~*;+mul(367,248)$@what()mul(303,242)when()?>!!!-what()>%mul(547,184);when(300,185),^mul(196,377)~<{,[@from()when()from()'mul(727,421)>mul(199,873)],select()-+<&where()mul(962,772):what()/[${how()<*where()mul(534,770)select()/how()>*mul(356,156)((%where()}~how()#who()mul(801,644)from()mul#select(458,914)how()where()~}(}what()mul(273,584)who()mul(692,224)(>what()where();%,-mul(779,81)<[~who()!mul(786,30)]':mul(328,225)/when()#])(>:?:don't())*/mul(914,292)why()mul(83,290)[~mul(257,792),;+]*mul(949,612)*{;[-:]$^]mul(813,824)]/mul(5,572)}+don't():)from()*[>+(#who(422,860)mul(243,781)<-mul#<*<#&{?/'mul(579,115)(:mul(861,206)?)<&{>;mul(331,573)mul,^->,~>?who():from()mul(463,386)['{*>],when()?mul(740,88)]<[where()from()-]mul(685,334)+how()}how()^mul(973,106)select()$+mul(867,634)do()@-?mul(535,483)how()$[mul(161,41)]where(),#,mul(129,507)don't()who()what() mul(214,987)[ *^*[&mul(870,285)when()how())(+)+who()^how()how()'why(990,834)<@from(535,913)mul(741,712)why()when()who()@>-*!mul(296,759)$@where()mul(585,923)where()who()/why()]}>mul(163,233) ;;what(771,674)when()mul(553,949)select()when()'mul(545,530) +( !>{!-*}mul(646,888)]-?why()+mul(359,478)why()mul(186,893)]where()what()when() >select()mul(592,52)where()select()from()[select()from():~'/mul(164,308)what():>^)why()how()$&who()mul(377,499)why()((where()@~who(),mul(658,47)#how()+%from()'/select(287,448)]~mul(759,688)!@?]{(~mul(196,596)who()+mul(759,422)/@[#>where()mul)-how()>$&:$@},mul(861,458))mul(126,262)@;what(){};:don't()#mul(695,332)>what(),why()#++%select(){mul(471,679)^select()/mul(387,568)when()%(]*how()>do()' ^]?mul(777,56)>where()(why()select(14,295)mul(874,481)!from(){;~how()@mul(22,505))~who())when():&mul(395,566)-(~what(231,98)$$;$mul(326,815)>from() select()^who()#mul(189,720)what(),from()don't()>%+/mul(418,532)-;):don't())@:#!when():$[:<['mul(764,131)$:@&[from()%+~ mul(258,284)from(810,941)&how(232,473)mul(338,578);/select():mul(990,919)from()how()>[{!}mul(69,711),when()+do()}/[#who()*mul(907,255)?!}{]*mul(781,224)#select()mul(755,122))-:[',mul(863,596):how() who()%select()select()[what()do()+where()^+mul(722,58)^[,how()?#^ !:mul}&select()~{}mul(885,728)when()?mul(468,630)?where()from()select()>*~:don't()%#}$@-)mul(200,579)why() mul(10,435):)/;@{;mul(298,190)where()why()$ mul(9,631);where(7,826)@-don't():':;[,!@+mul(273,471)mul(325,690)mul(267,174)mul(339,952)}what()>)>@mul(307,862)do()why(335,510)]&>( mul(636,749)(-mul(592,773)!mul(2,50+'#mul(338,514)#;why()where()mul(127,711)@mul(146,911)mul(860,601)%select()mul(806,306)mul(410,847),:[mul(73,481)+don't()>}&)]mul(509,882)*/@~*where()-!mul)$do()where()?mul(308,113)/@?why()where()>%mul(970,425)~[$@'[mul(83,136)+<%who()who()who()^~where()mul(960,152)when()mul(714,401)how()?]from()*!what(313,652)mul(455,389)select()why()<-[)%:(%mul(929,53[-@%~;%]where()why()mul(293,806)from(258,940)[mul(959,603)!@)why()?{{$mul(744,798) '{}&<&?,mul(389,91)#'select()@,who()/$($-@,mul(792,652)how()mul&)!do())-}?~select()>mul(527,656)mul(220,657)who(453,708)mul&when()/when()+ how()how()%%@mul(62,191)'{%,mul(13,233)mul(780,237){from(817,212) when()mul(216,970)@what()}<: - ;mul(954,521)+mul(645,579)mul(695,53)%{},mul(927,488)$/$$what()&what()mul(815,350)](select(){;mul(650,656)&<:what(70,971)* mul(452,286)select()#&)when()+mul(438,390)>what()/mul(353,213) &from()~mul(920,652)~mul(971,287)don't()select()}/mul(4,595)where()-mul(332,353);>+mul(64,192)mul(33,846)}'where()(:mul(13,41)[@!^][(^$mul(728,587)what()>^&what()>who(73,62)why()where()mul(501,624)(%!mul(187,871),;>from()(, mul(563,884)]]/[who()mul(685,629){where()from()@@',who()why();mul(57,598)mul(799,387)why()?mul(963,99)*-!''when()mul(606,12)who()]how(), mul(615,921)/what()from(858,351){how()#who()@mul(379,934)~/{mul(871,792)<[+&mul(206,542)mul(154,161)-when():why()[mul(663,226)>from()from()?#mul(320,293)>select()!@;from()where()<^~mul(865,92)mul(834,118)select()#>mul(36,886) why()[>,,))select()mul(777,447)what()};<>how()from()*mul(942,312)how()]mul(810,261)select()@+>)'){mul(356,987){mul(33,198)[select()from()where()({mul(752,594)$why()who()>,?^where()->mul(478,705)why();select()from()when();]/]from()mul(878,617)@who()-who()[&&mul(554,90)#+:^do(), <{mul(978,459)/##[^ ]how(); mul(470,763)mul(342,6):&mul(192,104)from(220,224)[mul(309,253)$}mul(98,92)[mul(690,632)how()why()!>@mul(880,975)~/what()why()/*[mul(663,3)when()[@! )&,~^mul(965,150)#~},%^(]from()mul(503,640)::where()mul(146,66)&)~[mul(37,741)mul(714,547)/; \ No newline at end of file diff --git a/src/day_03/mod.rs b/src/day_03/mod.rs new file mode 100644 index 0000000..ec839a1 --- /dev/null +++ b/src/day_03/mod.rs @@ -0,0 +1,2 @@ +pub mod part_1; +pub mod part_2; \ No newline at end of file diff --git a/src/day_03/part_1.rs b/src/day_03/part_1.rs new file mode 100644 index 0000000..5470c95 --- /dev/null +++ b/src/day_03/part_1.rs @@ -0,0 +1,12 @@ +use crate::utils; +use regex::Regex; + +pub fn answer(text : &str) -> u32 { + Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)") + .unwrap() + .captures_iter(text) + .map(|c| c.extract()) + .filter_map(|(_, [l1, l2])| Some((utils::str_to_u32(l1)?, utils::str_to_u32(l2)?))) + .map(|(x, y)| x * y) + .sum() +} diff --git a/src/day_03/part_2.rs b/src/day_03/part_2.rs new file mode 100644 index 0000000..7ccd4ca --- /dev/null +++ b/src/day_03/part_2.rs @@ -0,0 +1,30 @@ +use crate::utils; +use regex::Regex; + +pub fn answer(text : &str) -> u32 { + match text.split_once("don't()") { + Some((head, tail)) => { + multiplication_sum(head) + tail.split("don't()").map(take_valid).sum::() + }, + None => { + multiplication_sum(text) + }, + } +} + +fn take_valid(s : &str) -> u32 { + match s.split_once("do()") { + Some((_, valid)) => multiplication_sum(valid), + None => 0, + } +} + +fn multiplication_sum(s : &str) -> u32 { + let re = Regex::new(r"mul\((\d{1,3}),(\d{1,3})\)").unwrap(); + + re.captures_iter(s) + .map(|c| c.extract()) + .filter_map(|(_, [l1, l2])| Some((utils::str_to_u32(l1)?, utils::str_to_u32(l2)?))) + .map(|(x, y)| x * y) + .sum() +} diff --git a/src/lib.rs b/src/lib.rs index 3748ad7..c0f5171 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod utils; mod day_01; mod day_02; +mod day_03; pub fn day_01() { let s : String = utils::read_from_file("inputs/01.txt"); @@ -13,3 +14,9 @@ pub fn day_02() { println!("Day 02 part 1: {}", day_02::part_1::answer(s.as_str())); println!("Day 02 part 2: {}", day_02::part_2::answer(s.as_str())); } + +pub fn day_03() { + let s : String = utils::read_from_file("inputs/03.txt"); + println!("Day 03 part 1: {}", day_03::part_1::answer(s.as_str())); + println!("Day 03 part 2: {}", day_03::part_2::answer(s.as_str())); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 142a34a..7a34b15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ fn main() { aoc_2024::day_01(); aoc_2024::day_02(); + aoc_2024::day_03(); }