From 7dad442a18accaaebc37c1ae26601b14a0b1dd1a Mon Sep 17 00:00:00 2001 From: artemtam Date: Tue, 3 Dec 2024 12:24:53 +0100 Subject: [PATCH] feat(day-03): parts 1 & 2 --- 2024/day-03/input.txt | 6 ++++++ 2024/day-03/solution.test.ts | 16 +++++++++++++++ 2024/day-03/solution.ts | 40 ++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 2024/day-03/input.txt create mode 100644 2024/day-03/solution.test.ts create mode 100644 2024/day-03/solution.ts diff --git a/2024/day-03/input.txt b/2024/day-03/input.txt new file mode 100644 index 0000000..0e4ec4d --- /dev/null +++ b/2024/day-03/input.txt @@ -0,0 +1,6 @@ +{!how()'&where()don't()select()@]how()}mul(884,758);-mul(971,475)who()~from()]~mul(358,23)~; +$><+mul(459,784)%,}mul(36,125)}?who()${select()mul(26,486);*@why(34,219)+who()}>*mul(439,790)who()^^what()}]mul(721,171);~>mul(240,453)mul(422,258)~-who(){'how(309,607)mul(437,905)> mul(670,503)?-*mul(979,674)%who()why()/&*:who()mul(412,637)> select()(select()select()'[do()&what()%how()from()<~ $what()mul(600,238);what()*mul(982,137)%~from())+~mul(550,306)@mul(331,66)>:&~ don't()?[mul(123,637)+)when()where(){mul(116,755)>why()mul(500,433))] where()] mul(801,467)~:,(where()^don't()what()(!/%mul(425,706)+ -!<{don't()from()+]',@mul(491,379)why()~:^select()from(286,158)mul(79,608)mul(892,927)&>+mul(681,799)why()> ]:*$how(167,759)%mul(79,235);,-mul(674,156)]why()who()from()from(949,563)' ~where()&mul(332,74)@how()mul(186,755)mul(65,13)-^),mul(334,303)+] ]!>{how()#who()mul(230,17)$how()+mul(673,345)%why(531,581)&+~]><)mul(37,326)$*$mul(119,45)from()mul(276,12)+^ do()?>%{%>why(){]mul(138,385)?~>mul(453,763)?;%mul(162,449)/[mul(629,968)&+@>:@select(){/don't()when();why(104,367) mul(136,744)mul(963,430)?~*what(829,68)}?#&{mul(32,898)~mul(875,563)#&+how()mul(946,225)why()what()^%#when(807,508)%mul(266,130)&%{$where();;--!mul(682,427)~who()$)from(69,589)%mul(103,378)}who()mul(458,652)%when(420,875)@(do()*when()mul(868,688)?mul(160,791)>+@when()>^}'what()from()mul(473,136)?$[;:who(){mul(825,302)where()&!why()/>from()select()what()when()mul(325,635))when()^from()mul(299,8)when()how()]:mul(332,547)?!}%#when()*from():mul(532,358)#[?how()@from()mul(961,542)select()don't() ;>,mul(357,878)@where()from()%/mul(61,265)~who():,{!where() how()^mul(854,862{why()what()%- {&what(671,387)mul(578,695)mul(436,41){{-/{ mul(447,680)when()?@$@#select()^>%mul(317,162)):>what()mul(13,671)where()$mul(309,369)({select()mul(249,325))-#from()~%mul(102,356)),*:;where()(mul(55,153)[-who()^^[>%<+mul(945,918)'[do()mul(115,952)-[from()()?[)mul(647,503)mul(474,649)'[mul(239,709)from()select()?^$^select()what(967,742)(where()mul(143,91)why()^,^{mul(465,739)+select()~who()/mul(244,259)don't())!when()who()mul(278,249)what()mul(276,375)do()mul(45,460){how()$from(),%<)who()don't()why()[how()mul(217,899+'mul(307,28), how(171,736),who(168,147)-;mul(372/'<{}who()mul(510,402)]why()<@!@-from():@mul(229,636){}[do();?)where()+(select()$how()mul(752,67)mul(344,833));)&,mul(601,124)*select()*< where()]mul(375,322)when(),]when(106,699)'$mul(477,697)+]{ how() %mul(626,452)don't()<'$when()mul(410,47) +mul(273,442)!what()) where()where()!mul(128,601)select();$/from()how()[ ;-mul(37?mul(150,909)}{where()-{mul(260,599)~/< &>mul(104,207)%-;?+>from()select()[-mul(328,706):](?>how()mul(706,921)mul(820,805)&[who();(what()/&(mul&(when()^'[mul(819,464)-+mul(880,740),'(]>mul(366,77)^:mul(211,80)^+([$@mul(363,53)&}why(541,819)'{mul(239,819who()#when()}$'*):?mul(53,216)<~{{:don't()&})why()]&mul(442,772)#why()%~!%*[/+mul(9,796)why()>}@don't()#what()mul(772,725):(/^-select()when()?][mul(464,901)from(218,74)why(203,518)how()-<{?mul(558,885)mul(206,857{@;$[how()}~^how()where()mul(373,741) <*+@ :^@$mul(717,894);where()who()where()@mul(864,646)mul(490,329)-mul(528,726),,@,+ ,>+mul(223,265)?,where()mul(343,632)){from()what()why() select()@mul(809,361)(from(524,60)^%]when()$?)mul(300,959)*@when()}select())what()mul(88,356)&}where()];mul(232,864)-{@:]do()mul(482,56)@{where(455,533)):$<[mul(10where(601,497)mul(426,581)who()}*]%select(907,807)>&why()mul(227,368)%mul(812,31)from()$-don't(),~,}/mul(149,756)'select()who()mul(52,518)from()who(47,890)what(),>&who()mul(476,778)&$'how()!<[#when()mul(317,462)what()%mul(556,241):#%*mul(37,135))+?select()%how()$mul(462,741)what()select()!where()!who()-mul(758,786)who()what()-);$*!mul(288,131)&who()do()$:mul(694,781)!mul(737,400)>mul(692,914)^%},>+&mul(292,865)[[]why(){why()&!how()how()don't() select()]who()when()-mul(120,234)}why();%^:$mul(12,755)from()from()select()%-?%}mul(616,813)@why()%from()-&mul(941,163)+;{~]mul(578,90)^;&(mul(947,324)!- ]//when()mul(881*}~mul(336,395)> $~[?mul(725,706)&<%mul(824,186)-@<)mul(330,957)$!mul(204,391)mul(265,505)what(),@:>-? /mul(974,931)$what();{[@{%!why()mul(90,923){#}what()?~-mul(696,784)<~who(718,997)mul(538,499)mul(476,975)mul(940,308)why()mul<+where()]'what()>%$}where()mul(820,423))mul(386,97)][,who()don't())?#,~*who()@mul(918,462{!what(382,394)where()[#(why()mul(904,736)%'}+mul(374,305)++!#mul(653,473)how(),-mul(162,434)?^@when()#mul(343,280)select()from(124,931)when(5,118)mul(468,653)}@#{mul(920,562)don't()&how() -mulwhat()---mul(950,664)!!select()-mul(4,72))when()$(how():!%mul(152,355)$+ '{<%mul(639,874)mul(704,465) !-$^&when()?)how(688,518)mul(718,288):{:-:()where()#mul(673,322)mul(871,761)from()when()~~()mul(893,784) +%mul(189,64)/}mul(537,332),;+}mul(275,738)]<:!@&),&don't()mul(886,924)?+when()/$]$-((when()do()^[where():?what()mul(436,577)})(mul(158from(238,897)+from()when()>how()what()@mul(302,395)how()&;from(886,251)%+!^%do(); {@,mul(349,117)>#from()from()mul(304,66)mul(598,654)?where()#mul(660,67)~[mul(977,328);who()!mul(728,941);+#when()*-mul(874,428)%mul(789,631):>+)mul(704,887)<*$$,%[what() mul(53,94)when()]#%$what()* ]-mul(455,519):<~(*>mul(385,498)>]why()(/mul(93,328)mul(293(where()?^where(474,333)?(mul(13,796)% %do()from()select()mul(632,756):why(11,315)what()mul(703,564)mul(969,63)!$/mul(887,388)#)-mulwhere()[(mul(134,297)how(){:[(don't()-select()from()}&#/}[ mul(679,478)&%'{mul(435,924)'[select();when()why()mul(840,427)!/*{:mul(188,195)mul!;$how()mul(187,258)~)#mul(488,233)}why()(:[when()select(){mul(567,681)when()}~%?$select()$:mul(72,236)+where()when():%where():@@mul(44,340)when()%mul(480,990),+who()mul(318}/(~%}select(596,203):/{mul(951,330){['+@-+^/mul(474,877)&,@@select()from()^mul(123,577)mul(828,17)}@]where()[mul(346,649):(>@{;$mul(568,77)%(mul(582,991)>where():from(155,417) from()/mul(93,912)from()%when(),mul(656,124)}do()what()from()^+;what()from()mul(660,451)what(187,581)['^^mul(642,281)&mul(690,722)why();when()[$+!when()mul(545,305))~mul<*&'^why()$mul(640,100)$:how(728,83)where()when()select()mul(333,982){#from()%/[}how()mul(720,343mul(833,88)~where())'mul(479,932)@why()/who()@from()what()!mul(346,663)select()mul(884,485):from()from():[://'@mul(674,41)how()!{mul(729,49)'select()~@who()^^who()mul(650,61)from(432,392)$^{~from()[mul(754,933)why():,*$'!$select()do()'when()?how()@-$/what()mul(964,456)(/ *mul(583};!)/(mul(476,807)[(how():how()'@~[#mul(227,838);&mul(339,718)'[from(),@mul(953,984)mul(999,724)mul(43,842)don't()>&;^how()/#mul(803,31),'@'#where()$mul]&^')]when()][where()mul(134,795)&+how();?>++*!mul(237,729)>{/}+what()[mul(41,980)<*where()mul(835,273)}:;mul(23,904)^#where():$]mul(969,17)mul(461,814)mul%what()]why()mul(759,464)what()@{mul(322,326)>where()who()'*why()):mul(175,609)how()^mul(53,892)why()select()]select()^){mul(750,639){how()(~>where()'mul(107,296)what()$:*mul(373,967[>]{)-!$ what()mul(281,521)when(573,540)who()] $$where()mul(465{from()$how()why()!+#(%what()where()mul(895,95)[why(216,972)}&& :!what(886,953)~mul(593,11)why()$[]~<+%from(926,553)who()mul(423,287)why()(from()(-(from()mul(854,833)#select()select()^mul(167,269)why()/]}:?]}>mul(745,993)~@what()'how())mul(827,736)([:@,what(293,969)'what()don't()-!~,mul(192,429);what()how()mul(847,188)>mul(141,869)[;how()mul(848,705)?when(){mul(559,567)^who()}from():!don't()%!where()>-what()](mul(105,363)why()where():where(578,872)(when()+mul(881,376)mul(695,809)^how()}!mul(364,248)?}:/from()}]]~%mul(804,4)mul(119,641)%from())%what()&mul(712,534)%??;-@@:what()mul(123,22)&-^from()$,mul(772,667)mul(42,86)])+!;when()^!$mul(622,677select(864,78)$mul(580,723)$where()#when()mul(751,301)+/(;+)]-select()mul(171,677)mul(661,374)~what()'*+ select()select(764,759):don't();)mul(840?{*%]why()mul(226,734)*where()(#*>^&mul(299,672)(who()mul(625,138))/<)don't();mul(468,424)+?%select()mul(760,645):(:who()};*#don't()mul(759,862)&@}] mul(334,129))]how();what()*how()#mul(305,664)when()):/do()%where()select()+'mul(58,303) ->what()* $mul(413,139)mul(982,406)-![[+select()(when()mul(165,136)]why()(when()mul(616,183)!+;/:%mul(481,602)}[&#^,{[@why()@<+mul(204,987!/mul(677,349)select()when()$who()why();[^,mul(194,35)select()',mul(127,110)-#:mul(596,30)- /who(12,984)-do()why()-mul(167,577)from()/'@where()$@-do()!>mul(981,462)*select()-+where()do()>]#~(,~mul(387,979)&mul(387,101)>mul(501,522)#{-mul(383,636)what()!+<mul(329 !$select()#]@[mul(864,202)select(){what()from()mul&/what(){>](~from()[%mul(806,858)[(>mul(892,860)!<:^:when()'@where()mul(491,377)&;/where()why()mul(478,743)when()mul(464,509)@;#')(+$mul(837,217)($mul(921,229), !/do()from():mul(45,689)-select()what()]$;mul(903,906){]<^when()where()<-mul(213,893)what()do()@^mul(514,953)where()[mul(143,932)~/when()$why()*~mul(990,283)~{# !mul(755,667)mul(474,41) +-:'&select()},how()mul(224,420)*{]}from()~select();)+mul(244}mul(789,459)from()+select()+?+'&}(mul(570,585)' ^select()why()why()^select()*mul(474,591)}++why()-{}%^mul(2,90)how(){!%mul(899what()$@,*mul(517,561),~)mul(510,855)from(){how():>[mul(485,191)?mul(238,765)~<(mul(606,527)<+/-where()'select()mul(446,167),mul(445,372)<>/(~;mul(142,440)[+%,}mul(687,94)@-?::-mul(297,299)mul(381,157)mul(711,860)from(359,7),% what()>select()mul(18,464)~ ~(':mul(340,143)+/mul(717,45)}#how()~^'}?when()-mul(268,887)#,mul(896,967)mul(29,886)~from()*select(169,789)where()]how(885,167)],/mul(802,796)([^[@*~from(),mul(373,685)who(),!what()+mul(834,143)select()@/how()from()(mul(30,941)'!;do()@&mul(925,254){mul(516,742){mul(244,359)&$!mul(458,20)why()mul(978,711)%how()+!){mul(402,128)why()mul(188,495)who()select()mul(62,537)&where()$()who()~:mul(720,554)?mul(888,389)@how()[$)~when()'mul(404,548/#mul(857,511)who()+#]who(919,86)mul(900,41);{?&/who()mul(134,188)%mul(140,747)+from()mul(440,286)}<$why()what()from(),mul(677,567:where()&*mul(406,675)%(how()where()how()~who()select()mul(544,316)select():how()who()where()[+,don't()where()mul(583,24)why()why()%mul(522,711)@mul(659,520)[@[;~what():&[mul(688,528)>where()select()]{;who()when()$>mul(951,800)select()when()%,;select()&$!mul(491,196)when()mul(800'^from()(where()how()mul(914,415)mul(610,36&]how(){{'>&from()mul(766,281)( ;how(250,381)what()^$;-mulwhat(385,84) };;*&why()when() mul(837,935)'+what()]#why()^%mul(477,874)-!how()(:,why(20,835)mul(764,685)how()select()'//mul(162,27):#*mul(64,768)] where()#mul(172,905)-(mul(686,898)%~'mul(498,986);mul(643,131)@mul(162,11)from() ;#[select()#>~where()mul(170,845)-mul(363,18)/@select()where()>mul(200,237)/%when(){^%;why()<>mul(203,934)+)when()what()+[^-~ mul(942,540)(when()where() ?]{,mul(488,295)*&?mul(127,140)from(){why()where();mul(347,470)* 'why()+what(360,948)}#do()'-]>/#(mul(266,243),'$mul(247,211)who(660,219)mul(250&^?/mul(382,956)what()%'/]mul(970,955)] :)(who()who()@mul(310,784)~$*mul(269,773)-where();!mul(754,294)select()($from()*where()select()(how() do()what()who())~,,why()%select()$mul(162,353)!from()>^~who()who() #mul(324,434))+%what()how()(when()don't()from()who();@mul(946,450)where()(!%']^do()~where()why();;>&^;mul(593,963)where()/from()mul(743,766;{?'&what())when()mul(184,787)'how()-where()mul(369,262)/from()@]mul(527 %what()#select()++mul(960,324)+do()mul(823,600))[>^mul(992,885)*mul(451,4)%+{don't()where()-(}~)~mul(616,643)select()what()@/~%:mul(112,749who()!*-don't()>%$who()mul(138,173)mul(824,205)from()&when()++mul(262,910)-@select(491,392)~'what()mul(480,642)+mul'when()%]mul(442,572)what()',*@[)don't()$mul(534,385)who()/*?[{:mul(706,170)[mul(867,234)what()why()mul(726,221)when()[&how()/{when()}when(456,111)mul(573,834)*when())mul(754,405how()]:mul(93,454)(how()+mul(604,209)/,[$:{@when()$mul(915,970)mul(381,253)what(655,195)mul(287,906};?what()#'# :#when()~where()!mul(635,559)from();what()+mul(119,665)where()-mul(702,720)][@mul(127,989):-@-!when()(from()who()mul(833,931)#why()]+where();mul(165,144)]{$where()mul(876,422)from()?where()mul(202,475)+what();~%$[~#mul(25,855)from()'(#(mul(50,883)/why()@when()],do()}why()-#&who(936,429)how()!why(112,951)mul(944,216)><(select()@:how()!mul(321,509)from()%<$why()>mul(743,426)why()how()mul(893,894)how():-># :]]@mul(978,976)(how()mul(504,604)#who()'where()]@,)who()mul(196,555);@mul(820,463)(#mul(497,730))from()[!mul(272,539)from()[&}@,{mul(542,203)~>-who();/mul(300,118)%$-^@^#mul(769,400)mul(393,409)mul(36,369)how();-(mul(660,560)-mul(815,344 -,why(356,705);*mul(752,637)* select()@%when(115,426)don't()why()who()$mul(95,166),^:what(912,146)who(506,607) !# mul(4,36)(:)^why()*mul(716,650)?*^''from()*;)mul(176,861)where(31,360)from()what()how()':/}/why()mul(345,935){+mul(858,985))mul(845,922){^why()how()who()]>who()mul(516,571)@who()@how()~};select()@mul(226,334)mul(311,634)mul(96,221)[mul(495,880)*@>({!?~mul(514,411)]~]' {what()>{mul(876,885)from()why()mul(491,922)select()> when(){mul(598,731) where()what() !&why()%how()mul(574,551)@$mul(836,195):{'what()where()what()when();]{mul(799,133)~}how()&why()}&@,!mul(390,782)+why()@mul(900,205)' what()+$% who()mul(805,770))#how()what()}->~mul(876,688)select()how()mul(584,620)#mul(917,463){mul(16,596)+when()how()mul(251,417)!}mul(184,265)'<@mul(472,681)from(162,792)!$+^]do()mul(211,162)(select()>%select()^{$mul(272,42)?how()}#?)*#@mul(887,322)when()]who(630,940)how()mul(420,32) why(),~+where()how()select()[why()mul(782,2)from()(who()%({+]when()mul(742,809)select())~/@ why()from())}mul(176,438)'?)+%!}&>mul(365,870)~mul(266,568)select()@%#;,who()when()}}mul(995,844)from() what())how(){#mul(206,566)mul(477,631)+,where()when() from()when()where():where()do()[from(624,927)when()&what()&who(161,77)>:~mul(715,209)&do()who()what()where()^)>&mul(349,130) why()>/select()(^who(239,506)when():mul(945,479)() +^~{mul(276,89)!:)how()$mul(506,517)/>where()select()mul(419,813)-%}@mul(310,325))where()[::,who()]& mul(409,240))when()+mul(859,189)-@what()what()from()^]]mul(297,639)@!how()where()~}{?don't()&}-?*^ !,mul(695,731)when()!mul(180,470)--[@how()from()'(mul(699,904)^mul(598,451)($ what()/(mul(22,310)'select()]-((where()mul(65,130)select(476,925)&>,what()how()~}why()do()@,+^?/mul(851,562)!'when()what()^where()$from()))mul(377,692)#where(){from()~don't()&^(>^from()/mul(433,800)mul(546,657)<*what()how()&how()%:do()&}what()>/who()select()where()from()mul(925,978)^(>%mul(991,829){;?[select(89,764) ( ]}mul(202,773)@from()mul(600,632) { + expect(solvePart1(input)).toEqual(182619815); +}); + +test('part 2', () => { + expect(solvePart2(input)).toEqual(80747545); +}); + + diff --git a/2024/day-03/solution.ts b/2024/day-03/solution.ts new file mode 100644 index 0000000..6136261 --- /dev/null +++ b/2024/day-03/solution.ts @@ -0,0 +1,40 @@ +const INSTRUCTIONS_REGEXP = new RegExp(/mul\((?\d*),(?\d*)\)/gm); + +export const solvePart1 = (input: string): number => { + const matches = input.matchAll(INSTRUCTIONS_REGEXP); + + let result = 0; + + for (const match of matches) { + result += Number(match.groups!.a) * Number(match.groups!.b); + } + + return result; +}; + +// For Part 2, the regexp will also capture conditions (do() and dont()) +// this will let us iterate over the matches and keep track of the latest condition +// to determine if we should multiply the numbers or not. +// +// NB: group names are used to later identify which instruction/condition was matched + +const INSTRUCTIONS_WITH_CONDITIONS = new RegExp(/(?do\(\))|(?don't\(\))|(?mul\((?\d*),(?\d*)\))/gm); + +export const solvePart2 = (input: string): number => { + const matches = input.matchAll(INSTRUCTIONS_WITH_CONDITIONS); + + let result = 0; + let shouldDo = true; + + for (const match of matches) { + if (shouldDo && match.groups?.mul !== undefined) { + result += Number(match.groups.a) * Number(match.groups.b); + } else if (match.groups?.do !== undefined) { + shouldDo = true; + } else if (match.groups?.dont !== undefined) { + shouldDo = false; + } + } + + return result; +};