diff --git a/Advent24/Day3.cs b/Advent24/Day3.cs new file mode 100644 index 0000000..29456c5 --- /dev/null +++ b/Advent24/Day3.cs @@ -0,0 +1,62 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; +using System.Text.RegularExpressions; + +namespace Advent24 +{ + internal class Day3 + { + public Day3() + { + string fileData = System.IO.File.ReadAllText(@"..\..\..\inputd3.txt"); + + string pattern = @"((mul\((?\d+),(?\d+)\))|(do\(\))|(don't\(\)))"; + + Regex r = new(pattern, RegexOptions.None, TimeSpan.FromMilliseconds(150)); + + Match m = r.Match(fileData); + + Int32 productSum = 0; + + Boolean mulAlwaysEnabled = false; + + Boolean mulEnabled = true; + + while (m.Success) + { + // do() matched + if (m.Groups[3].Success) + { + mulEnabled = true; + Console.WriteLine("mulEnabled = {0:s}", mulEnabled); + } + // don't() matched + else if (m.Groups[4].Success) + { + mulEnabled = false; + Console.WriteLine("mulEnabled = {0:s}", mulEnabled); + } + // neither do() nor don't() matched + else + { + if (mulEnabled || mulAlwaysEnabled) + { + productSum += System.Convert.ToInt32(m.Groups["Multiplicand"].Value) * System.Convert.ToInt32(m.Groups["Multiplier"].Value); + Console.WriteLine("{0:s} * {1:s}", m.Groups["Multiplicand"].Value, m.Groups["Multiplier"].Value); + } + } + // look at the next match + m = m.NextMatch(); + } + + Console.WriteLine("productSum = {0:d}", productSum); + + // to keep the console window from closing + Console.ReadKey(); + } + } +} diff --git a/Advent24/Program.cs b/Advent24/Program.cs index 34c073a..96ddfe0 100644 --- a/Advent24/Program.cs +++ b/Advent24/Program.cs @@ -8,7 +8,8 @@ namespace Advent24 static void Main(string[] args) { //_ = new Day1(); - _ = new Day2(); + //_ = new Day2(); + _ = new Day3(); } } } diff --git a/Advent24/inputd3.txt b/Advent24/inputd3.txt new file mode 100644 index 0000000..5e64f19 --- /dev/null +++ b/Advent24/inputd3.txt @@ -0,0 +1,6 @@ +mul(498,303);when()}!(%mul(846,233)-,what()($where()how():}mul(334,117)]~>?,<,%^,mul(886,213)/:from()?-how()~}mul(343,197) mul(33,616)~%*~why()^*-from()mul(757,847){{who()why()#mul(927,553)>-?&!-@[mul(589,387)what():[?mul(865,934)#/ (why()+from()when();mul(804,792)-where(144,652)(mul(620,348);];]$mul(584,827):who()^%from()mul(381,633)why()@ when()where()?<;@#do()'< mul(643,715)@@#/>&-@?when(295,120)mul(465,37))]#mul(742,669)?mul(519,650)mul(546,337)@what(),{'^mul(769,202)^mul(808,254)/#why()@~mul(71,204)from();mul(150,335)[mul(302,220)>[$*~don't()mul(385,231)what()?*mul(852,324)#$}<{mul(838,178)<;+}@/*mul(579,121)select()why()*{{:mul(810,214)don't(){what()/)who()*%mul(273,606)&]from()@:why()@mul(788,896) }& don't()select()mul(568,713)$$'from()@+mul(390,36):@who();&from()mul(984,787)/mul(209,744)why()%[:what()mul(929,15)+how() who()@what()$!mul(751,670)$]from()mul(821,742)why()}:/why()>mul(445,950)>:mul(174,953)-where()how()+:mul(529,661)+{+@@why()##mul(513,442)how(827,318)}~}where()#@-:mul(941,152)~[,+{&@why()+mul(468,787)how()!$from()^^>%~from()mul(497,717)mul(930,672)mul(313,480)^%where()~-))#^select()mul(407,85)<#who()mul(776,808)~select()$?@,@mul(420,163)mul(666,491)$:+do()mul(823,835){who()?,mul(728,808))[who()/^<;select()~:mul(38,577)+>mul(985,224):/what()where()[%*what()mul(31,270)<>when()[?!!mul(895,903)%who()select()>/when()!mul(367,623)'#+mul(362,189)&from()'#$':$?{mul(571,203)what()mul(615,719)what(),what() )^mul(663,163)$@;&'select()mul(93,614)~(from()mul(490,261)#mul(530,933)}[from()];don't()$%what()%~'&'what()mul(134,307)}from()%;&why(65,575)?/mul(657,957)where()mul(969,590))how()when()how(){mul(534,951)@},mul(141,80<'mul(573,576)why()mul(952,460from()+#;;mul(36,336)&don't()&}how()why()select()^mul(267:+(+#'~mul(654,481)--mul(17,334)'!*why())how();-$mul(886,695)mul(954,476)where()!mul(73,181)?@mul(326,125)!@{)*mul(945,965)what()-select()~^who(884,691)how(846,544)mul(346,328)!<^~+-when(871,168):-mul(653,891why()when()how()why()mul(603,626)~+when()where(639,311),#mul(527,536)how()]%{/!-do()'@mul(368,63) +how()@]]from()select(976,708)?;what())mul(649,499)}&?how(792,889)~who()why(){do()mul(569,139)^[mul(701,8)why()!#[>'who()from()%mul(288,867)mul(706&*~>?select()@mul(819,789)(@%mul(180,466)from()[why()}&}mul(766,269)}@mul(827,216);???!mul(670,899)!-[%(<@^do()*}mul(475*mul(462,887)'@[])[?&mul(754,83)%]%(~from()}when()?mul(379,965)@why()mul(63,665)+[-{mul(543,711);>?/)(&mul(535,403)?')(mul(603,306)* when())what()-*mul(22,75)#who()<:{where(780,74)]mulwho()'#from()~?how() ,{{+mul(576,621)why()why();mul^;+^'mul(977,718)}why()>'[~ mul(179,326)#[mul(735,639)where() when()/from()$mul(779,50)don't()'<:)'>:}mul(232,847){where()mul(63,282)>mul(737,540)why()+!>don't()when() ']what()#,mul(778,85)how()[mul(809,798)![ $where()%>' }]mul(587,975)-?# }-]mul(800,437)$mul{[mul(603,103)>-/#why()mul(285,453)who();+{@ when()]don't()+[what(730,765) mul(399,137)%]select()#<[^>mul(146,316)~][from() mul(687,295)mul(347,287)when()>!from()&mul);<when()+;{^mul(267,700){~,!]? how()mul(779,914)select()&>^/%?}>-$*^mul(387,58)^who()&mul(157,49)#?/mul(35,606)when()')from()?%mul(451,146)'(where()mul(977,491)mul(916,789))&#how()how()>don't()^*mul(437,605)[how() mul(626,940)^*%/mul(884,929)select()mul(84,192)mul(111,274)what()mul(95where()![[(who()[mul(593,249)]]@#when()why()-/mul(230,390)#~],(:mul(840,931)mul(421,658)from(653,680)>&mul(34,90)>when()&}:why()mul(956,881)!when())$^]@who()select()where()mul(94,737)from()who()mul(816,357)')^^mul(454,208)$&mul(136,496){$what()mul(897,490)>'mul(376,325){why(),%]#what()mul(953,618)+from()mul(378,46)mul(138,440)who()#where() >(from()#mul(708,59)&why()mul(830,439)mul(468,479)why()<$,~},mul(279,437)%([mul(276,252)-#$mul(596,842)from()mul(873,817)*{{from()#(%^mul(765,528)!<< what())^mul$how()}/what() +:mul(411,270,,({(]!mul(627,262)/how()**how()$''+mul(791,814);-;;:mul(990,764)}where())))&((!mul(5,111)~how()from()&,how()mul(492,492)from()[$,+) @(mul(999,416)$&who(959,257)&mul(722,457)@how()) ^>;)@{mul(221,679)?who()$+{-^*{mul(241,314)how()][!mul(940,32){$(~@'+>mul(453,515*;mul(215,275)~/[,why(21,55)(who()',>mul(567,83)mul(330,717)>#!mul(611,308)+&select()(})/mul(744what(424,184)?mul(377,16)):(<[@mul(757,897)#^mul(404,155)%don't()}what(573,178)what()+mul(204,894)%mul(34,307))mul(794,115) +)from()}mul(515,433)^mul(529,684)from()why()where()?mul(956,914)(])?[mul(645,179)+mul(400,263)from()select()&^*mul(422,526){~;what()/)#don't()*%!/(mul(326,211)*%@select()from()[~&mul(456{why()from(138,423)-^(&!?mul(995,210):;when()why()how()!mul(394,185)how()!~who() ^how()'where():mul(348,18)mul(194who()&~*~&{/%mul(505,325) *%mul(459,604)>)) ~ -where()~{mul(579,481)mul(135,794)-*mul(453,355)~/](![from()mul(443,346)mul*[,~%why()-{do()/when(43,713)mul(136,26){^when()why()'mul(108,341)[@?!',+@who()mul(195]{&@{?:how()what()select()don't():]]#mul;what()&?:mul(541,941),&;-what(75,560)who()what()?how()mul(314,260)!)mul(250,620)>#mul(612,209),#?;mul(661,597)/mul(563,594)when()!:!mul(683,595)*what()when())'how();@mul(287,255)how()/^mul(345,555) who()/when()mul(681,924) 'mul(189,542)'mul(102,360)why()!'how()-?;,mul(874,55)+:!>what()mul(538,354)**]~how()who()mul(277,851),where(802,203)<{(];'?mul(268,251)~{:]mul(139,820){^^[ /;when() what()mul(775,595)/,)^;&where(),mul(644,487)?(mul(590,599what()%-who(){mul(112,571){$mul'/#mul(835,137)$&(mul(201,460)how()from()*@where()mul(934,320),[what()where())? :don't()&+'mul(66,8)~ mul(465,911)]?how()what(),;/:*-don't()#:?where()$mul(951,775)from()%@{who()who()!mul(78,456)^[from(),mul(144,347)']where()#([ @[]mul(660,565)}}~mul(738,761)-mul(514,849)mul(648,439who()+<,*/when()!why()mul(904,440)^ select())!@-mul(442,126% #?{mul(47,20)%mul(113,81)/;&+{@mul(504,712);select() mul(720,642)when()*#who()<-'];;mul(821,613)?select()!>when(){mul ,mul(597,943)$-what()*when()where()-}select(){mul(999,17)select(230,593),% mul(920,227)>where()@!~(select()!*mul(301,278)mul(538,232*!/when()mul(283,204)'who() ]mul(861,903)what()? %{* <-mul(112,180)'?(!mul(121,398)']<~/mul(363,850)])from()mul<>?!!from()when()select()!mul(551,325)[where())-:{#)mul(114}}:,,mul(341,114)from()?why() from()# mul(611,400))what()% from()}/when()'mul(874,416)'@&~+]mul(916,295what()@why()how()>where()-^mul(988,626)select()#!)who()why()mul(597,791)};mul(160,184) from()~{;!where()mul(332,928)where()(#!/usr/bin/perl+^who()]:,~}select():mul(810,730)when()mul(312;&what(415,542)who(560,710)who()from()^mul(828,831)> }how(595,281)!why()*/:mul(70,967) +(do(){,-how()<(how()[{why()mul(16,675))+don't()!>-((&what()!mulwhere()+( mul(446,260)#+&how()}}when()who()/do()[,who()]>-~from(),mul(200,312);$mul(997,25);?where(203,706)&?}select(),mul(553,783)@^~mul(779,661)%)mul(252,997)what()from();%mul(439,801)select()#?')~;%}mul?why()+!mul(385,635)@)^):!*)how()mul(563,131)#&mul(720,10)~#<+]^!:@how()mul(24,633)~mul(103,646)mul(331,217)~#mulwhy()@-$> mul(288,357)how() when();;why()<+mul(513,580)?when(299,618)!<^-mul(318,287) }mul(88,924)mul(220,403mul(264,790)mul(78,811),,(/who()who()mul(978,770)[what()$)]#:%mul mul(57,890)'(mul(146,967)[<#%)^;how()]>mul(166,279)]mul(24,319)%;select()-!/don't()>:mul(858,696)mul(622,496)*{{-~why()how()what()-mul(973%}+> mul(904,891)*(:{%mul(618,932)&!~*mul(77,889)}%~{mul(206,797)select()what() who()+*what(499,999)?@@mul(114,405)(()where()}/){]}mul(841,73)@!where(581,676)]#from(879,459)}^mulhow()do()}select()~!~)[why(364,979)who(533,785)mul(839,475)[{;>)(@what()mul(597,209)-$-^select()mul(453+;:#:?&mul(972,784)what()#select()%/[select()[,how()mul(436,10)]!~)select() mul(982,995select()~/%(,do()#who()]!,/*mul(718,383)when()>^*mul(803,383)when(); mul(568,612)/from()from()}what(219,960)from()*don't()@@why();who()where()mul(222,279){ mul(758,766)what()who()!how(38,972)~;],select()/mul(974,879)!don't()mul(368,975)where()^$[^&] -{mul(153,172)what()&+)[mul(97,652)when(534,157)~,:where()do()%%@]?&when(435,925)what(){mul(155,627)?{;@#mul(466,775)(;mul(834,395)//>()-don't() $mul(329,300)<+&'@^mul(469,14)*&why(99,525)',mul(427who()*@< ?do()[>from()when()?#,'&:mul(585,898)mul(128,392)-(?[,where()'^where(575,788)+mul(378,473how() '$'#@who()from()how()?(//mul(768,590)when()-: ](where()who()(mul(716,399)/~)@mul(754,670)what()mul(656,266):(;%!]mul(595,874)why()%@<^?mul(702,835)(![&&/+%mul(421,278)}where()-%^'+mul(842,496)/?@:]mul(282,851)~@)how())mul(543,112))]+^)mul(133,862);#)when()who()why()when(58,966),,mul(513,202)@mul(716,648)(mul(793,735)what()]where()%+-/mul(359,127)-+}/mul(116,988)when()mul(153,16):'@$who():~-mul(441,603)'how(){ #why()%{{mul(877,856)select()who(777,457)select()from())how(345,367)from()mul(447,557)%/$-select(),when()+what()mul(189,616)what()}mul(822,616)mul(454,622)how()&who()&#do():> mul(353,679)^(@select()<;mul(414,440)why()+how()&,]#-&mul(402,532)-})~#$-!mul(883,415):<@]*from() mul(28,253){where()? what()select()~mul(259,972)-(]<}>when()from()$mul(650,381)~from()}from()who()>mul(72,307),who()where()?@{where()mul(706,447)from(671,711))~mul(601,925)} ;what()[mul(533,851~{&what()(@{<'where()mul(799,568)%where()mul$>select()[)#^:mul(414,93)]mul(16,757)&[who(),/mul(629,760)&''how(),from()mul(951,329)^mul(781,711):/',+'{%when();mul(661,844)/why()<^}#mul(337,134)!)mul(339,829)([how()+&~how(512,132)!;:mul(696,374))}/what()from()mul(640,944)'{*how()what(279,739)why(843,673)? &!where()mul(347,404)^?>!$&mul(316,524) when()]mul(875,937)$(@]$don't()/who():/]mul(833,846)who()mul(190,362))mul(183,740)how()}mul(14,502):why();why()'mul(341,888)mul(668,734)]/what())**&(do()how()*:select()}*:]mul(604{mul(123,983)#&why()mul(159,440)mul(437,830)~};'])[?mul(36,75)mul(836,766),?*select()who()mul(548,488)%--where(521,841))$]do():-from()*mul(355]<+>:mul(58,505)what()from(49,769)mul(108*]~>mul(131,644)?!from()mul(11,884):where()!;select();+[select()mul(358,741)(*?]do()(?{@^mul(117,14)^+;what()from()}mul(847,203)/who()where()from(567,960)when()how()%~'}mul(677,415)mul(91,507)when()*when()from()*(mul(935,477) from()@ mul(556,85)?)@mul(196,674)^;*+#$mul(531,124)!what()%select(){who()}';~mul(67,663)when()'?mul(205,527)@:+ ?:why() %mul(513,956/}:,'@+$who()-mul(963,914)@from()why()how(292,305)!from(980,892)mul(456,909)!who()<(*@select()&why()mul(947,805)who()how()^}mul(154,935)+?when()('};]>^mul(855,799){who()]+*{mul(823,352)*<$when()+)^&(do()mul(623,916)(['])^what(){select()mul(815,135)}mul(464,34)' ^)-^~where()where()mul(50,471)*{select()from() mul~:why()when()[why();%, }-where()#mul(184,632)~]+'select()}what()mul(675,164)mul(360,852)mul(875,801):where(811,851)mul(702,746)mul(29,632)[{^]-:!{ who(719,324)mul(720,687);:/[#*mul(431,415)mul(123,914]&?%$how()mul(655,184)]@?how()*$(-what()mul(323,687)mul(474,75);mul(846,335):%;do()~?where()~mul{}?%mul(446,300)^how(),<&from()#!#from()mul(423,746)how()what()don't()what()+*#:mul(268,185)@do()!>?~;@]mul(657,833) ,(who()-who()mul(415,876)]&#?@'!/&mul(437,971)^,+mul(884,392):mul(183,497)^from()mul(204,743)^'>}who()$;}mul(892,574)+ from()?$mul(922,854))select()#,+>where(),*mul(551,303)?}mul(261,657)?why()}why()when()%-mul(124,265!mul(979,380)/@select()}>why()+#mul(384,303)$,@@-mul[mul(479,548)-]#> ?:!do()how()@&mul(917,657{:$&mul(966,387)}what())[-]mul(183,849)^^who()&<,mul(669,375)mul(323,459)$%/from()~+}when()mul(789,390)'+mul(804,927)#who()mul(102,842)$$)how()from()select()][mul(380,252) )>when()where()how()who():#where(184,552)mul(244,943)$&$*}*-mul(727,613)(#why()}>mul(807,274)mul(577,489)mul(731,223)>where()/!]? %where()mul(359,604)/$;$+}mul(182,17)why()-how()mul(792,962)why()/{-(how()%'select()mul(997,718)/{mul(7,739)select();:mul(800,703)[when()' #~why()where()*mul(345,374)mul(308,927)how()%@how()mul(233,785)#{!select()mul(208,457)#from()}what()mul(347,911)where()from()@;(mul(492,896)&;?+, mul(948,682)+~when()}from()mul(930,691)[&]*((#mul(600,425){what()'{)@,where();:mul(844,671)~+)#,(mul(446,155)$%@/:mul(749,494)mul(24,97)where(6,997)&where()?why(449,637)mul(631,323))mul(192,700));/$]what()mul(820,765)who()(>%>mul(702,770)*-[;}(mul(438,245)@)]^/'#mul(554,294)!mul(34,410)where()$<#how(360,499)@#mul(585,680)<^};mul(295,437)where()] {+mul(607,28)why()#% do();mul(96,932)]why()+who(){<,[where()mul(271,645);^:{mul(411,161)]<>do()@';when() (mul(41,299)(!what()*select(387,698)$]mul(942,465)^?~what(),*?-'from()mul(70,415)why()'from()how()mul(682,699)!^-when()?when()#mul(473,999) {(~>^why()}mul(240,258)+why()&%mul(579,916)!(]((select(112,996)#($,mul(584,139)%