From 9cce383da7eeaff7f2c1adc6f4683b5424fb026c Mon Sep 17 00:00:00 2001 From: Andreas Feldt Date: Tue, 3 Dec 2024 08:22:35 +0100 Subject: [PATCH] Added day3 solves --- day3/input.txt | 7 +++++++ day3/solution.py | 23 +++++++++++++++++++++++ day3/solution2.py | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 day3/input.txt create mode 100644 day3/solution.py create mode 100644 day3/solution2.py diff --git a/day3/input.txt b/day3/input.txt new file mode 100644 index 0000000..58dc3d8 --- /dev/null +++ b/day3/input.txt @@ -0,0 +1,7 @@ +^mul(721,815)who()&[how()mul(990,467)select()why();what(327,762)?@mul(636,928),[!#$??:+do()where()@%>@&],<}mul(766,598)mul(117,278)where()$mul(469,793)[~]where()mul(884,216)mul(795,723){what()mul(184,793)when()!*&mul(87,768)(mul(436,871)>>?::why(645,65)~/why()why()mul(761,50)'&-: &;mul(450,66)>what()-how(244,505)}mul(737,574)$where()who()!select()mul(828,224) %/((mul(612,706)[mul(56,369)<+when(871,669)+when()/^#when()]mul(33,835)when()$what()from()'when(575,599)mul(98,444)(&from(399,328)]mul(149,861)%who()mul(493,109)+#(how()!$}-where() mul(992,42)when()>mul(966,422) {mul(128,242){$,when()-what()from()who()where()/&what()mul(111,554)-+don't()}}/(^(&+mul(130,716)[+!/mul(585,272){how()/:who()who()where()mul(638,890)>mul(892,569) -/;mul(365,771)mul(703,962)mul(891,916)!why()[{:!why()!mul(85,405)^?$what()where()where()~,(select():when()mul(102,589)who()]#when()^do()$when()+*mul(433,264)!%where() mul(676,814){who()mul(791,825)?mul(69,953)}mul(348,556)##how()what()who(){[>?how(151,194)mul(346,158)mul(698,833)>(:mul(249,39)!{<{//~when(){mul(686,621)what()mul(516,43)*who()mul(326,716)select()do()^#$/&mul(692,750)when()&[?->{mul(218,8)&&mulwho() $where()}{}#mul(820,744)/mul(230,927)^ :where(351,105)~/(;mul(900,172)$]{?$#where()mul(652,481)<}don't()*where()select(200,394)select()#who()mul(650,602):}when(552,753)-when()how()'mul(349,401),&how(){'mul(989,165)%$* ,,&mul(906,532)mul(701,543)when():?#what()~+how()mul(113,743)[$;select()!how()mul(68who()mul(153,928)%()^[from()mul(555,552)@select(522,482)mul(295,900)where()/mul(189,189)+'&mul(490,144)++/&from():why()+*do()how()[~when(612,413)-when()?do()}who()mul@/why() {?-%%mul(126,29)@+from();mul(548,673)mul(51,970)--}what()~,what()(mul(261,780)@;'why()>;what()mul(843,665)when();@mul(575,431)]:;>{#mul(123,103);]where()where() mul(331,417)?+select()}when()mul(730,923what()who(514,796)where(716,633);when());why(122,722)(mul(923,885)where()''#'where(645,705)}#select()!don't()where(),,;!}&^mul(609-'(mul(245,580):mul(898,814)how()+mul(999,143)),select()mul(251,507)(%<:who()why()how()/%mul(192,864) +*,/~):^[mul(802,939~;!:@mul(656,831)}]?~mul(92,389)/:who()&why()'mul(698,92):who()]/< @@@mul(956,581)who(252,379),#&,%mul(315,398)mul(518,43)@, &/[* mul(344,100)how();mul(826,831)when()select()++'mul(175,965) [ mul(786,297)from()how(520,671):*?]&%]mul(647,834)why()when()select()/mul(585,934)!@<&how()!/mul(965,795)!,@/how())(#{mul(701,685) mul(51,440)/)mul(382,190)@why()how()>>#+;%{mul(808,654)#*mul(128,793)%:->mul(397,504)];$]from()mulselect(533,968))>why();^mul(661,310)mul(415,957)}?&%#@(mul(261,4)*;mul(292,885)!mul(639,791)from()how()when()mul(451,10)mul(853,231)+~+']>mul(543,437)mul(218,441) *mul(447,927)why()from()mul(178,473)how()<*why()[&how()how(),mul(139,541)>~why(644,46) who()what()+~(mul(888,667)^from()&^when()^when()-where():mul(585,185)>from())]+mul(766,787)mul(16,587)%@mul(475,198){^mul(353,741)&~]$<;]:mul(529,533)where()~mul(980,696)how()when()/*>when()#^don't()![]#[*%-mul(315,512/when(57,948)?why()mul(107,632)%>&who()[mul(554,91):from()!who()?how()why()%#when(313,134)don't()mul(694,300)/~(,+who(382,968)}from()mul(69,889)')why(768,374)+how()mul(338,324)$select()}] select()mul(572,647):from()mul(12,530)mul(651,890)mul~@?mul(841,284)]%^where()mul(201,525)where()(do()&@}]how()<^who()]&mul(166,168)>;mul(419,733)!mul(197,692)select()+>-@'&{who()mul(936,290)select()/when()#(/when()mul(201,918)~,how()>&[@<}'mul(400,452)<&[[%^select(131,478)$where()^mul(31,294)@{who()mul(275,538)^]@;}]&^mul(362,151)~mul(840,788(,/;&why()^[[don't()mul(615,413)where()$mul(404,421)select()@,when()~+%}$mul(157,970)$mul(846,324)what()mul(449,574)-mul(517,831)* +when(478,401)?]mul(991,421)@%mul%select()%who()mul(970,585),&[?/'?~:/mul(831,536)^]how()>mul(577,864)^$+mul(391,353)who()what()@${mul(144,420)])why()* !:mul(595,206),select();when()-)mul(723,270)+!*~^]where()mul(629,243)how()(mul(651,134)'')$)why():'$mul(488,642)mul(975:#from()^?%mul(663,263)#+!(from()/;why()(mul(4,649)mul(520,137)*'>mul[?where()-:%;mul(647,34):-what()why()select()-mul(549,57)}mul(917,540)where()?why()when()why()why()/$!select()mul(938,743{mul(61,74)+@mul(405,137)$who()mul(937,685]'^mul(965,910)}what(838,413)mul(975,550){}+?++<'mul(928,26how(128,389)'?>#who()<-select()do()%[how()]select()@[ >/ mul(114from()}*what():when()#when();mul(613,512)where()[~~;@(how()^mul(46:where()($mul(773,236)^*from(101,73)$mul(127,952)):?,:<-mul(12,239)mul(683,676)how()/[#mul(650,459)#;'mul(252,579)?from()@[do()mulwhere()]!,->$]~>mul(657,580)select()$>+;when()>mul(983,621)*from()}how()/]#!<{mul(135,869)!@:what()select()}#mul(947,363)mul(267,51)#mul(178,200)mul(131,552)^(why()mul$-+/~-~-^[who()mul(763,94)&(]% ~^/>mul(820,422)&,select()%: !mul(27,886)select()what()+/}^@what(),mul(704,766)*[why()why()mul(358,218)-&>when()mul(432,681)?% ?mul(502,580)]{::'*!]mul(237,998)how()++where()who()from()what()/mul(897,575)how()how()+?&>}mul(696,285)select()] {/(+mul(340,384);mul(164^from()-%~{mul(283,522)&+'%~- )}where()mul(237,601){! ~mul(19,553)/(#,when()-from()from()!mul(933,578):(@#^from()#>*mul(296,919)from()what()-mul(239,343)~$when()mul(184,945)&!?&+!#when()why()/mul(814,466)@how()where():why()what()%/mul(498$!where()mul(121,233)+<)~select(89,323):;}where()why()mul(293,662)[from()mul~}mul(96,122))when()who()who() ':(}how()#mul(303,846)+&^%;]select()!why()mul(856,3)$'where()how(),mul(985,407)~mul(75,217)>%@({@don't()how()mul(601,409)]$-}[^@&don't()@>from()mul(313,975)!$when()-;<~+~mul(273,261)[-(mul(849,769)*select()how(480,586)from():+)mul(263,796)%what()why():!{mul(732,962)@mul(541,715)who():)++-?~select()mul(272,709),%how()&from()where()mul(128,863)-%>mul(66,521);what(),&~@}+:{mul(683,867)^mul(967,554))+(mul(163,187<:}; where(149,753),-!mul(454,102)]from(581,325)^>^how()?select()~'mul(612,695)>$~;?from(335,390)>;<(mul(158,809)mul(994,415)why()^who()('mul(906,133)[:mul(454,783)select()}/ mul(496,587)&$[)]<*<^%mul(916,337) *where()(,}from()%mul(781,945))^select();why())mul(966,406)!$+(why()}<^mul(118,864)/how()mul(946,437)#{mul(723,269)%*&*^when()$mul(337,544)*-select()!when())]what()[$mul(624,678)&;who()what()how()$@mul(41,821)mul(860,521)&-why()from())@^where()'mul(627,836)?>;?,$mul(862,746)when()from()&~$who()select(171,606)mul(325,203)?from(569,805)what()%select()]mul(316,107)?~&&from();^}&when()#mul(229,118)what()what()why()-* +%;mul(635,909)mul(965,642)^mul(942select()]{mul(793,836)@/+why()*>/(mul(453,491)>'^where(){select()select()%mul(628,54)*>]?select()$who();mul(508,795)}%!%>mul(823,280)from()%mul(247,633):mul(779,419)/#^*<}?mul(921,457)where() -where(439,307)?+/how()+(mul(231,858) :mul(205,634):[how()?-'^<'mul(160,480)%!)(what()why()^^~,mul(276,727)when()^[ ##select()mul(382,146)where()when():?^[]%-mul(976,132how()^-&how():-)!#/mul(25,564)!(when(464,814)how()when(440,219)who()[~where()mul(956,412))]@select()who()$~']'mul(507,562)&}> mulhow()$/ &&,(<-}#!/usr/bin/perl^when():{+^('how()^mul(452,533)@where()?$mul(442,913)how(){]from()select()$how()select()from(),mul(484,240) +&mul(444,854)when()mul(774,478)where()&&^ mul(922,900)how()how()]+{what() mul(606,127)from()]what(){[what()mul(721,275)mul(235,360)(%'what()*)mul(572,460)'mul(48,7)who()],where()from()>>mul(593,878)where()%<>&mul(820,458)from()&select(271,971)!do()where():>-mul(355,304)from()(;why();[select()}mul(805,242)+![select();mul(775,789)~mul(628,350)$$from()@(*how(){'mul(960,498)why()<>select(440,141)(mul(670,865),>what()+::mul(208,606)mul(329,619)$don't()^from()-where(651,585)how();,how(753,435)>'mul(198,179)~mul(687,19)#->/do()+%$]^mul(36,106)%where()who();)mul(146,12)^/-mul(957$mul(722,483)(^where()mul(791,915)&+^who()!do()}>'%:^:mul(419,941)/don't(){when()+when()~*(^mul(595,305)from()why(),/how()where()]}mul(819;^&what() ]-where()&who()>mul(335,130)what()]where()[/where()what()?;mul(193,281)~,who():+why()mul(346,672)){@do();^>select(629,407)where(){^/!'mul(523,137)]&select()$/?/^mul(757,373)don't();when(648,805)mul(937,925)where()mul(52,179)?*&}>;mul(820,72)from()mul/}from();&!when()what()mul(341,113)%when(),/+@@mul(282,840)%what(845,759)?/%?who(38,906)mul(790,88)select()when()(-}&select()/mul(460,748)}#why()how()^}($from()mul(736,446)#$/don't()@*:<$mul(647,695) where():where()who(){mul(124,189)}:~@mul(567,682)from()''mul(537,138)mul(126,676)>why(){how()/;select()what()mul(211,918)]@*%where()<;;!mul(367,207)',?mul(699,852)what()>,)~mul(223,597)+what()how(556,77)+don't()/@/:[{-mul(610,646)(why()who()how()*}mul(388,590):why()!mul(777,494)[who()+mul(332,45)!:when()mul(107,845)who()(]from()select()mul(518,311){*mul(75,698)(/!~#@$where()!,mul(55,490) :{where()mul(681,43)}~>]--^:$]mul(55,980){;mul(443,44)@mul(750,251)from()$$&#when()>mul(979who()!how()mul(504,318)mul(402,389)[!#@why(525,210)+mul(984,318)when()how()}%/mul(221,803){who()how()~$mul(88,963)>}$&from()where()+]from()%mul(38,316),mul(207,135)who()what()what()+&mul(627,85)]: &?why()?*--mul(79,741)+(?~%!!mul(51,132)where()@where(994,962)mul(691,851)from()+why(): {[mul(543,495&#how(){(;how()['mul(352,179)/)+ /~mul(250,222)^+<} mul(719,257)%from()why():^?!^[don't()(,where()$mul(894,942)how()!(where()$mul(506,810) from()}%@)**mul(261,233)mul(179,77)>$mul(248&why()%where())+%}<,who()mul(920,816);mul(384,108>mul(954,663)mul(11,936)who()&%{:mul(698,682)who()select(903,689)who()from()-where()/+mul(332,753) where(){?how(52,910) )(what()mul(580,139)[[~,('mul(762,762)from()~}- ~*^[mul(348,345)%*mul(776,555)how()/when()-!&:+mul(354,880)when()%<>;mul(121,687)-#:]when()why()/::mulwhere()from()mul(353,738)@&}#don't()mul(888,259):{[ +*mul(457,716){from()mul(784,718)::^mul(34,955),from()select(545,368)%!]?/mul(795,164)-?)why()how()'%'[who()mul(754,424)from()-select()from()^mul(772,122)where()where()where()select()?where()%-mul(849,64)what()!$'&++mul(557,560)!? :]select()(mul(824,973)>;?{(mul(333,526)why(),mul(814,560{from()#mul(860,603)what()]{where()select()mul(209,175);when()>where()mul(242,542) [[mul(563,973)-mul-&$;)~why()$%why()mul(443,590)?[<;when()don't()what()where()}where()what()mul(178when()from()where()where(244,944)mul(674,125)'mul(709,133)'<):?&why()&>%+,mul) , !-mul(160,387)~&^mul(725,120)who()[]mul(512,611)select()/?mul(617,372)select()>who()[(mul(802,268)-)*&:mul(815,27)]what() )#-,%<&mul(429,97)}!&mul(106,545){~: when()+,mul(438,161/>--mul(818,804)<))don't()[>mul(602,506)mul(55,393)*[:mul(157,531)^don't()?{+mul(554,938)*;mul(385,567:who()>how()&+?*mul(574,353)mul(984,83)-how()-mul(872,273)((mul(517,792)?select()why()^mul(730,625){{'mul(83,516)when()((mul(130,817):mul(519,651)]&/why()!mul(836,209)[%,[mul(213,670) )what()>;@!mul(611,517)>]mul(197,385)@mul(68,987)mul(162,106):when(7,896):mul(796,638)>^!/'>mul(845,941)/where()do()#:what()what()?~&?mul(806,128)]%why()mul(348,883),'+mul(336,235): 'how()mul(855,266)@$^why()mul(742,387)who()!mul(995,906):]]when()<)*from()(&mul(764,180)~how()@# :mul(707,290)#/{@mul(65,579)-who()where(960,596)>*(don't()from(766,430)where()mul(75,404)(when(457,596)#&mul(612,799)#)where()mul(401,474)what()%where()#>+mul(31,289)&>)?/(-mul(189,149)>~,when():mul(985,763)$&who()#%!mul(804,329)>'mul(229,945) don't()who()who(84,291)*select()why(),;,?mul(470,948)from(681,767):*who()&%mul(850,581)what()>(#}%}mul(636%mul(641,599)mul(184,164)mul(575,439);({{mul(738^--^)%mul(60,730)where()^]from()who()-/:what(848,902)mul(958,89)},where(148,634)-mul(371,786)when()?mul(151,757)select(973,278)!)select()]'who(130,338)):mul(644,833)mul(480,246)what()mul(305,497)^mul(233,355),}who(563,221)>who()where()/when()'mul(412,498^how()-$?what()&[select()mul(179,439)&where()^{#[%mul(510,163)when()where(353,632)':mul(629,922)+%}:+how();@how(103,365)}mul(961,420)~select()*~:#mul(914,166)who()/!why()mul(392,362){<@what(){from()select()why(917,470);why()mul(217,122)~}what()where()$(}mul(422,535)(*how()}&mul(777,997)&[where()]mul(664,558)!/+)*{ why()mul(308,60)~[*how()what(){,!from()where()mul(234,662)*)+who() who(){]{mul(638,389)select()$%;}~how()who()mul(223,862):what()mul(150,399)mul(566,885)mul(231,725)[/mul(230,942)')-:)who()'{where()^mul(400,853)why()@[from()where()&[@<^mul(325,706)+-{mul(115,2)&&?*{mul(954,823);what()mul(600,695)when()how()when()*;:[-mul(78,362)#select()/$(>select()$^mul(981,64)>mul(899,724)what()}who()mul(94,857)@$&<:)mul(142,322)&&;/?*who()?where(315,764)mul(169,174)&/%('mul(340,253);@)[select(607,401))select()mul(683,991)&:how()select()[<#how()mul(186,106){>!(*mul(490,980)(]))from()]where(84,603)mul(342,395what(){)&];why()who()what()[%mul(751,371)who(720,254) why()when()what()}select(455,617)'mul(53,661),:/>:!mul(446,454-?what():$[[+mul(498,205)>(mul(759,494)mul&>?:^'} mul(867,709)mul(605,504)what()[-!}!mul(106?>mul(239,94)+>where()#+! mul(618,663)&--':%mul(57,203)/![:,$select()mul(614,15)^]mul(570,897)what())?who(812,559)/*don't()}mul(150,813)&+what()]~!+mul(999,729)[mul(110,688)>:select()@>select()/,who()}mul(22,21)how()~^- $+#from() mul(45,829)don't()([why()+-:>mul(488,321)&*-mul(652,853)>when()^ how()where()select()*mul(468,965)++mul(781,52)why()>+~^}]mul(268,658) ::where()>'mul(233,327):select()?}mul(269,680)/{+>@:(select()$how()mul(322,587)/;mul(406,648):*who()/mul(53,8)don't()::mul(340,161)/?>/&don't()select()*>mul(315,698)]who())how()[,how()how()}mul(987,159)&!mul(979,457)~[-where()#mul(381,476){from(261,299)from()};^,where()from()?mul(681,344)*+when()why()@;(#&don't()~ ,select()where()mul(239,813)!mul(808,143)what()(>select()!what()what()!,mul(494,477)do()select()how()-+where()from()what()!mul(797,39)>}}}+[(^why()how()mul(393,474);{mul(159,285)!how()[}mul(936,615)]mul(311,234):>)why()!mul(70,105),[]mul(143,155)?who()/ +(when()select()~mul(225,35)/^!mul(637,97)~!&*#+<$mul(886,753)what()!]what()(mul(513,884)~how()-+]mul(906,496)+!&where(){>' ;when()mul(302,264)@!~/where()?what()$do()>$who(391,417)what(106,572)mul(97,315)'-mul(211,823)},/mul(460,903)!;#{ }~'mul(264,775)mul(913,491)-when()~%[how()/}?%mul(482,242)@+<:from()$'don't()~;mul(396,591){from():where()]~mul(525,827)when()#why()from() #%*who()mul(407,541)*mul(484,568)mul(62,785) what()@what()when()mul(908,348)%>]how()mul(531,232)]how(827,75)>-don't() ??++mul(7,189)#do()who()mul(661,112);who()'--;mul(793,568)who()select()-~{mul(937,614)mul(775,294)<'when()~^;+mul(140,643))+<#' + diff --git a/day3/solution.py b/day3/solution.py new file mode 100644 index 0000000..4c0dbbd --- /dev/null +++ b/day3/solution.py @@ -0,0 +1,23 @@ +import re + +example = """xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))""" +input = open('input.txt').read() + +def solve(input) -> bool: + find_all = re.findall(r"mul\(\d{1,3},\d{1,3}\)", input) + + print(find_all) + + total = 0 + for find in find_all: + a = find.replace("mul(", "") + b = a.replace(")", "") + c = b.split(",") + + result = int(c[0])*int(c[1]) + total += result + return total + + +print(input) +print(f"Total: {solve(input)}") diff --git a/day3/solution2.py b/day3/solution2.py new file mode 100644 index 0000000..d8d1c91 --- /dev/null +++ b/day3/solution2.py @@ -0,0 +1,23 @@ +import re + +example = """xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))""" +input = open('input.txt').read() + +def process_instructions(text): + mul_enabled = True + total_sum = 0 + + pattern = re.compile(r"(mul\((\d+),(\d+)\))|do\(\)|don't\(\)") + for match in pattern.finditer(text): + if match.group(1): + if mul_enabled: + num1 = int(match.group(2)) + num2 = int(match.group(3)) + total_sum += num1 * num2 + elif match.group(0) == 'do()': + mul_enabled = True + elif match.group(0) == "don't()": + mul_enabled = False + + return total_sum +print(process_instructions(input))