From 4e43c858167df4fd667d2213fea78ed52597cc3a Mon Sep 17 00:00:00 2001 From: Nicola Zangrandi Date: Tue, 3 Dec 2024 15:09:37 +0100 Subject: [PATCH] Added day 3 of 2024 --- 2024/day01.py | 2 +- 2024/day03.py | 29 +++++++++++++++++++++++++++++ 2024/inputs/day03.txt | 6 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 2024/day03.py create mode 100644 2024/inputs/day03.txt diff --git a/2024/day01.py b/2024/day01.py index cd2f979..55c96ac 100644 --- a/2024/day01.py +++ b/2024/day01.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 lines = [] -with open("./inputs/day01.txt") as f: +with open("./2024/inputs/day01.txt") as f: lines = f.readlines() left_column = [] diff --git a/2024/day03.py b/2024/day03.py new file mode 100644 index 0000000..8c09681 --- /dev/null +++ b/2024/day03.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import re + +with open("./2024/inputs/day03.txt") as f: + instructions = f.read() + +r = re.compile("(?:mul\\(([\\d]{1,3}),([\\d]{1,3})\\))", re.MULTILINE) +matches = re.findall(r, instructions) + +multiplication_total = 0 +for match in matches: + n = int(match[0]) + m = int(match[1]) + multiplication_total += n * m + +print(multiplication_total) + +r2 = re.compile("(?:^|do\\(\\))+?(?:.+?)(?:don't\\(\\)|$)+?", re.DOTALL) +matches2 = re.findall(r2, instructions) + +do_multiplication_total = 0 +for match in matches2: + mults = re.findall(r, match) + for mult in mults: + n = int(mult[0]) + m = int(mult[1]) + do_multiplication_total += n * m + +print(do_multiplication_total) \ No newline at end of file diff --git a/2024/inputs/day03.txt b/2024/inputs/day03.txt new file mode 100644 index 0000000..313be04 --- /dev/null +++ b/2024/inputs/day03.txt @@ -0,0 +1,6 @@ +<,:[*where()%mul(53,612)!^}&mul(3,518)??$~select()>??]mul(245,515),why()who()*@from()(where(242,190)mul(817,764)^select(),+who(851,301)where())from(){;mul(431,780)mul(110,982)what()what()]mul(441,829)??where()mul(269,112)>when()?who()$**#!@mul(343,569),;what()from(){(;}mul(486,404)why()]#~when()%@do()+:'why(256,886)why()who(868,710)mul(103,406)?>mul(563,652)from()$-@when())!@:from()mul(744,992)[<^~}mul(822,789)],+select(45,52)!,why()*mul(433,911) }why()<&&how()who()?]mul(877,899);':what())#{]@mul(401,705)@&mul(627,54)? )~+mul(668,930)%)}do()what()#from()$/when()[:when() mul(545,444)how()]mul(409,150)>?%?@mul(295,205))'mul(321,875)mul(915,728){?when()what(644,532)*?;<):(:mul(566,595)%'mul(499,468)/'where()}mul(721,340)]when()mul(162,291)mul(73,373)<]where()mul(100,385)][what()]mul(832,226)mul(675,546)who()how()#<^+how()'mul(119,723)what(){+,-mul(959,612)select()mul(758,905)!mul(247,521)&}$?don't()!'$ what()where(353,94)select(){mul(997,311)@from()/mul(987,583)&select(207,730)mul(299,379)select()do()what()select()when()select()]*?(?mul(841,179)!when()what()where()@:why()'>]mul(265,944)mul(968,747)what()}),(mul(541,36)~how()~select()where()when()-+when()mul(448,567)select()where()why(342,599) -when()<}mul(566,357)/$*>> mul(414,962)mul(904,116)what():/'@--}where()mul(390,663)who()~~],when()&where()[%mul(304,146))'who()>+when(){mul(526,627)!;(]@}where()where()mul(253,767)-^+]how()>,,!do():~[^when()]mul(263,450)mul(757,944)><' (],mul(934,282)@{who()+%when()mul(136,652)mul(366,573)!$//where()mul(501,845)}+~select()+]mul(532,462),~},from()mul(322,350)@~mul(64,374)&what()>[){mul(962,241),?/}-'+mul(39,522)%!$do()^}mul(447,40)mul(971,488)()mul(983,842)where()/mul(956,799);*+)^mul(233,820)mul(957,287)&>[when()why()where(855,333)<@)mul(304,558){~from(),how(){mul(555,718)]]-where() mul(881,781);^&);mul(231,766)$)mul(7,979)^why()'/,mul(352,951)mul(575,449)'mul(803/#(<^when()>mul(500,697)]~^<>[select();^when()mul(822,422)@[where()what() how()&;[(mul(756,744)how() @mul(741,854)what()(do()&how()){what()'^}-mul(664,580)<+ &+mul(788,961) -&}#!,how()who()mul(378,886)[!{,mul(53,236)what())select():~>why()~@who()mul(275,625)*from():what(80,711)why()@don't()mul(426,724)?&from()-)/^-mul(47,60)&+select()?%why()&where()mul(706,388)from()/how()mul(2,761)mul(377,995),from()*?{how()mul(198,840)why()&';[/+^~mul(72,849)mul(720,901 <(mul(998,859)mul(409,24)>?#-)why()mul(208,800)~how()+^?why()^mul(765,394) )mul(269,340)>mul:$~&select();mul(466,453)why()mul(59what()-?'where()?;who()mul(474,555)<[]]when()%-how(225,412)mul(626,574)/,{{what(832,679)where()how()mul(250,300)<#$;+mul(840,193)who(562,102),mul(390,251)(mul(6,397%~what();%who()from()#![mul(95,829)where(811,818)@mul(847,742)'~+&~@/mul(329,961)>~mul(625,917)#+what()%?>mul(314{mul(392,487)mul(770,684)]'mul(607,73)]where()/#what(378,688) :}'why(778,249)mul(732,423)from()$;@why()>mul(255,618),#+from(849,914);>mul(99,409):]mul(948,905)select()<@mul(342,35)$!@how()/)where():mul(49,617)mul(396,154)>mul(671,367)mul(68,795)~select()<:select()~/mul(900,855){ why(622,665)#when()-<@mul(629~{;~+how(){'mul(907,182)}<;+{' mul(223,519)}+>mul(776,473):where()what()^when()don't()/^ -/(/mul(894,814)mul(698,226)}~/mul(570,960),?@?what()select()}/mul(918,543)'[from()&why()where())~from()[mul(321,507)mul(616,831)why()when()/]{mul#>[!!; mul(480,401)'%%~](from()mul(643,85)who()where()mul(982,510)when():,(mul(799,43[from())$what()%where()@;)mul(420,538)how()mul(358,891)who()what()what()select()(?how()*)mul(612,717)<+(where();who()from(170,645)how()why()where(310,422)mul(789,673)mul(779,49)what()who()[from()$*'&mul(211,472)why()< mul(77,895)?&:*-;^mul(861,148)/what()[+#}when(360,356)mul(905,744):~<-how()][-where()^mul(272,757select()'mul(994,335) what(362,794)$;$mul(242,327)who()when()who();don't()$mul(808,996);don't()/}from()(what()who()mul(398,570)from()~select()[mul(420,441)+mul(452,212)who();~what()#how()what()mul(557when(699,863)mul(184,806)&')from(),:'';don't()[select()mul(139,882~^(how()&mul(931,970)>]] &do()&*)(,:;,mul(924,272)who()$$%select()?/[mul(712,215)~,mul(114,862) :select()#who()$&how():mul(617,150)]why()~(&@?mul(28,990)-)mul(221,546from()^:<&/[select()mul(5,78)mul(476,316)(<'%[mul(227,163)from()where()who()why()[ mul(229,680)>/+?how();select()&$[mul(110,33)~{)#who()]^+/mul(479%%#{#from()select()when()what()what();)//mul(229,348)how()*/mul(643,351)mul(615,362(/->(how()-when()mul(499,697)*don't()mul(992,821){from(413,389));when()who()#mul(921,742)mul(253,580)]how()how(102,372) mul-from()where()mul(104,127)what(),{what() what()[*mul(657,659)'-select()mul(612,851)how()<~-when()$select(718,662)from()(!mulselect()-/^,<&^~mul(497,827)who()-how()-$select()+*mul(943,533)@;mul(403,629)why(826,610)} +:>mul(943,942)why():-)>^why()what()how()mul(302,757'mul(22,286):/-{mul(391,954)mul(528,734)select()-where()why() mul(909,426)!/($+mul(106#&-from()^mul(592,459)'(select()]!when()@{}*mul/&'>mul(894,472)who(),-#mul(924,264)%where()-{from()!mul(956,587)-$do()&why(173,205))$&~where()#&}mul(615,17)how(){>%from()from()@where()mul(683,358),how()when()-}](;/mul(688,622)/select()}(what()mul(872,568)/why(){mul(600,562) #mul(733,946)( ?]/when()$(mul(606,986)@!when()mul(597,648)what()-who()from()?,>]!'mul)>-&@~who()/%!mul(486,264)from())mul(556,968)(who()[:$:,*mul(564,519)# %)&/from(390,158)mul(583,746)how()why()('#/mul(519,835)&(/?^#,mul(447,762){?,]]]/@mul(257,451)&<$why()(mul(670,126){#:mul(555,660)}+%$:(select()mul(723,37)why()who()}where()}how()%)>don't()#where()how()]%what() select()mul(726,542)how()mul:how()/~}) mul(753,350)mul(905,645)+ how():(when(879,434)mul(632*?'$/,<*what()mul(442,98)$&(&;mul(424,515)!mul(443,813)**),^%how()what(),mul(252,469)select()[)mul(370,416)%&::&+%mul(885,78)how()^mul(885> {&,why()$mul(52,196)+,{%select(),!##;mul(714,770)mul(607,394)when()how() -@]mul+^@mul(90,350)~@+where()mul(934,150)when(788,941)^mul(557,327)mul(86,867) :~$-how()select()who()mul(166,855) what() %]mul(928,22)%when()-how()$!what():mul(628,252)~{>,who()~where(967,401)#mul(943,366)'[*>@mul(781,986)>:when()?&don't()~ :where()who()select():what()mul(839,261) *&*where())/]when()mul(24,536)how()how()'who()why())<,~mul(254,257)^@:where()mul(945,657)!&]select()from()}'[#)mul(148,151)-#!/usr/bin/perlwhere()>%why()}!/]{;mul(549,60) +how(477,669) mul(853,888)<[/who()>}+mul(361,751)mul(837,651)-mul(8,515)(}>!!# do()&@!:^][@mul(143,286)&?^&*how()&!'mul(88,781):}who(){!*!&mul(348,435)mulfrom()?>]-}+ }@mul(33,697)do()what()why()}(#]:mul(3,176):$mul(613,679),'how(882,581)who()*mul(519,798)from()mul(865,503),+~mul(767,837)/)?^why()/mul(520,288)where(){)who()what(); )mul(274,562)$:<^*what(423,612) from()>)mul(375,282)#select()from(646,967)?(*!'$mul(991,635)mul(598,876)%^?+mul(716,646)!)&mul(699,500){^^who()when()from()what()mul(314,198)%@;mul(419,800from()]$mul(844,419)$&]mul(317,754))<;^@(mul(222,12)##:what(751,642)$[from()%;mul(205,32)when()&{>+'>why()+mul(864,418)*@select()mul(911,629)*why();)^who()mul(248,4)}mul(132,503)/when()why()%mul(441,482)-mul(818,349)(#(where();(who(22,952)mul(534,621)$'~]where()why()-mul(199,973)mul(746,506)@'select()%{mul(866,951)from()mul(381,827)[select()*}(what()mul(744,979)mul(907,379)how(347,456)who()@what()##>;@mul(114,609),}what()'mul(406,863)][who()/when()mul(900,454)<'-)->[select()!select()mul(499,399)who()mul(349,409)',-~)select()mul(234,866)'why()mul(225,989)-(-mul(626,378);,$who()mul(597,85)mul(977,267)select(){mul(111,206):(&mul(989,396)#[:)<~{who()select()mul(689,817)mul(240,827)#how(){-+-@mul(663,156)'^$from())mul(416,908)*%why(714,896)#^+mul(993,707)'when()#;who()>where()select()?mul(978,206)@how(315,762)*/>why()({mul(998,362)who()$<(mul(488,946)},when()):}?where()where()}do()[!;?~{select()mul(673,574)%[::@;why()#]+mul(455,366)$~:mul(351,347)%,~-?what(595,890)]*select()mul(186,325)where()/:why(744,171)mul(513,868)/+why(118,201)mul(691,608) %mul(469,265)why()do()[mul(80,813)*<:mul(925,31):((mul(640,344)when(758,885)where()mul(474,482)?[>{where()]!mul(66,500)why()![# -mul(549,187,how()'where()*don't()#-<>%select()?who()]mul(581,943)?]what()}?) mul(54,791)]do()+$-)mul(255,793)who()]mul(338[)how()!@mul(253,351)++who()$mul(696,214)select()&mul(899%what()~}/what()-mul(302,687)when()#&mul(842,464)}?^$when()([()mul(579,190)@mulwho()where()~mul(238,298)from()<^(+,mul(766,436)how()^)%':{mul(427,646) ?%}(?mul(995,814)#~where()'}mul(267,274)']mul(174,583[,((!what()+when()*)mul(146,807)!:where()+!mul(856,39)?;@!mul(653,577)%}^do()^mul(961,701)::mul(23,391)what()^*~-do()how()how(832,138);!}($#+mul(841,486)how()+# mul(255,21))-$who()when() -)> mul(427,236);&who(),where()+{mul(97,633) +::where(946,25)!where()do()?where()-why()mul(129,728)>^&mul(557,253)mul(535,49)who()~$>mul(342,811)([*don't(),+how(398,338)}why()<:mul(556,398)[@??:^(),how()mul(708,821):;-?where()/when()select()%?how(955,77)why()mul(428,310)how()where()'mul(759,360)(#>;where()when()mul(396,975)}{mul(131,376)(!@what()%] @mul(100,818)mul(478,394)<%$<:%($&/mul(337,545)mul(227,220)who()-'+why()select()select()don't()why():';!mul(49,915)+))[do()?[when()mul(886,984)'^ who()>when()~mul(468,20)from()(<>why(){mul(791,347)? &} +mul(389,748)/[<;when()select()*mul(370,770)::[select();what()mul(63,701)why()^mul(361,364)when(592,110)select()+: ]mul(988,308)$mul(693{from()!(how()~/mul(782,22)why()mul(355,443)}<)%how()how(){[mul(47,507,^}>)&#don't()why()mul(459,418)?who()/(how()select()when(456,391)?select()mul(451(mul(473,946))who()how()*mul(733,242)mul(575,863)why()*>&,}%!]^mul(613,249){#from()mul&]who()where()}+}}$+mul(94,794)mul(813,82)#mul(294,897)mul(995,245)!%#!^why()mul(411,763)/}what(),)mul(952,776)mul(868,728) ':~$}mul(869,784#who()don't()%@-} (mul(766,260)']who()@^&select()mul(723,930)what()what()from()-#~!mul(873,766)%:,?mul(520,521)& mul(407,520)select()$mul(547,416)~?}%^mul(677,996)mul(661,622)[~!mul(310,928)^;>-mul(740,171) /$#$*don't()select()>: &mul(306,628)*{select()$mul(705,686$/mul(803,660)mul(485,760)[select()'select(){@mul(917,878)#mul(188,324)(>}<:select()'mul(281,982)(select()},what()mul(730,931)}-#mulwhat(469,510)select()'from()where()#from()#do()/mul(869,458) +when()from()mul(912+{@mul(635,991)>}mul(158,871){[*{>'$*/ mul(686,481)mul(339,86)$>how()[when()mul(78,204)+&'mul(58,734)<{who()where()]';~mul(964,213)&select()where()@where(513,771)mul(471,186)'%$)@~({# mul(517,787){+%from()>]{,mul(762,604),~from()~+do()select()mul(827,656)$^>['-what()mul(318,236)select()'+>who(530,155)*mul(394,518)&#)select()from()!)select();:mul(805,79)why()}how()/&?mul(924,569)who()$%where()]~*&[why(621,535)mul(152,407)[why()]#mul(478,24)mul(107,697)select()'>~mul(481,432) mul(248,61)^:+don't()mul(400,374)^]{who(){mul(959,329)from(){))]mul(566,182)}what()(where()>from()&don't()select()who():+?!]]/mul(766,741)why()@ !how()who()?select();mul(470,17)/mul(374,487)%@@mul(685,185)} {<#(mul(845,259)'why(230,429)[??^select()^>/mul(791,324)$how()/?mul(478,641)+/ <{(when()mul(794,547)why()[select()!*-mul(474,709)#select()# select()}select()mul(896,775)select()-where():!mul(817,123)mul(333,359)mul(433,461)mul(722,474)@'!@$mul(49,892) #(+,mul(726,435)what()>,;mul(957,798)) #%)$ mul(541,867)-?*# do() -]who(265,168){'*>{mul(770,902)+,-who()*what()mul(21,402)mul(843,622)~#mul(604,248)how()%(why()where()[;where()+mul(342,802))from()*when())#,<#,mul(439,954)}@-&)>+what()mul(593,520)>?,what();+,select()~mul(202,526)where()+#-]? mul(654,204)>why(),(]+mul(95,133)mul(626,152)@,[+mul(811,855)when()$when(387,144)when()who()mul(781,279))+&@&mul(77,160),why()%from()do())#mul(982,99):mul(722,476)mul(757,193)from()when()select()who(){mul(569,549)]*$mul(943,277)why()@who()}{from()!when()]mul(714,885),;*do()@where()^how()mul(941,709)~@{when(215,138)how()mul(772,310)~,what()>who()!?++]mul(858,573)-what()<(from()from()mul(254,457)mul(890,367)from()why()[mul(590,183)/@who()*#-^?~mul(446,447)how()$select()mul(734,361)]~where();}when()mul(50,364):(where()-<^why()!&mul(692,682)from())&mul(949,43)mul(923,776)^/mul(987,377)!who()mul(286,329)#what()~from()^who()#/mul(13,658)why()>@(>]mul(592,423)mul(148,486)*mul(878,721)$%#how()what()mul(710,18):[#%do()where()@!(~mul(357,115)where()select()how()don't()mul(356,206)@,? how()^^what(295,729)[mul(759,861)from()*/select(334,18)&*how()@>what()mul(580,828)+;where()^;*select();what()mul(542,530)~where()[mul(549,51)'!mul(12,992)#why()don't()!;how()-(+{(mul(543,363)[what()from():mul(135,364)/select()mul(312,500)@where()[when()&$)]when(610,72)mul(215,533)~;who()}how()from()*)>~mul(522,58)@mul(402,510)!!;who()mul(867,26)-mul(25,987)?select()mul(973,76)why()&#{(/mul(148,345))^'%@when()-how()mul(233,85)