diff --git a/2024/03/Cargo.toml b/2024/03/Cargo.toml new file mode 100644 index 0000000..5fb957a --- /dev/null +++ b/2024/03/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "aoc-2024-03" +version = "0.1.0" +authors = ["MÃ¥rten Kongstad "] +edition = "2021" + +[dependencies] +anyhow = "1.0" +aoc = { path = "../../aoc" } +regex = "1.11.1" diff --git a/2024/03/src/input.txt b/2024/03/src/input.txt new file mode 100644 index 0000000..a1d15f0 --- /dev/null +++ b/2024/03/src/input.txt @@ -0,0 +1,6 @@ +why()$mul(735,469)^?!what()don't()~~('mul(982,758)what()^-^who()mul(294,364)&#/ )&%)[~mul(285,81)what()[{where()+ mul(129,761)^]why()>$^<>who()mul(53,344)select()%mul(567,352),mul(915,20)/#(>+^how()'mul(141,658){&~!how()from()?~?who()mul(830,116)#%;mul(139,634)?,#+who()@mul(169,719)[)?'-who();mul(18,751)},when(){;/mul(691,391)mul(856,314)^mul(558,88)]('/>how()@where()(,mul(274,100)%who()#~*mul(220,748)@how(){$how()/*%$when()mul(718,754)[!when(617,291)!{(what()+*do()[;<+ /from()[mul(840,803)}?mul(429,848)?select()how()^why()],&#select()mul(519,894){ !>:^@+mul(522,225)@!^^/'[>select(118,66)>mul(847,195)when(585,749)]mul(641,667):>mul(317,349) +:/^*what()mul(352,440)select()mul(349,981))@mul(450,917)why()how()mul(471,401)?where():}select():mul(632,956))mul(727,370)!}$~*%+$don't()why()select()^]who():mul(276,773)*^^mul(757,668):}mul(232,346)%[*@$ where()mul(898,992)who()why()~+['mul(43,434),where()who()>what()mul(908,953)select();do()}!select()when()!mul(676,368)how()from(966,785)@:$+)how()mul(414,120)mul(108,395) +~{#!@select(),mul(483,964)&do()?>mul(963,893)$,,/%'mul(318,508)?,{mul(390,813)select()>(select()mul(792,357)!{where();mul(717,18)why()$#]why()$^?mul(948,239)how()&>$from()]:>?select()mul(453,180)&&]&,[how()mul(674,281);*how()mul(548,919)how()]{{who()~+where()where()&mul(962,14)/why(),%>when()mul(31,451)/)&:,>}mul(225,266)-*)where()who()]#mul(224,69)#&-+~mul(309,393)@#;%!(][select()mul(389,558)+*how()*^+when()]when()mul(995,181);(mul(5,74)&(#^from()$~&don't()}? ,($ when()mul(216,767)>()!,/how():@mul(425,905):]>>from()mul(93,831)}select()(mul(70,72)mul(137,510)[]{where()mul(477,400)&?/who()who(335,756){/mul(364,311),$select()what()mul(710,251){mul(351,763)+-}mul(517,661)mul!}?how()where()mul(948,407)-{<]*^from()who()from()mul(979,180)/,:#mul(602,496)%'what(),#}what()~how()mul(343,710))'when()from()#mul(314,877)(+,[?mul(480,685),;do()%*<^)from()what()#@what()mul(502,65)where()why()(!*where()from(){/mul(847,541)mul(116,490)]';how(612,404))when()who()&; don't()mul(446,220)(%'?;{^?mul(784,876);)]mul(959,402)%]];mul(362,432)mul(330,805)where()mul(142,473)~don't()-who()mul(965,688)when()[]):-}from()%mul(949,478)why()>~+,!&~,select()mul(633,237!-who()>where()]mul(809,464){@'/when()when()mul(720,462)&select()select() )@}'~how()mul(518,133)mul(512,118)/}what()*][++mul(56,978when(),where()&don't()who()}where()mul(329,879))&$,:when()#when()@what()mul)~who()(;mul(141,130)'?^}#+#from()'-mul(237,78)&what();];%where(614,809){:mul(391,45)mul(787,970)from()-?&~when()mul(59,790) what()where()~?what()mul(399,503)^from()where()when()mul(190,97);why()who() ,select(){mul(428,654)mul(350,302)?{mul(592,241)when()how()mul(705,191)who() !who()mul(50,6)~'from()% &]mul(902,106),select();/ why()from()>mul(800,867)select(659,586)~ )when()}~&'mul(709,298)-:'do()[who()what() mul(31,997)$%(where(309,559)%: mul(177,184)@mul(58,102) +'^why()>-why()mul(489,67)who(682,115)where()how()>why()~;'^mul(179,994)who()#[+when()}mul(343,121)]^mul(622how()select()<{how()mul(315,335)-:%mul(611,869)who()when()$where()who()mul(242who()from()%&!['do()@:^-})}';*mul(266,851);^[{!&why()how()from()mul(980,269)^{where()how()/where():;+mul(971,795)#>][+^where()mul(279,554)/why()mul(736,24)mul(63,622when()who()how() -;mul(500,888) ]where()where()when();mul(255,25)<]!-,#!+how()@do()mul(670,579)?(//]from(41,426)$do()mul(427,50)/%mul(359,247)'what()~{~-?[#mul(862,40)]{from()when()mul(426,212)>who()mul(653,462)why()mul(15,779))]mul(258,38)how()**$?(}% ;mul(624,853)[;&select()where()*-)<)mul(111,225)mul(440,503)/what()###mul(840,369)who()mul(529,595)>(-/who()mul(168,832)mul(658,544)%:~~)what()why()%+]mul(358,878)who()'$+@-)*mul(30,603)&-do()mul(627?mul(834,46)+when(751,926)-mul(132,173)who()do()-%where()mul(487,54)!$what(),{mul(546,92):$where())&/mul(978,930)-/mul)#mul(693,136)!$select()mul(490,906)%!}({;where()mul(853,453)*]!<%what(85,66)+ mul(720,355)]+mul(65,942)? $mul(736,168)#who()from()< %mul(279,223),,how()select()'mul(949,79)@)-who()when()+;/why(),don't()~(@,^~mul(55,965)what()select()'where()$)?mul(615,265)mul(801,65/why())mul(55,877)[why()+>:~&%~mul(418,463)from()+'who()(how()who()when()mul(176,831)!#'mul(350,399)>'@{ '!mul(847,397):{who()'#$where() @mul(148,609))&,+,<->$@mul(475,800)who()what()do())}from()&why(821,875))mul(675when()select()), %mul(786,308)-[,/,+mul(305,3)how()what(107,974);!}+how()mul(535,976)from()[)mul(820,558):(why(){^mul(715,523);mul(288,378)'why()$why(),@mul(184,850)when()'';^where()mul(957,549)do():~{:{)select()mul(347,642)from():(mul(834,376)>mul(803,111)(,,'select()select()who()[ !mul(143,742)+:# {mul(891,385)where()}<%^>mul(995,109)'?from()$):who()mul(414,498)&/%mul(809,958),why()[!>,mul(591,908!(how()}mul(600,673)'%* *mul(48:$);(/from(371,7)(>mul(261,884)?&don't())mul(246,229) >;*'&!don't(),+mul(131,199),$don't()-(what()mul(13,595)'don't(),who()how() %mul(839,198))do()select()*from()mul(903,555)what()mul(74,674)%;@mul(190,543#)])[^*! ,>mul(428,756)/:>+%?mul(585,685)?@~/*why()){[mul(87,499)],@~!}mul(585,674) [,],]}?[:mul(511,978)!?)don't()&~*mul(735,638)select()[{from()!{>mul(897,633)select() #*mul(680,8)mul(671*how()^mul(580,632)(} how(9,101)from()where()'<&mul(936,112)select()what()how()#mul(720,608)'&^/ &when()@mul(697,116),#from(),what()!where();(:mul(833,800#mul(842,722)why()$from()[(^from()/mul(591,920)how()(don't()}mul(65,254)#why()@mul(696,883)^?what()mul(917,413)from();>{~mul(524,943)mul(465,955)how()select() &how()how()mul(528,18)how()]]do(){who();~>who()&mul(837,821) ++when()mul(957,88){mul(446,234),(%[mul(225,735)mul(616,489)<[]~+@why(274,559)-why(45,518)mul(841,337)@mul(826,883)-(where()don't();@'+~/from()mul(829,811)(what()how()how()#mul(435,969)mul(743,179)from()/'[/,];mul(797,84)[from()>:~&$} mul(934,535)@?})%mul(630,155)]mul(81,271)why();@;%]who()mul(616,154)^do()^)-*;/mul(562,475)select()(select())$mul(119,594)what()?mul(103,712)why()(what()mul(358,496),why()~+select()what()~select()from()mul(316,752):+^mul(851,254)select()from(){when(216,975):*mul(776,658)from()from()?!mul(724,606)from()mul(878what()^%'why()>]mul(920,733)mul(500,262)when()why()do()]!who(333,560)]why()^ ,}mul(594,767)~':}!mul(69,623)})where()where()@-^mul(510,559)from()where()mul(990,412)%)[ how()mul(627,280)@mul(314,481)- from()how()who()}'<^*mul(515,459)$how()do()]mul(307,482)@~( $(@-%:mul(460,946)%mul(942,646) {>mul(160,832)where()when()what()~{where()&@mul(24,840)^#[mul(344,387)#how();mul(525,285)mul(221,638)-!/?#-$^don't()mul(91,395)select(43,376)what()]~mul(677,682)from()$ mul(358,545)$+%mul(697,89):how()who()^mul(599,952)?where()#mul(752,46)where()* ?},#'mul(803,569)select()+()-when()*mul(5,223)'<])#~*^[mul(360,79)<$+mul(784,713)-%when();from()why()<]mul(288,526)';^~do()[;where()}%?<^+<mul(25,575)#^;mul(169,580)when() ^where();]mul(575,888)'who() &::,mul(172,538)@when()*!mul(718,235)%+/mul(281,117)who(),don't();what()how()select(126,34);why(460,565)mul(153,278)~when()*]mul(284,592)what()#@mul(416,344)+how()from(472,343)+]+*? mul(218,315)who()#:$@{}mul(415,704)mul(367,153)*where()!'do()what()[mul(298,416)<'*$:} !mul(8,562 ~#/[{~-$what()&mul(312,424) '??mul-$ -mul(49,590)>[+{how(472,207)]^mul(482,423)&why()(#^' mul(178,565)~^&>from()!#?don't()why()who(596,596))who()from()>how():{@mul(385,718) ~?@//**mul(783,105)~why(22,278):#how()do(),from()!:mul(40,924){[when(),/*mul(694~+mul(311,607)who()[/'mul(744,908)%>: ]~,:mul(310,993)from()%:-(+where()when()[mul(455,786)from()~]?from()^where()mul(460,406)how()when(831,855)'where()>[,:mul(250,253)~:select()){#)[[[mul(825!<{from()mul(205,719))why()@?%mul(729,531):where(); $~ mul(547,617)when()/mul(791,433)from()%select()what()%how()%}}mul(616,548)?%:',when()where()who(),mul(417,691)mul(548,124)<-%mul(459,792)mul(93,742)&from()who()select()mul(38,516)why()$ ; -how();mul(682,71);*why(603,344)mul(588,565)*#:do()'from()!who()-mul(564,344)mul(58,41)mul(881,91) when();/#!/usr/bin/perl!+'mul(791,572) +%mul(264,168)mul(409,229)[#what()mul(393,212)$-from()'*do()mul(274,560)-{+::&>,mul(778,511)when()[;why()'how(701,98)%}mul(630,304){%{>*why(52,488)?}[(mul(149,108&'why(222,790)[mul(8,101)}>;why())what()mul$-%^why()mul(823,831)'*+mul(2,756)>, *don't();/}~&mul(602,479)~%)from()*}-when()%^mul(849,417)$>!-+from() +mul(280,463)'select(),@how(637,7)+&?mulhow(771,951)$mul(373,910)what()when()from()-]:why()mul(835,707):<]&when(915,675)~mul(628,69)&^@&#)mul(419,539):%:mul(534,992)'mul(987,602}why() when()]mul(438,915)~}$why()@!'#>don't()^)}?-select(355,550)},,(mul(649,262)^ *[&}/%mul(867,47)mul(104,808):@why()'@'($$mul(74,805)what(597,630)}(;]>~mul(217,927)>what()mul(669,965)>from()mul(647,126)->mul(52,497) select()mul(22,612from()why()'mul(277,463)!mul(473,104)[!:;mul(655,992)how()>%(what()-mul'why()!&where(11,929)+mul(314,422)$how()what(610,732):&{~}#select()mul(517,349)why()!mul(815,441)#why(349,312)))how()['$where()[mul(593,303)]#$^;mul(901,6)<-}:,who()mul(254,964)select()@['mul(297,705) who()#^mul(794,226)when()who()who(562,667)>}[mul(265,242$'from()@}mul(724,493)mul(313,610)*->%,mul(83,839)^)%mul(149,753)&when()select()from() 'mul(3,784)select()select(715,428)mul(502,975)how()who() !when();{[mul(106,800)<;mul(513,728)select()select()}@;':from():mul(259,697}'$]&':+mul(998,295)how()@who();!mul(947,199)?/what()where()where()]mul(415,221)select(487,105)^mul(688,128)#&mul(394,445)mul(384,810) @!why()do();&!where(424,549)why()mul(934,449)%()-^from()?who()<(do()when()'what()#mul(424select(76,581):<+}from()#>+mul(616,317)#$what()@-select(425,315)mul(146,882)mul(509,291/#from()mul(152,967)why()how()mul(348,501)<[&^who()why():+mul(36/mul(856,681)!mul(46,203) +mul(315,935)mul(451,658)when()when(353,392)<why()]*')mul(21,640)from()mul(755,282)>^why()from()mul(442,981)!when()^why()why()(/mul(876,31),who()/[mul(740,627)]who()/+[mul:+mul(557,285)!(&how()mul(538,865)select()what()~$from()([+when()/mul(826,278):@select(546,967)!?&select()what()mul(847,194)#{mul(468,530)*![%where()^<what()?&mul(277^ mul(780,751)^what()~!#mul(635,606)?*when(): }who()]-don't()mul(42,308)+,(how()how()$-!~mul(29,791)%)mul(248,71)#when()-)]mul(670when():from(505,247)who()>select()what()'(mul(327,578)from()from(){who()who()from()^/mulwhen(801,176)what()};}! :#-:who():/mul(466,327)#~'!),&>mul(776,335where()]why()&]?]:how()how(),do()mul(442,378)when()/^^mul(407,519)'-mul(157,451)+]from()when()when()/mul(689,597#{mul(387,624)><]&$!from()%[-don't()what()why(801,120)how():%select())(mul(847,645)where();&'}#how(698,791)!don't(){)<^mul(694,504)](/*{$who()+mul(480,559)where()#>'who()[}select()!@mul(225,926)when()?%>+mul(631,126)}#@how()mul(712,802)-select()why()~'mul(748,579)from(){%mul(649,879){:select()~&!,[ !mul(147,693)<+}mul(87,949)-{where(345,734)/ mul(351,115)>mul(52,566)&mul(427,269)}mul(901,457)when()&mul(600,957)mul(734,851)!why()/[$/how();mul(383,59)'why(802,790)(;%'from()who()mul(904how()<;select()@{,who()>[don't()why()}[&[!mul(859,668)what()^mul(483,700) +]where()>>>&^@what()mul(320,309)[#<#:]~/mul(497,511)mul(95,653)how():mul(572,330)&mul(809,308)*(&:mul(29,810)who()how():mul(915,147)when()^#mul(849,988);who()?,(/*mul(129,470)select())$select()-&/why()where():do()mul(590,753),@;'/mul(396,339)>%mul(613,991) '{,(mul(899,674))/$}from(656,762)'select()(';do()+-(@mul(502,495)]{$>@)+do()'why()select(),when()mul(454,97)')'where(326,126) select()mul(334,130who(494,552)%@*$who(979,600)mul(876,925)^&^mul(975,151))who()who()&mul(136,404);%!';#mul(450,492[}+[&[$}%//mul(487,75)%mul(870,786),when()why()+~>mul(316,132)-from()who()when()'@who()mul(169,959)~~when()]how()+from()*^mul?&why()how()mul(641,834)^:!(-#mul(197,843why()?}[-mul(479,27)#,/when()when(284,485)from()(mul{)from()+'$~from()*mul(555,485)mul(37,175what()why()who()mul(849,203)who(607,487)how()select():&&;mul(874,736)when()/from(626,997)mul(482,451))who()[};mul(186,966)select()when()*mul(918,369)~who()](%mul(452,814)/&&^-!&$<<{mul(161,632)]mul(434,106):){select()mul(137,656)why()*when()^mul(198,859):mul(589,609) @-mul(79,388)/+}~]}from()how(),do()@<#])-mul(673,936)!mul(999,158)%mul(600,817)%~!#?@<^mul(948,543)$),'mul(108,479)]&mul(359,23)~@{+when()&%^!{mul(186,477)who()from()select()*{where()how()%%/mul(714,701)select()what()why()from(138,787)from()'$ mul(589,314)&what()}]**where()don't()@'?mul(905,694)%~mul(52,222),?]mul(726,995):where()^'[/mul(948,887)how()mul(396,68)what(){;select()$[% (mul(939,722)why()from()$who() &,$]mul(917,716)how()where()']~%'mul(174,257)/how()why()@select()mul(673,441)-<)'?select()do()how():what()]{}?-mul(202,788)[mul(842,734)who(980,842)/?/{+how()select()&how(149,196)mul(533,78),@#select()don't();when()mul(595,87)[(how()-;do()@>what()mul(543,798)/&mul(118,844)who(520,900)>!mul(615,214)'-/select(){where()@do()*#{%/mul(254,489)$@*,who()^mul(209,427)<%how()'^!^{how()mul(112,898)where()-/what()% ^'mul(400,703)mul(347,239)+:who()select()'<,mul(695,520)mul(595,418)why()?!#&^,;,:mul(128,623)$:/@why()$,!$where()mul(489,451):]}mul(426,973)/)don't()-<]what()what()!how()mul(384,927)?select()~:]*])-#mul(210,978)why()-/ diff --git a/2024/03/src/main.rs b/2024/03/src/main.rs new file mode 100644 index 0000000..659fb21 --- /dev/null +++ b/2024/03/src/main.rs @@ -0,0 +1,70 @@ +use anyhow::Result; +use regex::Regex; + +fn main() -> Result<()> { + let input = include_str!("input.txt"); + aoc::run!(part_one(input), 173517243)?; + aoc::run!(part_two(input), 100450138)?; + Ok(()) +} + +fn parse(input: &str, support_toggle_instr: bool) -> Result> { + let re = if support_toggle_instr { + Regex::new(r#"do\(\)|don't\(\)|mul\((\d+),(\d+)\)"#).unwrap() + } else { + Regex::new(r#"mul\((\d+),(\d+)\)"#).unwrap() + }; + let mut enabled = true; + let mut out = vec![]; + for caps in re.captures_iter(input) { + if caps[0].starts_with("mul(") { + if enabled { + let a = caps[1].parse::()?; + let b = caps[2].parse::()?; + out.push((a, b)); + } + } else if caps[0] == *"don't()" { + enabled = false; + } else if caps[0] == *"do()" { + enabled = true; + }; + } + Ok(out) +} + +fn part_one(input: &str) -> Result { + let pairs = parse(input, false)?; + Ok(pairs.into_iter().map(|(a, b)| a * b).sum()) +} + +fn part_two(input: &str) -> Result { + let pairs = parse(input, true)?; + Ok(pairs.into_iter().map(|(a, b)| a * b).sum()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const INPUT_PART_ONE: &str = include_str!("test-input-part-one.txt"); + const INPUT_PART_TWO: &str = include_str!("test-input-part-two.txt"); + + #[test] + fn test_parse() { + assert_eq!( + parse(INPUT_PART_ONE, false).unwrap(), + vec![(2, 4), (5, 5), (11, 8), (8, 5)] + ); + assert_eq!(parse(INPUT_PART_TWO, true).unwrap(), vec![(2, 4), (8, 5)]); + } + + #[test] + fn test_part_one() { + assert_eq!(part_one(INPUT_PART_ONE).unwrap(), 161); + } + + #[test] + fn test_part_two() { + assert_eq!(part_two(INPUT_PART_TWO).unwrap(), 48); + } +} diff --git a/2024/03/src/test-input-part-one.txt b/2024/03/src/test-input-part-one.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/2024/03/src/test-input-part-one.txt @@ -0,0 +1 @@ +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) diff --git a/2024/03/src/test-input-part-two.txt b/2024/03/src/test-input-part-two.txt new file mode 100644 index 0000000..30032cb --- /dev/null +++ b/2024/03/src/test-input-part-two.txt @@ -0,0 +1 @@ +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) diff --git a/Cargo.lock b/Cargo.lock index 7f1492a..c500de7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -835,6 +835,15 @@ dependencies = [ "itertools 0.13.0", ] +[[package]] +name = "aoc-2024-03" +version = "0.1.0" +dependencies = [ + "anyhow", + "aoc", + "regex", +] + [[package]] name = "ascii" version = "1.1.0"