diff --git a/aoc.cabal b/aoc.cabal index 8152520..91a97b0 100644 --- a/aoc.cabal +++ b/aoc.cabal @@ -41,6 +41,7 @@ executable 3 -- other-extensions: build-depends: base ^>=4.18.2.1 , split + , regex-tdfa hs-source-dirs: app default-language: GHC2021 executable 4 diff --git a/app/3.hs b/app/3.hs index 53e9db6..db44e98 100644 --- a/app/3.hs +++ b/app/3.hs @@ -1,15 +1,32 @@ {-# LANGUAGE LambdaCase #-} module Main where -import Data.Functor +import Text.Regex.TDFA +import Data.List.Split -parse = undefined +e :: [String] -> [(Int,Int)] +e = ((\case + [a,b] -> (read a, read b) + err -> error $ "Parsing failed on: " ++ show err + ) . splitWhen (== ',') . init . drop 4 <$>) -solve1 = undefined +parse1 :: String -> [(Int,Int)] +parse1 = e . getAllTextMatches . (=~ "mul\\([[:digit:]]+,[[:digit:]]+\\)") -solve2 = undefined + +parse2 :: String -> [(Int,Int)] +parse2 = + let h [] = [] + h a = let (t,t') = span (/= "don't()") a in t ++ g t' + g [] = [] + g a = h . dropWhile (== "do()") $ dropWhile (/= "do()") a in + e . filter (/= "do()") . h . + getAllTextMatches . (=~ "mul\\([[:digit:]]+,[[:digit:]]+\\)|do\\(\\)|don't\\(\\)") + +solve :: [(Int,Int)] -> Int +solve = foldr ((+) . uncurry (*)) 0 main :: IO () -main = readFile "inputs/3" <&> parse >>= \i -> - print (solve1 i) >> - print (solve2 i) +main = readFile "inputs/3" >>= \i -> + print (solve $ parse1 i) >> + print (solve $ parse2 i) diff --git a/inputs/3 b/inputs/3 new file mode 100644 index 0000000..0e68180 --- /dev/null +++ b/inputs/3 @@ -0,0 +1,6 @@ +^+'*>,,why()mul(229,919)&$-#^~mul(187,600)@[#' *+&mul(388,743):~^&;do()when()&^&^mul(415,678)>what(180,378)when()/)!#how()~&do()(((]how()[~{;what()mul(792,328)[;(,why()#mul(767,729)(what()@-why()}who()how()where(373,159),mul(91,503)select()~;where()@+;;++don't()mul(766,411)~'&%what(217,603)>why()mul(528,603);how() &who()mul(418,950)-select()mul(440,425)mul(42,798):what()[^%mul(28,566)from()<%>]//(*mul(121,164)+/)@}/why()([mul(94,107)where()why()$what()} ~when()who()when()mul(51,763)&:$)mul(117,974)*%?]%;who()>mul(224,518)@/<*,}mul(681,258)(%{#select()&*who()where()mul(301,302) don't()who()][!from()>mul(410,170)do())what()##mul(873,748)$~}mul(797,721):when()how()[:-where()>mul(959,163)mul(992,12)%/)$why()*mul(141,535)how()mul(681,821)'&how()],mul(123,886)+#don't(),>[mul(119,227)>!@*when(720,847)%where()+what()mul(525,607)*+&mul(918,604)where()'$]>mul(138,66),';@!where()mul(251,314)[;/do()who()mul(843,719)?mul(193,439)why()/;don't()/::mul(677,473)($*[@mul(729,623)~+from(),(how();when()mul(496,695)+):mul(403,498)mul(723,890)#from()when()*why(153,516))why(364,80)who()+!mul(723,971)mul(805,935)#$@+!{@#mul(110,950){mul(161,461)~*/;,/mul(115,959)}:how()':,@'what()mul(816,844)mul(568,840)?do()[,how()*mul(601,790)where()select()#+select()why()mul(27,586)(}how(570,902);where()?+]-mul(425,554);how(812,935)where(),what()%&^mul(623,585)@ why()mul(995,90)#+%'do()>~#}what()mul(374,323)mul(249,230)~,>+select()when())mul(741,526)%{{mul(373,937)when()mul(523,799)mul(438,812)how()~}<{select()where()(]who()mul(774,128)what()}:$!$%where()>(mul(376,393)+where()(&)*[how()!mul(388,253)#-when()#>$,@ mul(37,722)how()mul(272,60)~@&% why()}mul(806,354);}where()how() ^mul(707,617)*when()*[{$where(191,569)]when()mul(475,36)'what();-] mul(409,381)select() do()};mul(106,765)@mul(962,693)select()@+where()how()select()%[mul(594<&(mul(838,166)]from()[,how()};>[,mul(407,924)mul(305,306)'+,]mul(755,425),)don't()mul(119,487):mul(143,902)-{'from()*,>,>@mul(378,307)%,mul(204,648)(:mul(812,89)$*+*,##mul(397how()[]@mul(593,676)&#why()what():mul(561,33)*do()$}''!$)>who(955,135)mul(428,61) what()@!from()[+::where())mul(242,136){why()!how()why()mul(789,485)':mul(950,684)-mul(734,60)!+'where()(mul(897,715)why()(?mul(344,613)mul(945}/[$mul(354,310)why():*!mul(162,884)when()why()-*}<)%mul(999,479)%how()^?[mul(549,221)?mul(10,487)mul(473,495):from():how()+@mul(409,356)@where(424,972)mul(245,522),^mul(830,352)^['select()+?mul(348,779)^)where()+ {select()mul(631,610)from():+!mul(364,859)from()+,what()-/*who()$mul(381,350) +select()#'mul$%({]!how()^mul(832,589)' (>?{:when()}mul(558!&mul(315,787)what()[when()mul(331,479)^#<:^!when()mul(705,411)from() :from()/*}{mul(173,420(~]#mul(812,264)why()mul(170,516)!#:;mul(115[mul(41,851) %when()?)select()}who()mul(766,808)when()mul(358,761){where()'when()}[$;^'mul(170,223)who()who()mul(990,347)))}^[[?select()}mul(540,811)%mul(384,379))*+ who()&from()>)don't()why()!(mul(443,371)when())??do()&'!&mul(836,469)+},)mul(661who()'select()~%why()do()mul(794,923)!select()@!'mul(805,982)!~)from()mul(388,599~who()):mul(638,751)(>'mul(443,735)how()select(){)what()%do()when()) what())mul(272,487),#select(),when()do()][]what()}%mul(28,452)mul(178,145)mul(396,673)};%^@where()why()mul(648,926)^do()why()select()where(245,854)from()^&;mul(960-what(446,263)mul(810,6)*>)#@&mul(211,423when()mul(937,149)*from(288,317)mul(103,75)/'!:& mul(788,232)what()mul(52,428)from()?what()*{@:(why()why()mul(195,781)*]]$who()>don't() /how();'mul(610,736)mul(852,398)mul(690,890)(]~~mul(905,20)*how()@ *}%]don't()%when()++,^how()mul(630,398)[select()who():?(how()*&mul(344,752)don't()+{#%?select())^mul-&what()mul(651,800)}'[,(mul(528,222)[where()~/mul(341,14)what()}[when()%!what()mul(781,771);/:why()^%mul(609,662)who(),{+&mul(412,515)/~don't()?+ :@?} mul(453/^mul(581,391)select()from()(mul(896when()from()from()#{!mul(953,253)!select()~?&from()mul(656,409)how()who()~when()@''$!mul(139,643)how(776,270)from()[&<'mul};!{how()'!mul(534,208)why()do()-$*how()where()^when()!mul(421,915)who()~#+mul(143,731)don't()-/-,>mul(637,233)-mul(730,106)$mul(416,413)@^;~> what()~mul(382,92)}&)when()how()mul(127,684)}':how()where()mul(20,220)~how()select()}when():* mul(243,419)mul(352,910)#who()&#when()do()when()'!^#why()!@>mul(695,556)>who()*mul(397,920)}who()(;[##do()~mul(773,279)*}where()%}~*mul(643,570){mul(105,506)$~who()~^@don't()select()(%(;^:mul(229,887)@#''[how()mul(788,362)>(@mul(723,439)-where()where()what()from()?~~mul(843,893)mul(474,427)why():];(do(),{'!^(mul(15,142)mul(876,153),,-$?select();[{mul(664,520)-where())where()}@&?where()mul(785,595)from()when()>:mul(888,328)~(select()>how()?what()mul(633,692)mul(173,839)'mul(204,278)!+~,(~@mul(451,395)mul(785,151):where()]~^(when()from()do()who()mul(790,654)'&>where()when(710,794)from(47,899) who()&mul(806,984)@where(65,953)@!-#what()/who(634,853)+mul(861,224)[()who()!~mul(899,370)mul(549,105)%(-why()'~mul(725,344)]mul(41,164)?,*^@ }):mul(955,760)]>?from()>+-mul(827,250-mul(50,448)&&mul $>mul(315,814)&mul(194,503)>how()%/who()who():/>@mul(223,541)from()(//what()*what() mul(364,778)why()?/$mul(47,912)from();why()how()from()when()mul>where(){mul(693,8)',;where();:mul(528,520)~}+&**:who()mul(401,20)[mul(274,431)from()where()what()select(930,392)' ^mul(733,66)mul(463,265)why()>from()who(553,550)why()^%&mul(225,388)'!#!@mul(593,40)/@@!*,mul(773,292))$/%where()@*^from()mul(910,722)mul(983,696)where()^^~why()-what()where()don't()!{;what():mul(216,52))}#mul(105,130) ~when()>+#, ~mul(26,766)([?+!mul(178,420)**mul(317,607)where()how()[mul(654,90),mul(931,884)#why()/when(){mul>^?!mul(298,763)%mul(355,538)]mul(43,357)mul(810,34)from()why()}select()'mul(33,953){mul(459,607)what()~]$why(30,320)mul(266,245)[why()-@who()$}{mul(322,346)$!)/mul(418,539)mul(678,561)mul(800,662)from()-];?where()when()^mul(648,356)->@}(mul(891,938)mul'&#(*from(265,14)mul(707,267)#&mul%+>#~%mul(50,976) ^%(where()!why(315,655)'from()mul(33,958]<{'mul(927,525),>when(597,620)#mul(365,283)>{# !,who()what(730,791)don't()')mul(2,790):how()~/*;who()mulwho()^&]%+{/%/mul(884,224)where() &who(837,688)?where(),['&don't()^>{?)/,]!&mul' select() (:*mul(113,260)what()*%;where()mul(614,724)%mul(718,620)>-~{}why()]mul(416,163):/{ what()where(913,318) !mul(205,361)mul(412,549)[[who()('}why()why()[how(530,659)mul(721,436)]what() @**mul(573,713)&}%@mul(201,630);;do()#select(),what()mul(448,64) %who()^@$mul(679,387)where()]>)!-mul(486,701)who(996,660)?mul(559,794)mul(627,726)what(){;how(){mul(550,422)select()why()#^]mul(339,807)!]why() %<+$mul(662,33){^~what()^?$> ,mul(148,900))*~mul(60,504)-/(/who()+/mul(44,663)#!&$'}]how():%mul(285,731)]}]when()select(525,368)mul(268,251){;^select()mul(478,141%>}who()how()<<-+})mul(653,304)[#$)#!/usr/bin/perl~mul(445,156)?,what()'mul(57,772) +mul(155,845)<@-<),#!&mul(408+#/+select()select(589,946)why()select()when()$^mul(126,507):^mul(59,335)*}why()?>+'when()!mul(448,637)(+;from()mul(100,753)why(274,266)mul(383,541)!-mul(858,418)*mul(632,981)what()]?when()~/'why()!when()mul(964,498),/mul(763,71)why(391,517)(where()what()}mul(465,534)select():'[';/^^/mul(440when()*]( 'why() &^)mul(532,924)((-,&why();)where()-mul(123,757)#}!)'mul(263,796){&;:when()),: }mul(530,962)( '/+mul(353,976)!>-~}*where()%mul(579,525)>mul(623,336)^']~:~$don't()when();where()%when()(')}mul(731,106)when()from(),from()how()/}(#when()mul(73,364(when()$/-mul(74,496)from()mul(411,383)+])%from(803,142)/][(mul(500,546),)-@mul(981'do()where()mul(756,84)!]what()what()@>]select(374,28)!+mul(767,508)don't()&;:where()mul(587,258)&mul(254,192)!mul(80,230)* :+$?/what(){;mul(187,528)from();-;$[>when()&mul(816,345)?(&why();{mul(497^select(912,851)((}%!mul(39,99)/<#;[*{~/&mul(678,429(*#@&mul(769,236)%~mul(23,229)$>&&from()$-#mul(619,573)%}[where()+mul(981,353)@why()what()when()what(),mul(482,105)from()who()from(){from(590,820)mul(269,29)+}<@why()where()?how()mul(123,830)} mul(589,571)what()from(),where()) mul(971,260)?why()when()<+from()( >mul(663,513)*how()%where(){:{who()why()mul(691,983)'#mul(465,961)mul(231,307)}*>:-*mul(511,596)what(){$$,(why()$%(mul(694,486)-[!;-]**mul(606*{select()+ {)';-mul(769,112)how();{?mul(281,824)-(}%from()!mul(557,222),@&>who()#mul(965,925){when(),}: mul(72,17),mul(366,881)$who()~(why(),-@select()mul(816,410)?how(191,3) $what(){/~what()who()mul(392,824) / @mul(744,684)}who()'{mul(808,581)where()mul(580,161)!*mul(517,629):@$what()what()when()select()mul(252,204)~] !who()-from()(mul)>[)mul(915,708)~{from(),mul(244,280)}@[^/@mul(127,150)]^mul(189,40)from()^?mul(631,283)' !-what(),#select()mul(242,34)how():+how()mul(625,833)~~mul(570,337)'how()!mul(827,377)>select():}'(&+mul(175,219)select()<{+{{$mul(96,718) >,;{from()^mul*#/]@from()'>]}%mul(602,848)/~)where();#mul(844,941)$:<]!mul(192,246)^!&what()mul(876,655)*select()&who() -mul(711-$when()>where()from()*@how())from()mul(202,362)what(){,@]/!%mul(812,546)!how())do()&^-~mul(937,389):';'~what()mul(7,641)from(825,618)what() ')~mul(604,895)when()#$~mul(762,876)~+!@+mul(953,72)@}mul(771,143)from()/+,')do()why():mul(127,841)~+why()mul(324,780)*what()~from()()%+select(19,530)where(567,447)don't()mul(330,681)^ what()#how()when()$!mul(624,186) mul(57,174)~mul(825,181)-'mul(560,884)-]mul(524,985)[:select(962,509)mul(781,265)+;!&#&/select()mul(533,308)#@where(472,118),/what()>,what()mul(327,56);where()+what()}what(),mul(422,317)mul(777,506)from()what(649,57)mul#?)['()/from()don't()$from()?(who()mul(64,560)who()mul(329,383)!where()?mul(859,391)/-}from()%#!where()'mul(144,512)*from()how()mul(875,781)^>{how()from()where()@who()+]mul(669,466)%;$where()select()mul(941,765)mul(421,766)mul(728,922)>mul(387,851)#~;? {: mul^??^/why()why() mul(870,458):,who()?mul(335,421)when(730,409)select()+)+mul(186,867)?$&''who()%#^mul(907,175)][don't()]what()@select()!'why()[where()?mul(475,57)mul(107,393)who()*[>>$why()~mul(620,425)]>mul(818,900)do()when() }:$*where()mul(790,845)why()why()'^~who()/~@/mul(181,583)what(182,109)}how()^{#;mul(532,225),/&)mul(510,91)+how()[}#&]&)mul(193,421);$[/~mul(421,747)why()%;mul(542,99)-from()where()from()from(){+mul(200,487):'^*,&$from()>mul(689,957)$]${*}where()mul(12,272)*{from(275,255)~who()*-%how()mul(501,743) +[^what()-select()mul(455,700),mul(39,833)?+mul(804,222) from()who()mul(45,702)[when()#don't()}}where()(#}#from()]mul(584--'-^#mul(858,680):)>&$}^what()mul(645,272){where():}&mul(313,324)what()who():!who()select()?mul(401,708),mul(656,174)why()?~)mul(679,594) where()#*,:from()!mul(45,422)~+ >~mul<:-/$$from(630,159)+<#do()])%how()^:?@]]mul(342,700)[[from()select()mul(333,963)!%$<:-^mulwhat()mul(947,945)!select(726,266)when()why()}&mul(494,61)<'why()mul(605,463^who()when(){?why(520,809)^where()>)^mul(842,681)how()how()[/^$&mul(535,168)@what()from()where()from()+[mul(926,493)/'/mul(175,156)mul(884,607)};#mul(998,216)mul(279,631@($(';++what()&don't():^:why()+when(391,664)/[{-mul(693,809)@)~/^>,#what()mul(942,229))/*{/^why()from()-}mul(653,168)do()who()+ mul(849,72)?,@when()!what()]~when()when()how()%mul(449,449)#^;{$(where()mul(24,301)~]]!!#mul(753,855)'#$don't()~where()who()mul(26,763)'!#where(430,52)what()where()]!*/;mul(20,683)~mul(471,401) $+](who()^;mul&*} )>mul(120,505)where()when()^~mul(586,106)/#<}+'}mul(56,277)%mul(944,864)>#^^!,select():!mul(5,629)(&{*;^@select())mul(544,161)how()/from()> ,~mul(531,531$^mul(693,695)where()#, +[mul(201,445) <:[$select()why()-mul(65,416)#-&(/mul(632,729)&#^+mul(18,292)mul(87,905)[do()from()<^why()what()(mul(292,732){}how(),why(302,606)]($%mul(577,286)--who()'from()^#,-where()do(),?>from()@@ %what()>mul(62,435)^:!^where(530,516)*(mul(605,627^&mul(638,58)@*,when()]*mul],;]-:*?mul(605,171)?>/select(449,616),-#[[+mul(178,672)*)#select(),mul'*+when(608,345)mul(671,276) '#where()*don't()]where()<;%select()select()[;/mul(653,417)>where();-}${->mul(3,376)!how()what(204,543)why() }:mul(531,438)?$!#select()from()*select()mul(482,192):* ^$mul(521,492) &mul(376,921);what() '*who()mul(631,117) who()select()#>select()]mul(665,901)when()*mul(787,27)/:where(356,180)from()mul(942,346)who()(mul(946,745)what()%who(){from()mul(166,509){:$who(58,738)]$&~when()select()mul(233,332)[]<;mul(831,96)how()[/((%mul(996,261)]?)):mul(581,265:!-why(796,972)^mul(921,535)why(),;[why()$mul(202,157when()!from()what(607,94)%from()from()what()+~$mul(578,993)@@ ,how()mul(224,265)mul(818,106)$:]&,>who(156,132)mul(828,230)+>from()<)who()^who()*:mul(633,389)who();from():&$mul(337why()*&)mul(148,542)(who()who()?mul(58,849)')***mul(138,264)mul(656,693)mul(292,571)#from()!mul(854,593)*!mul(93,255)/from():;:,?mul(938,2)$mul(882,42)who()mul(535^[mul(662,987)(^mul(86,164)*/$/mul(218,726)+-[where(119,390)^$mul(388,958)<#from()why()+select()#>%mul(24,167),mul(759,582)&$why()%mul(45,313)}-~&]how()when()from()mul(396,35)why()@+@where()mul(854,611):[mul(43,673)*what()@why()mul(914,638)--why()*mul(428,597)mul(478,658)(%mul(515,571)from()$select(),why(905,38)do()mul(892,588) + (mul(4,214)?}{},%don't():mul(6,78)% )from()mul(682,475)^;~%:mul(87,597)(mul(944,631)mul(976,495)[(%#mul(221,797)]from())mul(871,394),^don't()mul(808,119)why()& mul)mul(459,746)from():(~what(),mul(961,433)/{when()^ ?*{[mul(687,148)where()#mul(793,57)mul(791,305>where()~(/mul(416,515)what(562,107)$%~mul(578,152)select()'*{select()~from(778,947)mul(456,779)mul(610,422)$(:%~select(632,803)mul(652,488)]what()why()/select()@don't()mul(812,692){<[&why()don't()mul(339,986),what()-'>,mul(356,656)':$what()%{&when()from()mul(385,923){]>@(~&why()-mul(201,731)]mul(599,610)?why()@,&how()#mul(204,373){'from()}!#{%select()%^?}where(6,733)mul(118,396),)#:-when()'select()mul(788,521)'&#&($do()from()$,~!mul+mul(245,72)?;when()!')!who(450,16)how()#mul(341,203)[>how()who()'mul(116,924)@+#mul(536,362)when()select()why(),!, >+?mul(674,414)/%*mul(499,509)&)})+*{/mul(125,544)[who())+}]why()'mul(694,107)#-mul(589,269)<:,*what()what(),from())mul(779,394)?{+)[from()what()/&^mul(418,402)^from()]mul(771,556)mul(48,471)^mul(5,471) %,[who()mul(584,991)$$select()when()%,when()mul(662,976)&mul(378,372)-how(),select()-mul(693,943)^ mul(71,492)^{;(,from()*}$mul(325,434)'from()mul(39,981)% why();~mul(962,307)who()*what()select()when()mul(442,854)who()~select()from()why(848,680)where()/&mul(138,127)- +?when()where()select()!-mul(74,742)]who(449,197)-mul(58,606) <%mul(563,278)where()when()mul(167,344)do()-where()what(196,552)]why()%,((%mul(508,557)what()from():^what()mul(858,144)what()from(),mul(332,118)mul(805,968)how()@!- mul(31,7)(from():select()who()*}^+mul(790,338)?mul(876,269)what()$?>who()mul(705,526)*%;mul(284,73)/%!mul(336,280)&select()why()mul(841,593)##?:mul(611,712)%&:who()/[ who();do(),?)# }mul(10,584),where()^@,from()what()%;(mul(776,536):*!mul(440,957)where()# ^when()#*mul(282,762)]<~;#who()#*when()?mul(945,443)>*);why()[:mul(174,224)&~ who()how();when()@^mul(301,546)mul(180,572)select()<%#:&mul(642,14)why()~from()'where()#-:mul(711,95)what()^,<-!what()/#mul(787,954)from()/mul(449,665);why()-)mul(465,877)+#/what()who()$'<&mul(908,857)[@>!mul(190,564)from()who()&,mul(539,109)-why()/&{;,how()!>mul(381,931)>>]select()+mul(451,520)don't()from()when(),<^-+mul(198,363)mul(469,259)?@where()}~{select()-(