diff --git a/Cargo.lock b/Cargo.lock index 6d20dae..4a3ae72 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,3 +13,32 @@ version = "0.1.0" [[package]] name = "day2" version = "0.1.0" + +[[package]] +name = "day3" +version = "0.1.0" +dependencies = [ + "nom", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] diff --git a/Cargo.toml b/Cargo.toml index 3cba5a9..6f0eb98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["./day*", "day2"] +members = ["./day*"] [package] name = "advent_of_code_2024" diff --git a/day3/Cargo.toml b/day3/Cargo.toml new file mode 100644 index 0000000..c40adf2 --- /dev/null +++ b/day3/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "day3" +version = "0.1.0" +edition = "2021" + +[dependencies] +nom = "7.1.3" diff --git a/day3/src/common.rs b/day3/src/common.rs new file mode 100644 index 0000000..77c268f --- /dev/null +++ b/day3/src/common.rs @@ -0,0 +1,86 @@ +use nom::branch::alt; +use nom::bytes::complete::{tag, take_while1}; +use nom::character::complete::{anychar, char as take_char}; +use nom::combinator::map; +use nom::multi::{many0, many_till}; +use nom::sequence::tuple; +use nom::IResult; + +pub(crate) fn parse_till_mul(input: &str) -> IResult<&str, usize> { + map(many_till(anychar, parse_mul), |(_, product)| product)(input) +} + +pub(crate) fn parse_mul(input: &str) -> IResult<&str, usize> { + map( + tuple(( + tag::<_, &str, _>("mul("), + take_while1(|c: char| c.is_ascii_digit()), + take_char(','), + take_while1(|c: char| c.is_ascii_digit()), + take_char(')'), + )), + |(_, first, _, second, _)| { + if let (Ok(first), Ok(second)) = (first.parse::(), second.parse::()) { + return first * second; + } + panic!("Failed to parse") + }, + )(input) +} + +pub(crate) fn parse_input(input: &str) -> IResult<&str, Vec> { + many0(parse_till_mul)(input) +} + +pub(crate) fn parse_do(input: &str) -> IResult<&str, ()> { + map(tag("do()"), |_| ())(input) +} + +pub(crate) fn parse_dont(input: &str) -> IResult<&str, ()> { + map(tag("don't()"), |_| ())(input) +} + +#[derive(Default)] +pub(crate) struct Parser { + do_mul: bool, + sum: usize, +} + +impl Parser { + pub(crate) const fn new() -> Self { + Self { + do_mul: true, + sum: 0, + } + } + + pub(crate) fn parse<'a>(&mut self, input: &'a str) -> IResult<&'a str, ()> { + map(many0(|input| self.parse_till_expression(input)), |_| ())(input) + } + + pub(crate) const fn sum(&self) -> &usize { + &self.sum + } + + fn parse_till_expression<'a>(&mut self, input: &'a str) -> IResult<&'a str, ()> { + let (remaining, (_, (sum, do_mul))) = many_till( + anychar, + alt(( + map(parse_dont, |_| (0, false)), + map(parse_do, |_| (0, true)), + map(parse_mul, |sum| { + if self.do_mul { + (sum, true) + } else { + (0, false) + } + }), + )), + )(input)?; + + self.sum += sum; + self.do_mul = do_mul; + + Ok((remaining, ())) + } +} diff --git a/day3/src/day3.md b/day3/src/day3.md new file mode 100644 index 0000000..daf2f05 --- /dev/null +++ b/day3/src/day3.md @@ -0,0 +1,45 @@ +--- Day 3: Mull It Over --- + +"Our computers are having issues, so I have no idea if we have any Chief Historians in stock! You're welcome to check the warehouse, though," says the mildly flustered shopkeeper at the North Pole Toboggan Rental Shop. The Historians head out to take a look. + +The shopkeeper turns to you. "Any chance you can see why our computers are having issues again?" + +The computer appears to be trying to run a program, but its memory (your puzzle input) is corrupted. All of the instructions have been jumbled up! + +It seems like the goal of the program is just to multiply some numbers. It does that with instructions like mul(X,Y), where X and Y are each 1-3 digit numbers. For instance, mul(44,46) multiplies 44 by 46 to get a result of 2024. Similarly, mul(123,4) would multiply 123 by 4. + +However, because the program's memory has been corrupted, there are also many invalid characters that should be ignored, even if they look like part of a mul instruction. Sequences like mul(4*, mul(6,9!, ?(12,34), or mul ( 2 , 4 ) do nothing. + +For example, consider the following section of corrupted memory: + +xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) + +Only the four highlighted sections are real mul instructions. Adding up the result of each instruction produces 161 (2*4 + 5*5 + 11*8 + 8*5). + +Scan the corrupted memory for uncorrupted mul instructions. What do you get if you add up all of the results of the multiplications? + +Your puzzle answer was 191183308. +--- Part Two --- + +As you scan through the corrupted memory, you notice that some of the conditional statements are also still intact. If you handle some of the uncorrupted conditional statements in the program, you might be able to get an even more accurate result. + +There are two new instructions you'll need to handle: + + The do() instruction enables future mul instructions. + The don't() instruction disables future mul instructions. + +Only the most recent do() or don't() instruction applies. At the beginning of the program, mul instructions are enabled. + +For example: + +xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)) + +This corrupted memory is similar to the example from before, but this time the mul(5,5) and mul(11,8) instructions are disabled because there is a don't() instruction before them. The other mul instructions function normally, including the one at the end that gets re-enabled by a do() instruction. + +This time, the sum of the results is 48 (2*4 + 8*5). + +Handle the new instructions; what do you get if you add up all of the results of just the enabled multiplications? + +Your puzzle answer was 92082041. + +Both parts of this puzzle are complete! They provide two gold stars: ** \ No newline at end of file diff --git a/day3/src/input.txt b/day3/src/input.txt new file mode 100644 index 0000000..56ef8f8 --- /dev/null +++ b/day3/src/input.txt @@ -0,0 +1,6 @@ +[why()what()]:!*<)~mul(929,350)({)when()@mul(878,389)}$!? mul(786,950)~when();[(#mul(808,160)mul(659,500)&+?*mul(659,863)~~&:;,>do()(%@from()^!how()how()'@mul(281,23)-^~mul(473,263)}&!?when()~mul(208,697)?^)from()}who()]-where()mul(975,727)?where()'who()&&]how()don't()]',{how())mul(862,420),mul(505,346)mul(150,525)@'~[-,]^who()mul(842,702)# where()who()/*/@-select()don't()what():when()when()how()[what()%(mul(669,360)>@!+&what() ^%mul(285,288)mul(778,743)why()!+?((^;mul(725,987)*where(491,873)+don't():how()what()'(mul(757,125)what()}/where()why()]@don't()select()}(/select()$$when()']mul(494,390)}-who()+mul(182,852)when()#*^mul(144,423))mul(250,580){what()%%where()* mul(870,438)from(431,933)' when()mul(965,381)why()how()@where()?from()mul(817,13)$;what(648,988)mul(25,13)[(do()why()when(916,672){?what()*'what())mul(469,12)&!([[how()where()mul(619,597)+]where()&{%)[+mul(734,721)^:&%how()[where()who();mul(725,524))mul(389,895)mul(166,39) )select():mul(655,784)where()]),]who()what()]:who()mul(353,573)>~%^< #what()}mul(522,78)*where()when()^$+&&mul(236,646)!-who()$>from()?%mul(376,189) $''@$ who()mul(300,957)[mul(83,888)mul(107,58)'*#mul(535,222)'mul(370,487)>{:::+);select()mul(228,11)mul(961,125)^select()mul(489,332)+/!^#where()select()mul(757,495)*don't();]who()how()mul(814,931)?)how()^^?$when()mul(669,252),]mul(69,754)when(),@select()when()^mul(316,878)how()']mul(772,418)!}why()(,who()mul(186,499){^[where()%$what() select()from()mul(711,645/^mul(517,805)&[why()&~mul(970,867)where()>mul[% select()who()/what() select()?select()mul(739,456)**mul(620,935)-%,where();,what()how() mul(715,502)+when()where()#>mul(635,908)why()what();:mul(30,139){/mul(696,341)$mul(178,75)$,&/]>:why()mul(531,586);;mul(639,746)~select();/$mul(501,62);]mul(796,439)>*<%where():~,*?mulfrom()+}*do()mul(766,409){mul(418,953)where();#from()select()$&what(178,382)mul(435,11)}[mul(398,68!&-<@:$mul(628,277)mul(570,862)>]what()?<'$mul(624,957)^]mul(946,45)[~why()[from()) ?do()>how(),{+}from()mul(687,919)]why()who()who()how()<%mul(839,726)#*-'@>(~>mul(588,167)@%@when()why()when() #mul(503,12)select() %!;[don't()when(){-]%why()!%+:mul(55,105)@what())mul(349,994)}who()where(424,307)}from()what()mul(991,980)mul(313,17)where()*[+%from()&how()-;mul(118,53)~why()^@from()-mul(40,718)'&why()mul(393,507)why()[-:,mul(715,497)- @<;#)?mul(450,459)select()~$&mul(570,625)what()mul(944,195)select()what()+%where()where() /-,mul(203,571)(+why()[(#^who()~mul*where(),why()~~why()'@mul(469,314)mul(839,867)mul(607,678)'do()*when():>?mul(285,955)what()/}when(){-why()mul(358,393)>@[where()(who()mul(676,462)&]why()&how())mul(872,847@+#:mul(274,134){/how(53,761)>mul(895,912)why()!*>^-]why()what()don't()/)mul(219,961)where()how()[^~'-^{when()mul(177>&,,'}from(),what()mul(314,86) ]{mul(74,407)how()]!;(}don't()how())mul(395,998)]select(415,991){do()'who(),];},mul(374,334),!)who()(;}^{}mul(343,209)/what()[+ [{mul(952,521) [?[}:mul(264,303)where()*~where()*'mul(984,690):@@]mul(979,762)from()!(/why(976,660)who() mul(387,945)mul(885,520)$when()%who()&mul(950,716)[)when(84,652)who()/&~^-don't()?$from()mul(712,945)# why()&<}mul(83,500)?]:/mul(567,822)how()?why(515,831)&[<[^mul(503,349) +!*<,){$?'select()don't()what()%,(!~mul(961,381)select()*%why() when()#$)mul(380,949)~$what(428,872)how()?&'+mul(261,613)what()what()!']'mul(292,235) select()from()@who()?^what()mul(383,544):[mul(272,707)>(what()mul(735,912)}>when()select()select() {mul(62,107)/]select()mul(713,534)); #+/@?&/mul(572,504))&)>who(951,467),{*^&mul(31,687){mul(313,607)><,what(496,388) ]why(521,85)(mul(690,876))::'@why()who()mul(596,341)<,mul(455,990)<{mul(497,180)when()select()@ -mul(613,543)don't(){what()~[where()] ,^mul(333,124){where()where()>!:mul(545,512)why()(mul(561,668)what()%^/(mul(478,75)@where()mul(329,992)$from(): ,*mul(866(}*)why(),mul(241,743);@select()+>?how()}mul(844,735)how()>select(){^{from(){do()mul(966,882)how(){!<*'select()'!!mul(987,455)#~mul(515,519)&when(896,529)how()mul(631,876from()+what())do()what(){who()+(;'from()!mul(426,523)[why()-<&!,mul(460,572)&what()&/~/mul@what()&select()+who()$mul(243,403)mul(632,748)@#%@why() when()[))mul(17,783)}mul(713,921)+<>@how()~&who()why(274,888) mul(271,938)^]'!why()mul(511,239)$+when()^@>>mul(97,300)!who()when(841,168)$#!:don't()select(){how()>where()+who()*mul(237,42)how()how()mul(241,881)why()^^select()&when()#[;&mul(907,680)mul(984,346)^!}don't()[*>mul(210,168):[]~?/;~mul(936,602)do()}!(,!^*what(){*mul(818,741)^/]$}#when() don't()what()mul(675,247+{+:from(596,382)what()&#mul(545,450){how(536,664)({@,-/+mul(844,400)^%when()#~from()how()%+mul(178,299)mul(47,387)];}-,mul(73,974)#+;~['mul(722,711)why()when()<*&select()mul(662,195)%mul(928,541)why()}^select()select()mul/mul(875,585)when()when(999,528)[mul(604,342);[@<,:when()who()mul(620,728) :}what()mul(135,593)}when())):*;'who()%mul(453,733)#^'- '}[])mul(645,243)who()@^?when()>mul(625,242&>(/@*?${{~mul(407,983)#% ^* mul(932,718);who()~$mulwhen()how() mul(88,59)?where()@}~-mul(123,691):>^[/what()mul(744,809)+){!?- '*mul(213,965)'when()-why()how()mul(477,506)@who()mul(701,848)when()%~where()>^mul(165,63))*}$,#mul(399,533)from()when(){$ do()from()^,,}[when()mul(704,3)]mul(694,548)what()mul(900,108)mul(774,475)[where()<%why()what()~$^mul(883,579)~when()&%/mul(923,647)where()$^where()?*mul(807,659)when()]*@(+mul}when(76,45)//'when()%mul(222,35)}:%:mul(671,994)how()who()do()}from()mul(172,811)>{ ?how();?why()*don't()?what()what(771,976)^why()}mul(76,532)@%(>}mul(256,283)(/$,]from()]^[!mul-)%%%,@&mul(882,66)^where()&[when()do()when()/why()&^~mul(727,37)'%how()why();,';;-mul(535%~&select()mul(753,604)mul(442,703)#from()#mul(451,658)mul(690,961)[^+<}%^mul(409,762)!'mul(785,505)/$who()&:;%$;%mul(181,733)*+when()$#+select()mul(295,726)*@,do()?#*<,where(650,5):why() what():^where()mul(869,48)&?<*mul(342,192)from()@who()why()who()*mul(230,176)'mul(587,59)[~'/when()](mul(979,301)+:,mul(651,625)mul(325,740?'#&??when()@*;^do()when(),~}who()%$^mul(564,586)where()$mul(686,200)};{%mul(666,187)>'?)/$$mul(337,903when(),why(){*@)-select()*/mul(788,793)];how()!({where()@*who()mul(787,265)?(^/[#-)where()[mul(111,927)*mul(27,304)+{)when()%+-#mul(176,110)who(588,615)mulwhen(506,316)@]?(&@}' ,mul(358,826)~who()mul(501,531)mul(365,143)<^[who(677,182)mul(759,671)from()why()[*what()+mul(679,332){?*mul(817,808)?({how()when(556,48),from()}{mul(90,989)-mul(503,769);:#(]'how()'$:mul(859,40)how(597,760)-]~what()why(),;mul(638,143)}what()],!]]mul(428,646)mul(353,573when();,mul(691,924)[from(),~ mul(875,629)(~%;+#]select()mul(338,859)?{where()}+what()/what()when()$mul(375,365)<+^}where()&mul(336,223)?:~who()/:(what(501,622)?@how()*why()select()mul(984,814)?who()mul(808,237)mul(194,593)(^/~!mul(795,480)/!mul(749,71)mul(934,601)'mul(807,763)?]from()when();mul(515,591))who()where()what()$+from()[>mul(911,440)$&>($when(52,661))]from();mul(886,109)from()%don't()how() &from()mul(915,528)!;]who()mul(144,716)*%where()what()[mul(376,205) ?who()mul(274,491)mul(671,696))mulhow()]mul(375,359){*:~(mul(956,842)do()!?select() &(]}where()mul(618,693)how(105,340)when()*:+<^>&mul(761,859)]from()}>why()'mul(542,515)mul(462,546)^mul(660,240)what()]/:how()mul(609,298)**%when()(,mul(279,293)when()+%(;(%^^mul(392,795)?@-%])don't();how()-+when()<+mul(128,643)}!*}^}'}]-don't() mul(930,403)when()when(){<^*why()mul(11,788)>]-how()',?who()mul(442,615)from()(from()(#{?$(]mul(654,462),,#~);'/mul(960,246)select()mul(111,461)mulfrom()(~,where()mul(641,83):#{'*(from()mul(759,794)what()//+:*mul:;mul(543,856)mul(602,732)^mul(124,738)%),[~$mul(378,18)]/when()&~mul(723,701):-{why()+where()*what()$&mul(633,998)select()+when(562,500)mul(833,630)why()how()where()from(799,941)mul-<@-!^mul(945,843)}}-*^#mul(705,202)%]'how()&]what()~who()/mul(616,696)*mul(163,194)what()&mul(941,303)'}what()^ #'&mul(924,715)from()>@^{mul(526,75)&what()!@!{what()mul(248,994)$select(253,838)~)~mulselect()&^;^]where()mul(985,752)mul(275,971)@where()[mul(592,347)mul(209);:!when()who()$/mul(17,702)&{#!who()mul(800,427)-$&mul(939,888)~#when()where()']<}]?who()mul[,mul(833,540)~-^#when()@{^(>mul(946,450) +who()mul(242when(101,870))>@mul(753,627) @:select()who(406,895)'%do();who()%mul(361,888)why()!%how()mul(700,949)$mul(180,872)(*#(mulhow()mul(882,985)(+?mul(351,704)[)how()#how()mul(764,860)!who(404,988)from()mul(993,557){,why()>who()mul(56,860)what()[/mul(347,909)}{mul(699,230)?~%why()-:why()>?mul(445,473)mul(267,398)'~mul(650,363)*what(){*?{where(){}>mul(601,997)> where() mul(497,753)select(){when(868,294)mul(141,967)how()who()$mul!what()!~who()}who():mul(36,947)where()mul(681,450)mul(240,143)mul(444,949)?don't()>*^ where():mul(497,86)mul(430,567)]}%!mul(193,883)?mul(910,491&mul(689,168)what()<;~mul(29,977)~^?~&select()<~]mul(205,284)mul-**&,!{mul(737,674)}, ;mul(12,767)<[when(422,261)where(),})'$mul(532,879)who()mul(397what(941,622)/mul(765,185)who() mul(798,105)'&(/who()mul(332,180);when(757,619)how()mul(505,995)>*[({;^}#mul(890,215)%]%{-mul(908,576)when()[;mul(714,969)>+&]~? {@mul(60,591)>$mul(792,423)where()@what(114,480){^ -$mul(354,440)(-mul(713,525)where(784,906)#^}^[}mul(305,922)how(){: ~}<>}}mul(448,877)'!mul(751,305)!%don't()mul(179,695)? ?}how()mul(841,995)~when()from()&>[mul(353,537)}]+;mul(501,394)^{/^^ />mul(999,635)!$;,how()?$%~mul(605,217);[?how()#from()mul(936,501)select()from()'{~from() />don't():how()/>:select()mul(246,594)from(358,782),[)~mul(256,918)$(}where()}how()mul(370,832)&why()mul(925,451)mul(47,536)%$]@mul(694,750)#who()mul(604,542):%(how(){mul(972,130)@}what()$mul(222,940)));(*!)(%}mul(455,452)@&mul(8,457)how(){from()?how()'from(),]^mul(965,893)why()/-!+&^what()-mul(787,423)what(){where(959,674):who()]?mul(963,475)}~where():mul(81,905)$select()how()(@mul(465,909)-$select(849,483)?mul(534,596)why()->mul(196,18)select()+%mul(27,285)where()?why(){mul(664,943)$(who()%*mul(813,372)*~+&@('{how()mul(834,380)$/how(),why()}:#@*mul(813,509)^)from();*mul(545,292)%select()when(353,207)where()mul(767,38)]mul(24,562) +how()[[)&?&:mul(218,640)]who()^{mul(108,530)*+}}~<;-mul(179,490)}'+how()~%$mul(33,386)~when(694,12)(&where()}}~'mul(459&why()do()*:@+<<}mul(19,511)do()(mul(541,350)&!,mul(387,141)< {how(921,168) $}mul(429,335))do()mul(806,883):from(853,895)from()do()[select()+*mul(175,190)+mul(682,133){+'[mul(846,821)]%}@)&mul(116,347)who()/'<~mul(924,51)$,when()>(from()!mul(209,245)(@'where()select()?]/mul(460,178)-/why(825,431)!do()what(){!mul(765,879)&&mul(798,713)where()who()from()~mul(739,10)%] :>-when()]mul(131,912)?{ {$>?why()$+mul(825,581)what()mul(905,252)&when()>,%}'%-what()don't()~$where())?when()?from(968,779)mul(863,782)^$;:[!~what()mul(498,143)what()!*why()mul(347,195)#/mul(930,586)-/select()*:{mul(439,475>^from() )^>>^)why()mul(453,205)when()?[mul(75,857)[when()~how())who(){when()^+mul(201,137)>why():!+who(645,81)[why()mul(861,373):!when()*how()?'?;mul(410,947)what()%^[>mul(106,316)> ^(where()~select()where()what()mul(296,671)%select()&+{]*-when()mul(481,274) '$mul(929,179)&'&+ +~/%mul(29,898){^+{^/mul(580,352)when()from()]mul(585,530)*'mul(638,910)&/:^don't()->&!mul(894,636)when()*when()mul(577,671)}/~ <],$mul(999,788)/how()!;mul(574,638)-where()!^mul(66,674)/}mul(937,514)do()([*,+@(who()*;mul(750,907)^why()what()[*>mul(303,172)*why();how()+@}:;]mul(15,812)where()-:where()~mul(673,466)+{$'%mul(623,971)[select()don't()mul(7,739)!&,)'who()%'mul(509,601))select() do()select()mul(36,79)mul(199,611)mul(983,700)@;?@how()why()mul(136,594)[+%}how()mul(34,407)[where()^!when()don't()~how()(@mul(71,117){#?mul(857,810),>[when()mul(993,232)what()why() when()~+${mul(103,182)>-from()from(),mul(756,418)):what()'~from()select()mul(833,12)<#why()~$ ,mul(74,614):from()% @{]from()+:mul(120,343)mul(714,187)why():mul(379,724)^select()-$-?$who()~who()mul(5,213))(,?]mul(151,375))^$:;@:mul(34,537)${&!-#,[mul(289,643)what()why()>select()select()mul(938,238)+mul(32,613)mul(415,379)[,<~$$don't()mul(272,584)?-]&mul(164,981),>+how(623,296)!{+mul(628,434)why()how()(who()~-($how()mul(912,947)}mul(279,831)where()(++select()+]#mul(916,868)}+>^-from()@mul(834,585)!]@what()select()+#)/mul(722,165)from()what()mul(220,639)^[[{$mul(738,370)$who()who()< >mul(650,822)),/<,?when()mul(387,845)#from()&where()what(){mul(921,764)?from()from()do()>(;%~'mul(323,507)&}&#why()when()who()mul(998,500)select()[mul(543,802)from(){&?*/mul(809,888)+',%[&when()$mul(909,660)do(),:;)&>mul(303,499)when()!where(){%what()#mul(176,667)&*mul(789,667)}*when()/;how()*when()mul(181,142)how()what() diff --git a/day3/src/main.rs b/day3/src/main.rs new file mode 100644 index 0000000..1860f20 --- /dev/null +++ b/day3/src/main.rs @@ -0,0 +1,13 @@ +extern crate core; + +use crate::task1::task1; +use crate::task2::task2; + +mod task1; +mod task2; +mod common; + +fn main() { + task1(); + task2(); +} diff --git a/day3/src/task1.rs b/day3/src/task1.rs new file mode 100644 index 0000000..d5ba4a3 --- /dev/null +++ b/day3/src/task1.rs @@ -0,0 +1,27 @@ +use crate::common::parse_input; + +pub(crate) fn task1() { + let raw_input = include_str!("./input.txt"); + let (_remaining, products) = parse_input(raw_input).unwrap(); + + // 191183308 + println!( + "The solution to task 1 is {}", + products.iter().sum::() + ) +} + +#[cfg(test)] +mod tests { + use crate::common::parse_input; + + #[test] + fn test_task1() { + let test_string = "xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))"; + let (remaining, products) = parse_input(test_string).unwrap(); + let sum = products.iter().sum(); + + assert_eq!(")", remaining); + assert_eq!(161usize, sum); + } +} diff --git a/day3/src/task2.rs b/day3/src/task2.rs new file mode 100644 index 0000000..332c12b --- /dev/null +++ b/day3/src/task2.rs @@ -0,0 +1,25 @@ +use crate::common::Parser; + +pub(crate) fn task2() { + let raw_input = include_str!("./input.txt"); + + let mut parser = Parser::new(); + let (_remaining, _) = parser.parse(raw_input).unwrap(); + + println!("The solution to task 2 is {}", parser.sum()) +} + +#[cfg(test)] +mod tests { + use crate::common::Parser; + + #[test] + fn test_task2() { + let test_string = + "xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5))"; + let mut parser = Parser::new(); + let (_remaining, _) = parser.parse(test_string).unwrap(); + + assert_eq!(48usize, *parser.sum()) + } +}