From c43951c1dc31f650c969fe296cd62d75e1906fd3 Mon Sep 17 00:00:00 2001 From: Burnus Date: Tue, 3 Dec 2024 16:11:28 +0100 Subject: [PATCH] Add 2024 Day 03 --- 2024/day03_mull_it_over/Cargo.toml | 13 ++++ 2024/day03_mull_it_over/challenge.md | 53 ++++++++++++++ 2024/day03_mull_it_over/src/lib.rs | 73 +++++++++++++++++++ 2024/day03_mull_it_over/tests/challenge_input | 6 ++ 2024/day03_mull_it_over/tests/sample_input | 1 + 5 files changed, 146 insertions(+) create mode 100644 2024/day03_mull_it_over/Cargo.toml create mode 100644 2024/day03_mull_it_over/challenge.md create mode 100644 2024/day03_mull_it_over/src/lib.rs create mode 100644 2024/day03_mull_it_over/tests/challenge_input create mode 100644 2024/day03_mull_it_over/tests/sample_input diff --git a/2024/day03_mull_it_over/Cargo.toml b/2024/day03_mull_it_over/Cargo.toml new file mode 100644 index 0000000..c280af6 --- /dev/null +++ b/2024/day03_mull_it_over/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "day03_mull_it_over" +version = "0.1.0" +edition = "2021" + +[dependencies] + +[dev-dependencies] + criterion = "0.5.1" + +[[bench]] +name = "test_benchmark" +harness = false diff --git a/2024/day03_mull_it_over/challenge.md b/2024/day03_mull_it_over/challenge.md new file mode 100644 index 0000000..5040793 --- /dev/null +++ b/2024/day03_mull_it_over/challenge.md @@ -0,0 +1,53 @@ +"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the [North Pole Toboggan Rental Shop](/2020/day/2). The Historians head out to take a look. + +The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?" + +The computer appears to be trying to run a program, but its memory (your puzzle input) is *corrupted*. All of the instructions have been jumbled up! + +It seems like the goal of the program is just to *multiply some numbers*. It does that with instructions like `mul(X,Y)`, where `X` and `Y` are each 1-3 digit numbers. For instance, `mul(44,46)` multiplies `44` by `46` to get a result of `2024`. Similarly, `mul(123,4)` would multiply `123` by `4`. + +However, because the program's memory has been corrupted, there are also many invalid characters that should be *ignored*, even if they look like part of a `mul` instruction. Sequences like `mul(4*`, `mul(6,9!`, `?(12,34)`, or `mul ( 2 , 4 )` do *nothing*. + +For example, consider the following section of corrupted memory: + +``` +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) +``` + +Only the four highlighted sections are real `mul` instructions. Adding up the result of each instruction produces `*161*` (`2*4 + 5*5 + 11*8 + 8*5`). + +Scan the corrupted memory for uncorrupted `mul` instructions. *What do you get if you add up all of the results of the multiplications?* + +Your puzzle answer was `159892596`. + +\--- Part Two --- +---------- + +As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result. + +There are two new instructions you'll need to handle: + +* The `do()` instruction *enables* future `mul` instructions. +* The `don't()` instruction *disables* future `mul` instructions. + +Only the *most recent* `do()` or `don't()` instruction applies. At the beginning of the program, `mul` instructions are *enabled*. + +For example: + +``` +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) +``` + +This corrupted memory is similar to the example from before, but this time the `mul(5,5)` and `mul(11,8)` instructions are *disabled* because there is a `don't()` instruction before them. The other `mul` instructions function normally, including the one at the end that gets re-*enabled* by a `do()` instruction. + +This time, the sum of the results is `*48*` (`2*4 + 8*5`). + +Handle the new instructions; *what do you get if you add up all of the results of just the enabled multiplications?* + +Your puzzle answer was `92626942`. + +Both parts of this puzzle are complete! They provide two gold stars: \*\* + +At this point, you should [return to your Advent calendar](/2024) and try another puzzle. + +If you still want to see it, you can [get your puzzle input](3/input). \ No newline at end of file diff --git a/2024/day03_mull_it_over/src/lib.rs b/2024/day03_mull_it_over/src/lib.rs new file mode 100644 index 0000000..7993b1e --- /dev/null +++ b/2024/day03_mull_it_over/src/lib.rs @@ -0,0 +1,73 @@ +use std::convert::Infallible; + +fn instructions_from(value: &str) -> (usize, usize) { + let mut skip = 0; + let mut total_sum = 0; + let mut enabled_sum = 0; + + // We build a list of enabling and disabling instructions beforehand, so we don't have to + // rfind() them in the str for every product. + // We start with the default case of enabled. All other indexes are offset by 1, so an input + // starting with `don't()" will take precedence over it. The offset doesn't hurt us, since we + // look up by match index anyway, which is always at least 12 higher than the last match for + // `do()` or `don't()`. + let mut enabled: Vec<(usize, bool)> = [(0, true)].into_iter() + .chain(value + .match_indices("do()") + .map(|(idx, _match)| (idx+1, true)) + .chain(value + .match_indices("don't()") + .map(|(idx, _match)| (idx+1, false)) + )).collect(); + enabled.sort(); + while let Some(idx) = value[skip..].find("mul(") { + skip += idx + 4; + if let Some(idx) = value[skip..].find(',') { + if let Ok(lhs) = value[skip..skip+idx].parse::() { + skip += idx + 1; + if let Some(idx) = value[skip..].find(')') { + if let Ok(rhs) = value[skip..skip+idx].parse::() { + skip += idx + 1; + let product = lhs * rhs; + total_sum += product; + + // `partition_point()` returns the index into enabled for the next do() or + // don't() after our current match. The entry preceeding this, contains the + // relevant instruction. + if enabled[enabled.partition_point(|(idx, _is_enabled)| *idx < skip)-1].1 { + enabled_sum += product; + } + } + } + } + } + } + (total_sum, enabled_sum) +} + +pub fn run(input: &str) -> Result<(usize, usize), Infallible> { + let (first, second) = instructions_from(input); + Ok((first, second)) +} + +#[cfg(test)] +mod tests { + use super::*; + use std::fs::read_to_string; + + fn read_file(name: &str) -> String { + read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string() + } + + #[test] + fn test_sample() { + let sample_input = read_file("tests/sample_input"); + assert_eq!(run(&sample_input), Ok((161, 48))); + } + + #[test] + fn test_challenge() { + let challenge_input = read_file("tests/challenge_input"); + assert_eq!(run(&challenge_input), Ok((159892596, 92626942))); + } +} diff --git a/2024/day03_mull_it_over/tests/challenge_input b/2024/day03_mull_it_over/tests/challenge_input new file mode 100644 index 0000000..a2d0115 --- /dev/null +++ b/2024/day03_mull_it_over/tests/challenge_input @@ -0,0 +1,6 @@ + (who() what())>when()why()'mul(454,153)mul(565,994)(mul(890,533)#mul(875,768)+'-^where()}when()mul(103,598)[mul(401,600)when()from()how()+mul(928,225)why():mul(909,344)when()'?where():(what()mul(972,219)${/ mul(402,908)who()&where()~where()mul(60,961)#]$mul(459,473))})~mul(143,89),select()mul(772,463)mul(742,92)<(]},select()[%^when()don't()select()from()mul(776,917)+!!why()when(485,399)what(720,754)from()&mul(746,260)where()*who()~&/from()what()]what()mul(694,820)%why(16,195)when()'why() [when()why():do()when(),'#from()(mul(200,440)/]mul(58,834)@!!mul(175,542)mul(363,28)]}don't()+[{<@/,mul(103,154)mul(808,942)$where()why()from()[mul(780,395)^why()#-!:~why()+mul(879,633)<[where()!what()-,mul(6,80)how()?)^select()<}when()]how()mul(385,345)~?don't()<)'mul(557,404)+;/<;}%?//mul(586,270);^&,^}'&:mul(914,485)>,$mul(616,138)'[/-mul(786,357)&why()where(490,893)/-}*}mul(967,485)!'mul(664,982),when()^why()$%(,why()mul(826,970)$*~'),+when()-mul(586,837)+;(/%{[what(201,897)mul(540,583)don't()}mul(358,207)mul(988,94)$#&}mul(580,42)#select(190,243){*%*from()mul(491,435)* mul(345,720)+#{)/,'<},mul(529,664)? don't()'mul(925,474)}:!?]mul(655,353)when()'how()(don't()how()^/where())select()select()^~{mul(39,73)['[<^)/!;mul(338,323) ~* who()what()where()mul(746,703)mul(70,40);/:!mul(89,464)when()how()~?;~}mul(669,359)+mul(64,989)why()what()where(478,19)^from()$mul(49,43):why()mul(946,368)who()%?{'why()mul(754,618)select()}^mul(925,787)how()select()<^;{}}%mul(857,178)@?$;)%what()['mul(343[<^mul(622,524)$#{&where();@how() #mul(572,8)who(244,369)/*;;mul(22,910)how()?who(200,683)#what()why()mul(9,541)don't() %>mul(583,678)mul(977,618)mul(846,566):@mul(601,58)%mul(12,677)^#[how()?do()&'%how(985,614);@? 'mul(295,467)}@mul(462,179)[from(946,303)@what(236,293),from(572,15)^mul(602,286/*:(select()!mul(950,26){^}]-mul(541,949){/select() from(202,590)what()]select()~mul(931,833)^/}where()*mul(665,272)*'where()>} :]mul(470,26))mul(945,496)%-!;-* mul(958,735)+mul(292,844)where()mul(323,887)select()how()why()%~why(974,789){!/:mul(890,844)&]{'mul(727,812)?mul(521,348)']~,mul(604#what(201,469)]{'how()#[mul(68,187){#+%* when()+when()mul(270,65)select())how();! %where()]@mul(267,348)from()select()who(579,714)where()mul(359,887why()who(120,403)from()&!)do()mul(614,872)select()[:who()'>>?]who()mul(373!:>&mul(13,848)~how()who()+*what()&mul(214,262)/where(941,723)mul(34,526){ }( ~from()){ mul(648,400)mul(946,270) +where(),mul(156,431)when(444,909)mul(10,765)#mul(393,855)how()@&from()mul(538,109)[%^-<(how()}how()mul(841,917)~ ]^! ^select()mul(624,87))what()&[-'/&)mul(113+from()how()why()]&>mul(82,319)&:''where()/<;mul(882,75)>/^#/how()!mul(481,243)}*!what()#}#mul(93,825)>'(!where()(&mul(417,293)>[{*()}how(349,243))mul(577,515)}mul(644,588)why()~~what()'why(658,231)]where()mul(260,870)mul(82,64)$+>,&$where()$why()where()mul(513,597),when()why()&mul(559,679/why();~{mul(876,302)/+why(921,489);%@from(){do()&+from()mul(646,666)how() }^;{!?mul?),$*mul(244,591)<)select()(^select()where(454,602)when(){mul(201,421)) ,when()$/$;#;mul(715,512)~-mul(110select())&/;>:^')+mul(525,861)mul(94,520)select();mul(200,362)mul(662,110)what()[';mul(706,110)mul(524,196{ from()%/from()when()select()*mul(952,760)@<@(mul(359,289:?[!:mul(788,331)>(-mul(790,172)~/';(#mul(744,504)}@!#:!)where()mul!why()<+mul(24,595)($/why()&(>'why()}/mul(809,414)mul(202,168)& where(){- ]where()where()who()>^mul(543,106){don't():/('why()when()[):#mul(261,544)why()-+mul(325,976)-why()& what()who()%*&;mul(309,586)from()~;mul(527,332)]how()when()mul(266,71)mul(383,68)how()from()}how()[{;who()why()mul(479,649)-who()>mul(954,835))(,'?)why(),mul(73,982)-+mul(275,963)?:*>+%%}%mul(159,868)[{mul(849,590)where()how()when()mul(380,542),'{mul(716,261)@$what()[where(961,898)+#;mul(102,332))mul(151,993)who(433,641)what()!],{}mul(225,989)from()$>'/>mul(271,482)(]when(703,790)$['&don't()^ mul(292,747)why();;from()why() #[??mul(675,831)*/<{when()%mul(82,353)?~(~@from()-mul/,when()[+', /why()mul(531,95)')<&#]#!where()$>select()[mul(326,511)/:#::when()(do();mul(960,765$mul(947,618)!from(704,544)-);select()}mul(278,843)&+-&)!}select()why()mul(333,183)!~mul(360,636)}@mul(43,235)& ~*how()~&>$~mul(163,420);@' mul(328,120)&}#)what()~[~!mul(892,685);from())*select()]from(),mul(454,194)!what(598,913)~what()who()*),*who()mul(651,343)~'mul(311,104)~%~how(){when(){mul(348,414) +!who()when(),mul(565,187)'who()mul(573,606)/]/?^{do()when(),mul(301,391)select()!]%mul(662,532)why():>mul(317,266)-]/!#?~what(395,165)mul(882,259)(how(){%)from(355,754)from(){?+mul(251,179)+}$^-[mul(244,700), $$mul(989,641)how()&>[{(@when()!mul(17,250)why()mul(516,948)/from()&)](~who(),mul(936,726)from()who()mul(642,108){?/*:who()](-)[mul(234,936)//select(){don't()}!mul(711,979)when()why()~( what()%from()^:mul(970,839)who()~$from()how()mul(525,184)(*}from())mul(592,712)select()where()from()do()/!from()@%* /:mul(796,770)[select()mul(737,95)*who()mul(538,295)from()when()mul(202,997)mul(920,622)[^,'^{~&mul(890,310)-#mul##$what()how(),*{]what()'mul(753,51)why()from()%+*-mul(44,560)^/who() how()mul(846,745),$ mul(884,965)?];@;'mul(607,810)]don't()?mul(15,474)*^%where()>,#- mul(381,876)>]'how()[]?when()mul(350,528)&>%%#*;mul(465,783)?/from()'mul(109,659)*'mul(260,549))where(427,3)mul(750,184)mul(514,568)+ where(838,713) ~$]{what()mul(505,954)<>;-#:mulwhat()[!]why()mul(899,266)%}>select()?]'mul(936,764),~mul(189,689)}^mul(474,190)<;&>]how()/{mul(271,843)>}why(522,791)what()do()#/^where()%when()!mul(284,340)##[why() when()>{mul(998,539)*[*):'>'mul(577,914)[*)who()mul(305,505)who()!@how()'}when()mul(331,543))mul(91,478)(where(196,348)&:]>?mul(560,724)where(872,560)<% what()*where()]select()do()(what()-~mul(229,896){?~when()mul(826,622)&(#mul(451,442))what()mul(866,906)how()$-&where()>-([mul(199,824)!~)$/<:mul(727,123)do()$*-#-@+where()select()$mul(612,415)who()$#,who()>/from()mul(967*who();%@}how()/why()}mul(106,998)how():(;&mul(345,702)mul(275,94)]#>{mul(426,479)why()<;~what()$!+@'mul(40,71)&when()*%mul(973,215),what(),mul(709,788)who()):mul(205,236)~mul(50,81)+how()%~/mul(748,800)(}}mul(124,528)mul(129,959)%[&[select()mul(547,292);who()>???why()+who()mul(442,582):how()>select(92,760);+mul(440,501)@/@>#&:mul(764,753select()%:how()where(202,288) mul(613,367)how()?mulselect(255,7);[;why()/$ mul(351,228){mul(312]+!^#+,why()what(327,951)mul(134,668)&&?'!why()where()mul(172,288)+mul(90,27):';*@how()/what()mul(272,960)select(845,387)don't()}--}}?)mul(509,779)-)mul(682,498))from()#mul(69,525)%-?>>mul(68,359)/mul(923,989from()~mul(132,353)mul(980,998) (where()}*>&how()[mul(570,289)*~mul(388,859)how(829,278)[<]who();mul(199,174)*~:[what()^}#who()>mul(221,636)*-()%-when()from()mul&}who()[mul(725,983)>#!who(592,5)%mul(85,978)what()'mul(178,305)[[?:#->#! mul(374,804)<$&{->++mul(446,37)-#&}%)mul(19,914who()(# ;mul(959,330)what()>#who()who();mul(228,273)from()+}/&why(783,754)from()(#mul(649,849)*why(){why()(who()?!why()from()mul(344,447):how()+when(144,750)what()mul(217,542)where())%mul(253,8),$how()%[>#from(),:mul(174,127)-:when()-don't()where()(when()& ]^from()mul(263,164)mul(873,530)::@select()mul(459,547)why()!}$*mul(674,506)*]~don't()mul(570,150)what()do()<'^)~mul$mul(508,426)/why()mul(155,774)~?from()![mul(670,674)select()?^when()from()]{mul(736,574){>'~^mul(248,37where()@:where(938,730)select()!'>,;mul(827,796):what()%where()!/how(231,307)mul(88,489)@!!mul(173,890)how()[- +what()+$~mul(244,436)(why(),[,-;[<%mul(991,950)%mul(804,561)&:,mul(442,575)}~what(523,563)?+ how()/}[mul(866,343)]:where()where()>$mul(423,440)~>[mul(259,864))@ {^mul(256,238)[:]*$$mul(206,916)*}!what(937,263)select()}don't()'*&mul(630,249),mul(521,101)/who()!mul(884%/&;^from()mul(846,533);}why()?;mul(538,765)+,([ !}@]{mul(98,545) ~how(703,291)]#)~[[mul(588,167)*-+mul(246,51);+from(){;;what()/:?mul(921,460)<@where()mul(674,502)/)select()%~what()?)mul(406,916)>/mul(501,656)]mul(979,533)/why()*select()where(),^mul(370,549)^~how()why()+,how()@mul(168,884)',<$]mul(377,183)+$mul(425,602)when()~'>when(),when()%mul(231,25)[why();'!mul(879,941/who()mul(658,479)why()where()*mulwhen()mul(486,551)where()?,)~ what(221,542)/;mul(260*how()~~why()mul(571,395)mul(319,33)where()mul(313,612)select(458,89)when()?from(),)[%^[mul(489,554){mul(482,42)from();mul(911,838)from()what()&/when()when()mul(748,509)/!&who()mul(98,104)what()',how(660,701)^mul(904,678)&}'mul(498,173select()% -from()!when()mul(918,395)?<<@mulhow()/from()][don't()+select()who()what():^^@mul(826,867)select()^[@mul(673,493)select()@(select()mul(152,937),why(955,595)select()where()-'mulwhere()~?mul(572,408)when(606,22){~*(mul(971,805),how(402,696)')}who()&@*who()mul(76,96){where()mul(711,3)*-+~where()mul(812,534[select()!#(how()mul(799,474)>mul(251,649)(from()what();-do()~;]what(),what(393,86)?mul(892,224)who()mul(635,260)mul(300,39) +}>select()>@why(364,672)who():where()mul(385who()))>when()why()&>mul(276,702)&:mul(192,782)/:)@mul(948,155)[!don't())what(),what()mul(747,717)#?$mul(967,489)when()who()why()#mul(961,811)what()mul(307,623)~}from()how(),mul(669,93)^ &select()how()mul(105,129)?])*)mul(246,433):)how()mul(941,140)<[ ;&*mul(580,48);%%'why()what()( %mul(189,229)]'<@)/mul(741,684)[mul(632,558)'@what()what()mul(986,227)select()/from()*when()@mul(747,356)mul(674,720)mul(10,513))]#]-mulwhat()select(501,639)mul(114,558)[}$]who()mul(759,134)#who()/@*mul(444,475)!@+don't()#when()}where()[where()',select(){mul(206,80)/!&how()+]^mul(847,293)+(mul(837,220)/!#!mul(613,470)#where()from()&$mul(29,370how()]&/-where()%from())> mul(251,617)-why()~^don't()##~mul(176,935)[;%@?^,don't()!mul(564,493-(&+@'[<*;mul(241,470)<>}mul(684,534):?[>?+:mul(835,881)mul(901,992)mul(595,306what()% mul(333,684),what(910,959);^what(),where()'mul(493,314)}-what()&who(231,428)where()>' [where(),]who()mul(215,607)&'mul}select()^%/what()do()&mul(735,766)#%$']*;/+do()~what()how();$#[/select()mul(73,86),who()#who()?*[mul(839,173)&$$[why(349,377){why()>why()mul(49,44)@'};!)*;mul(265,467)why()}'mul(561,416)}]-mul(334,113)mul(126,247)}mul(536,247){?mul(780,762),how()<'who()what()%what()mul(324,650)* ^)mul(116,480)& %>,mul(722,539)-who()mul(69,643))+select()<'mul(398,585)mul(635,160),!{}mul(218,149))(why()[when(),mul(4,152)@what()*@>@how()mul(78,645)@*#}#select()({mul(196,97)when()@what()+why(287,586)when(){mul(908,484{%-where(){'from()mul(254,416)how()}*(how()#@}mul(670,932)mul(741,232)&'!?@$mul(775,804);),,!mul(757,128)/?'!mul(47,297) $/,,:@~select()?mul(604,497);mul(770,711)+mul?where()/mul(777,175)^>where()<#mul(376,176)~){}mul(758,497)!){%where(), mul(360,122,mul(54,973)):mul(248,44)[where()^mul(162,809))*what()^?+}what()how()why()mul(839,131)mul(913,94) why(),why()'mul(459,43)<-from(),#%{]%/mul(733,345){,/+<[?mul(292,416)& [>/^why(408,559)mul(648,879)#)$ +how()where()#mul(708,178) +how()*[mul(560,727)*when()from(); >~]'&mul(90,393)$~from()mul(578,828)mul(710,50)?+@what()@+!;mul(250,79)when())'mul(523,730)+*}^*mul(698,766)<-why()-/:!/select(),don't(),mul-+[where()$,mul(238,155)}(&<>;[!do()where()(mul(246,69)?from()where()!mul(385,209)who())<(when()!,[;mul(567@<})>mul(214,524)}#%>[mul(429,28) who(){from(664,943)>?mul(747,239)+!!who(75,291)(where()&from()mul(363,660)why()~mul(971,115)when()what()where()']do()/#/;<+)mul(313,612)(why()@[why()#from()where()how()^where(929,904)@do()*from(936,438)[ when()+:mul(772,586]*mul(843,866)<}+[/&^-when()mul(62,918)[mul(602,984)&@~where()how()^when()>'/mul(3,2)<]mul(134,654)<-};mul(456,534)/({ what()do();,*select()]^)mul(528,106)<>mul(157,393) &where()mul(697,508)mul(793,680)+[?%$~~%don't()mul(882,564)mul(954,646)]&%how()mul(322,840)~select()mul(589,361);#!#mul(781,972)mul(764,49)*?^}where()mul(698,69)]<&(what()[^<&mul(438,77)?@who()who()when()mul(712,314))^? mul(806,50)]},@(mul(522,258))mul(750,831)^mul(2,167)where()@@what()[)select()mul(480,518); ^&how()%]mul(856,366)select()}when()/]!#?%where()mul(246,951)~why()&%/{^mul(771,634)# {how()when()>+#![mul(489,260)!::?'from()mul(527,396)&})mul(557,921)$>[!mul(551,664)>[mul(868,16)}:how()how()^;/mul(641,963) {mul(130,436)>~?<@+]mul(745,360):)[/?]@mul(796,631)where())why()~~mul(191,523)how()mul(902,53)-what()where()who()!from()mul(908,532)who()how():${@#mul(998,383)$,[:;mul(154,723)]]+ &%<],mul(279,823){who()mul(171,633)&^when()<}'from()mul(408,866)select()#how()?mul(818,40)where():$)when()[&who())mul(203,389){why(717,710)where()$}])~how()mul(608,627))how()where()$-why()/mul(296,533){mul(341,534));$#/-^mul(501,854)^^+>what()]how()*:+mul(69,163)*;mul(810,823)-when()/)(mul(825,165)what()>])~@/mul(495,234)?] mul(918,879)]^';%)when()#?mul(656,686)*;