From 319fd4590a6791f141f9c3700c302cf0db11f28b Mon Sep 17 00:00:00 2001 From: Jonni Liljamo Date: Tue, 3 Dec 2024 11:36:46 +0200 Subject: [PATCH] feat: day3 --- Cargo.lock | 23 +++++++++++++ Cargo.toml | 1 + input/day3/example1 | 1 + input/day3/example2 | 1 + input/day3/input | 6 ++++ src/day3/mod.rs | 59 ++++++++++++++++++++++++++++++++ src/day3/part1.rs | 52 ++++++++++++++++++++++++++++ src/day3/part2.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 9 +++++ 9 files changed, 234 insertions(+) create mode 100644 input/day3/example1 create mode 100644 input/day3/example2 create mode 100644 input/day3/input create mode 100644 src/day3/mod.rs create mode 100644 src/day3/part1.rs create mode 100644 src/day3/part2.rs diff --git a/Cargo.lock b/Cargo.lock index 3bd4a23..a8318c3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -63,6 +63,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "nom", ] [[package]] @@ -123,6 +124,28 @@ version = "1.70.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" +[[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", +] + [[package]] name = "proc-macro2" version = "1.0.92" diff --git a/Cargo.toml b/Cargo.toml index 777ff2b..10ce7ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ publish = false [dependencies] anyhow = "1" clap = { version = "4", features = ["derive"] } +nom = "7" diff --git a/input/day3/example1 b/input/day3/example1 new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/input/day3/example1 @@ -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/input/day3/example2 b/input/day3/example2 new file mode 100644 index 0000000..30032cb --- /dev/null +++ b/input/day3/example2 @@ -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/input/day3/input b/input/day3/input new file mode 100644 index 0000000..d6f8c63 --- /dev/null +++ b/input/day3/input @@ -0,0 +1,6 @@ +@][mul(345,766);'(^mul(343,555)mul(532,723)'>mul(810,622)mul(163,901)select()@?who()select(643,43)'mul(598,56)#;do()from()~where()mul(905,791)select()/why()-when()~;mul(767,89)[#?&mul(874,668)!what(334,491):@@don't()#what()+{}who()mul(746,753)how()~+! -select(814,465)&mul(961,917)$!}?who()from(239,902)]mul(578,658);~' ]mul(693,262)~;mul(830,470)<%mul(310,717)what()when(392,442) :#${@mul(73,255)mul(673,551)+$[from(780,580)-mul(774,338)*:why()do()what(), >{* select()^mul(802,740)[#*mul(243,183)&don't()mul(806,801)from())where()(who()}>-mul(56,431):{,@/,<#mul(399,880)}how()what()~mul(512,540)}**,where()do()when()??what()>$*mul(365,444)who()]*where()> ^?%mul(114,504)what()who()~from()where()):who()how()<<:mul(953,560)mul(174,660)why()$#~mul(802,446)]from()?*%mul(306,304)who()where();mul(241,492)from()who()why()~from()&where()&[?+}who()mul(870,405)!mul(422,189)+]'what(588,411)select(58,996)mul(276,921)!why()#what(937,725)(where()mul(913,207){select() from()%[{}?;mul(818,262)&/]]what()'+?~&mul(440,835)?$)what()**(mul(347,140)%+mul(12,361) )what()@-mul(432,576)^@why()*#{*when()how()$do()@!*}:-'/~mul(310,592)&&&-:mul(492,471):{:mul(834,524)who()why()when()who())&$mul(234,340)[?;where(){$~mul(155,220)&&mul(721,750)<*#mul(778,260)%,:$why()} what()select(){mul(777,312)@)what()~select();-mul(623,101))&:/]!-mul(127,361)[)%mul(139,822)mul(465,869)mul(677,898):{)'~mul(240,110)/who()mul(994,931)mul(449,745)?select()/what(),]/:mul(217,696)what()!mul(221,836)^^;where(481,276)mul(871,646)]^)(^select()!mul(649,851)why()>how()do()?;why()#what()how()mul(203,549)--^what())$:%~[mul(566,723)@,@when()@{mul(71,88)who()[{?mul(680,450)where()$select(838,381)?^from()mul(43,229))mul(715,689)what()*+who()%:select()mul(244,492)^what()!~ ([~[mul(114,868)what()~#when()+?mul(57,122)[?%[, mul(569,552)',:don't()from()where()select()%who()!,[mul(760,25){~'?do();]]&when()who(439,358)how()mul(352,496)how(859,494)-what()$,mul(986,853)((mul(43,897)who())+$;&mul(207,469)]^/mul(237,787))!when()'@+what()?]mul(542,157)mul(466,974)how()^+!!-[#[why()mul(678,165)[(:}#'*-!mul(616,117)select()mul(904,600)![why():)who(),$who(904,821)/what(116,194)what()why()mul(720,13)mul(18?/^where()-mul(468,137)}$^(mul(990,391)mul(284,783)select()*how()%>mul(788,482)}#why()mul(211,898)where(24,695)-$mul(237,249) +:/@+@+mul(280,976)what()what(688,228)when()what()~from()?,/mul(920,121@?]^^mul(102,138)] :}when()&#~mul(171,496)-!mul(775,530)what(40,18)>[why()mul(178,21)mul(533,267)]&,<~-mul(576,4)*<@how()select()what():!mul(436,336)*what()where()>&who()$mul(463,367)mul(102,156)~mul(304,732)mul(970,264)({how()what(27,23)-'do()$*why()(what(932,987)#where()mul(807,956)&:((@()mul(773,746)-'where(); mul(445,526);who())why()^how()how()how()mul(420,885)]){mul(588,810&;how()!~don't()-&&)from()(<{from()%mul(138,683)$>why(707,353):how(){%+mul(152,58),((?!:+$ mul(43,304)+mul(715,799),what(),when()select()>select()mul(844,292)@:,'mul(900,791)[;#when()@]%mul(896-:'%>mul(123,811)when()where()why()*>]what(768,291)from()(/mul(612,112)mul(320,45))~&where()((who(68,102),mul(263,865)*why():><>%mul(628,402)don't(); $@how()!^: mul(947,979){-/(-mul(441,144)*;select(599,589)${why()-mul(103,568)}who()what() how()}how()<]mul(239,536)[*mul(537,157)how(640,298)how()@%)who()*mul(142,857) >from()!when()/*how(830,843)mul(199,630)select()do()*>/mul(63,689)$-/[;[when()why()mul(216,315)^where(),mul(181,124)why()%mul(696,385)~~;who()!][:}~mul+*?[}{[?'mul(23,341)[select()}%what();who()-from():mul(655,135)^from()&~^how(),mulwhat()mul(661,325)mul(394,949)!$?&^where()'from()mul(564-*?$-[mul(325,618);,]??mul(241,786)@!(,mul(395,180)(%)(%mulwhy(880,593)mul(543,182)!:'}why()what()from(75,377)/mul(242,439)^what() mul(211,696where()?@don't())how()/who()'how()~mul(487,649){{mul(761,854)?!mul(402,396)where()((who()why()don't()!:}{<+-mul(51,725)@where()from()>+{<%;-mul(471%/!from()$)-when()-;mul(468,893)mul(779,665)/what())&^-/mul(82,47)(/$how()[<<#@mul(966,718)]%mul(613,225);select()&%:~%&mul})[from(),[when(43,235)$}mul(635,416)?;%'~who()select(795,993)-mul(729,69)@[mul(754,156)>#]who()%from())what()mul(331,744))mul(536,505)>who() mul(270,727)from()+select()@mul(584,914)^what(188,788)what()mul(770,597)mul(566,371)<{{!}where()mul(431,257)~%{;/[don't()!mul(441,879))select()+~[why(206,654)+&mul(675,232)why(){who()+#how()where()when()mul(803,318){:mul(193,950)$when()?}}mul(850,957)*:~mul(481,93) +mul(473,855)mul(714,653):&'mul(98#mul(23,988)who()mul(561,251),@^){?&&mul(315,596)!'why()?, mul(151,933)]who()mul(883,474)$$#where())who()select())don't()why()where(),mul(41,669)! 'why()< mul(73,433)*[what(985,767)<]when()mul(258,921):}from(88,48)from()>~&:]mul(106,615)%(why();~what()[mul(278,729^'[^how()'/!$ $don't() @-where()-$mul(748,984)when()#mul(366,193]where()$(#[mul(650,149); /when()${(mul(386,30)+,/$$$!^$don't() {how()mul(903,99)&/mul(992,909);}-/$'who()when()don't()from()~where())~mul;#who()why()mul(944,954):)#:> ]when()';mul(953,447{+]what()-?'(')]when()mul(256,864)mul(359,893)mul(715,923,where()why()why()where(163,418)>^mul(550,432)&what()who()where()[@where()why()mul(412,403/{*;#-)&do()what():mul(228,774)&[mul(386,547)+{from()mul(259,966) )#,/*select()+where()mul(59,320)!who()select()]mul(837,608)when()mul(862,367)where()where()*+)!:-:mul(639,867)?#-/from()<~do();!^@]>>mul(417,713)]why()*mul(751,602)%)mul(731,827)^(!mul(302,377)&!:)$>mul(814,103who()/]$mul(658,242)mul(393,286)(~{{who():-}[mul(143,747)$mul(654,923)when()when())@<-&from()mul(412,619)%'>mul(94,395)^where(36,302)mul(543,140)mul(894,717),where()who()~why()mul(194,32)( !where()how();^mul(952,751)*mul(564,368)&how(35,521)how()[::;:mul(578,119)^why(849,522)%when()?when()why()}mul(257,172)who()who()where()what()from()#>#what()mul(460,165)where()what()select()when()}select()when()!!select(258,759)mul(322,660)/]what()[%mul(341,188)<)(-^who() don't()%*mul(179,768)from()}>mul(633,949)&why()}mul(986,569)from()(!why()select();mul(973,414)? 'from()how():@from()(mul(904,727)]why()]+mul(201,762)(how()who()what()how()(^mul(903,105)-+how()~?!+mul(344,148)##-mul(921,159)<$from()?--mul(885,837);,]why()from()*select()[who()/mul(542,794):{from()how() ^,mul(511,377)>:why():) when()}*when()mul(991,899)<(/+when())mul(244,653)%where(),where()why()(who()-mul(864,493)~where(645,381)who()where()mul*%)what()mul(585,982)](~mul(104,216)mul-]%select(573,835)>/-mul(888,953), ? !mul(969,193)who()how()^[when()who()%do()$,{%:()#mulwhy()%'+,}/how()-~when()mul(351,397)!;+ @^&mul(591,394)mul(581,992)from(){#)when()/ don't():}select()mul(833who(),when()how()*;when()mul(366,958)mul(41,963)mul(653,973);from()]-mul(902,82)mul(230,836)-when()[when()select()}mul(265,919);mul(748,89)!mul(3,621) ]](don't(){%:;,~&mul(378,465)}@>&mul(637,901)where()~@don't()[<;@>@<^mul(138,394)#!/usr/bin/perl]who();]})mul(356,794) +)*/}&{mul(151,94):who()!!!~mul(92,956)mul(188,489)!mul(52,932select(),where()select()$@@[;-do()#%]%:from()&why()mul(416,222)mul(78,182)%who()>{where()+;how()!@mul(456,135)('{how() ),~mul(469,864)$where()select()<#%?;mul(80,492)@][;<)?mul(970,872)-!~>$mul(989,699)where()<*~mul(998,127)mul(246,169)>,&,mul>(mul(564,254)?')from()-why()select()&mul(585,28){++select()who()who()!select()mul(105,926)~{)/@$#?mul(903,224)what()mulwho()<(mul(377,343),#+mul(731,518)/]~mul(104,159)<[ mul(864,248):^~[$;do()mul(671,810)from()(mul(373,295)-mul(612,243)*'why()'#mul(517,385)%-'mul(569,660)mul(149,268)why()mul(763,824)]^}!:[mul(432,368)mul(355,593)mul(325,34)%#,}(+mul:%select()-%%;why(),(mul(601,765)&%-//where()from()select()what(),;-mul(174,657~ where():when();&mul(818,690)%) 'how()mul(444,450)(mul(811,633)don't();~&/mul(41,34)why()! } >,mul(657,550)mul(341,872) #when()>+/why()&what()don't()*mul(805,568)&}!(#>-@(&mul(938,730)-(,?mul(509,112)?select()+?how()mul(765,701)(-where() what()how(){mul(800,467)[]/mul(401,967)/ +<&*]mul(152,480)do()??why()select()select()&mul(367,114)-mul(857,679)%&@>#mul(710,252)how()<;select(71,168)(^[*mul(106,321)'<>/[what()mul(393,583)mul(733,744)mul(731,76)]?what()$&select()mul(163,611)+})mul(44,657)%+}(when()mul(326,630)mul(316,4)mul(102,24)/^)when():~#%@{mul(586,357)*<;why()-where()~mul(830,113){#when()mul(854,851)-where()from()from()!',@mul(884,829)]-?$%-!who()mul(634,11)/when()mul(940,730)}who();+select()<<@mul(315,377)&:mul(947,179)}^<,&who(954,59)/'mul(354,837),select()from(266,967)['*mul(718,126)$ @*)why()mul(392,406)%*{select():from()[%why()mul(667,160)&%,(who(954,354)/where(){!^mul(673,986)>;what()$select()mul(782,955)!how()mul(922,151)?why()]-^from()mul(473,251)when()$';~?%]what()?+;#{what()mul(488,503)^select()?do()from()when(),^!mul(85,942)-}}mul(227,390)mul(270,642) #where()#?-why()mul(843,712)mul(853,87)what()'!who(682,875);,mul(132,10)??when()select()[%mul(440,436)how()>>^mul(332,464)$':;]~>](from()mul(251,318)why()mul(540,277)who())where()' &}] mul(937,405)>->mul(898,676)what()when() +)<>@mul(796,954)^;$$when(752,554){mul(28,311)@!;)what()>+don't() }%where()'>mul(903,227)+@??+!mul(624,625)/>from()<:-what()@mul(216,114)&>select()mul(743,469)?where(956,943):];%from()do()&when(){from()@why()how()mul(611,802)where()why()[mul(561,982)who() *@from()'(<(mul(207,619)]$+mul(846,461)what()what():mul(552)-how()^/why()mul(163,195)-)mul(753,176)what()~mul(910,478)mul(209,230)&:mul how()+)select()don't()where()mul(106,878)~how()how()^'+[who()mul(86,13),mul(200,412)mul(285,237)when(){/,how()!'@mul(308,309)(#?select()(>!who()who()&mul(880,171)where(264,559)where()>where()do()what()+[(}where(259,87):!mul(709,722)how()>{who()$*>mul(527,907)from()how();:mul(670,931)%)'mul(855,264)*,>~{mul(548,28)((mul(599,821),}where()from()from()when()]?-mul(818,682) why()when()^#~ 'how()mul(152,639)@^from(216,399)~who()mul(441,246)?:'<+#mul(763,310)don't()-'mul(289,172)from()'(!don't()~]^(where()&mul(218,233)mul(390,203';mul(535,511)who():}mul(685')(mul(922,128)$$mul(451/from(758,307)#:]/#mul(203,808)-#,-what(535,949):do()from(917,83)[ why()from()why()mul(338,424);who()what()+mul(170,324)how()mul(895,778)<^ 'when() where()>what()%mul(591,815)~how()who()mul(11,111)(~why()<}:why()mul(416select(550,793)select()&@] #what()mul(229,407)don't()when()what()}from()+(;when()(mul(18,891)-*+{where()do()#mul(435,135),>what()^^~when()*?when(791,112)mul(368,920)$/select()where()#/&what()mul(351}&)%from()mul(930,157)how()where()>who()<])mulwhen()}mul(682,770)from()%/:](mul(537,172); },what();!{&mul(289,489)>@ ;] where()mul(576,338)?~*~%{mul(854,88)&what()mul(726,742)who(32,67)why()@(mul(768,203)@when(23,700)what()/mul(468,338) +!-<%>>^/~!mul(543,650)[<*<((mul(528,681);?*$-when()> }%mul(746,92)from(327,471))mul(189,783)(:,(+how()@*mul(560,503):&;$mul(324,880)why(){who()mul(70,798)~{mul(465,724)!+how())%~mul(85,858)why(520,207)from()&-when()[mul(951,807)'& !mul(678,463)?select(439,910)[%?mul(971,903)where()<~}){)*[mul(712,760)%how()*!!%mul(752,188)'when(729,35)mul(428,573){-~(,;why()mul(461,956)where()mul(456,895:,from())mul(353,754)<( *mul(141,974)why()why(){mul(497,471)]+~[why()mul(115,659)^@mul(662,675)$[}-?@&mul(34,6)why()mul(941,440)$what()%select()when()mul(103,793);why()>?when()what(){from()?why()mul(999,411)['}[~?& how()mul(487,886) :how()@{mul(41,651)mul(392,297)mul(681,343)@&)/how(692,299)who()/*$do()$mul(326,461)-how()>when()(when()do()';~what()why()* *select(839,422)mul(747,631),when()what() )@*don't()what();from()mul(358,222)]mul(100,430)when()+select()(!:,{who()#mul(761,79)*^mul(701,138))from()?,how()mul(389,248)]:-(#?select()+how()}do()[?when())from()>(mul(403,415)why()don't()when())*mul(31,786){mul(219,86)mul(360,469)when()from()$}/why()mul(851,189)*from()()from()? :!who()mul(746,316)(what()~!+-[#select()when()mul(714,622)!:~select()how()#&when()mul(222,991),/where()select()mul(389,819)where()*what()/do()<&^{$mul(218,540)select()how();& ~where();%mul(593,988)where()},mul(277,30)#]+}mul(896,116)mul(59,615)who())do()!-why()?:when()>>when()mul(367,136)why()##what()what()mul(185,184)$~)+[what() mul(225,500)*why()what()'+who())}mul(178,560)select() when()don't()why())>+}>why(),>,mul(514,558)'?< >-'?,mul(36,747){~-why()?)who()~:mul(287,362)&mul(32,838)how()from()#when()what()%;who()mul(62,989)mul(199,673){*~mul(315,155)!where()$why()?:mul(10,649))'-{'mul(629,27)&#)'[who())!mul(195,137)%,what()~#}who()mul(606,783)!mul(481,349){mul(28,389)mul(628,171)why()mul(208,827)?}{~why():why()don't()&,from()why()/%mul(735,558)@<%; where()select()+mul(178,387)+:>~?#where()-how()^mul(565,609)#$,why()<{^+:mul(130,74)from(815,775)){/#what(60,600),>where()#mul(347,919)what():/mul(377,21)>>@'^where()@/mul(395,896)who()/~{what(347,8)#(mul(335,630)/ anyhow::Result<()> { + println!("part one orig: {}", part_one_orig(input)?); + println!("part one: {}", part1::part_one(input)?); + println!("part two: {}", part2::part_two(input)?); + + Ok(()) +} + +fn part_one_orig(input: &Path) -> anyhow::Result { + let mut reader = BufReader::new(File::open(input)?); + let mut buf = String::new(); + reader.read_to_string(&mut buf)?; + + let mut answer = 0; + loop { + if let Some(mul_beg) = buf.find("mul(") { + if let Some(mul_end) = buf[mul_beg..].find(")").map(|i| i + mul_beg) { + let mul_input = &buf.clone()[mul_beg + 4..mul_end]; + let mut mul_inputs = mul_input.split(","); + + let mut err = false; + let first: i32 = match mul_inputs.next().unwrap_or_default().parse() { + Ok(f) => f, + Err(_) => { + err = true; + 0 + } + }; + let second: i32 = match mul_inputs.next().unwrap_or_default().parse() { + Ok(f) => f, + Err(_) => { + err = true; + 0 + } + }; + + if err { + buf.replace_range(mul_beg..mul_beg + 4, ""); + } else { + answer += first * second; + buf.replace_range(mul_beg..mul_end + 1, ""); + } + } + } else { + break; + } + } + + Ok(answer) +} diff --git a/src/day3/part1.rs b/src/day3/part1.rs new file mode 100644 index 0000000..6fafeba --- /dev/null +++ b/src/day3/part1.rs @@ -0,0 +1,52 @@ +use std::{ + fs::File, + io::{BufReader, Read}, + path::Path, +}; + +use nom::{ + bytes::complete::tag, + character::complete::i32, + sequence::{delimited, separated_pair}, + IResult, +}; + +#[derive(Debug)] +struct Mul { + left: i32, + right: i32, +} + +fn parse_int32_pair(input: &str) -> IResult<&str, (i32, i32)> { + separated_pair(i32, tag(","), i32)(input) +} + +fn parse_mul(input: &str) -> IResult<&str, Mul> { + let (rem, (left, right)) = delimited(tag("mul("), parse_int32_pair, tag(")"))(input)?; + + Ok((rem, Mul { left, right })) +} + +pub(super) fn part_one(input: &Path) -> anyhow::Result { + let mut reader = BufReader::new(File::open(input)?); + let mut buf = String::new(); + reader.read_to_string(&mut buf)?; + + let mut answer = 0; + loop { + if buf.len() < 5 { + break; + } + let (rem, mul) = match parse_mul(&buf) { + Ok((rem, mul)) => (rem, mul), + Err(_) => { + buf.replace_range(0..1, ""); + continue; + } + }; + buf = rem.into(); + answer += mul.left * mul.right; + } + + Ok(answer) +} diff --git a/src/day3/part2.rs b/src/day3/part2.rs new file mode 100644 index 0000000..343b27c --- /dev/null +++ b/src/day3/part2.rs @@ -0,0 +1,82 @@ +use std::{ + fs::File, + io::{BufReader, Read}, + path::Path, +}; + +use nom::{ + branch::alt, + bytes::complete::tag, + character::complete::i32, + sequence::{delimited, separated_pair}, + IResult, +}; + +#[derive(Debug)] +enum Instruction { + Do, + Dont, + Mul { left: i32, right: i32 }, +} + +fn parse_int32_pair(input: &str) -> IResult<&str, (i32, i32)> { + separated_pair(i32, tag(","), i32)(input) +} + +fn parse_do(input: &str) -> IResult<&str, Instruction> { + let (rem, _) = tag("do()")(input)?; + Ok((rem, Instruction::Do)) +} + +fn parse_dont(input: &str) -> IResult<&str, Instruction> { + let (rem, _) = tag("don't()")(input)?; + Ok((rem, Instruction::Dont)) +} + +fn parse_mul(input: &str) -> IResult<&str, Instruction> { + let (rem, (left, right)) = delimited(tag("mul("), parse_int32_pair, tag(")"))(input)?; + + Ok((rem, Instruction::Mul { left, right })) +} + +fn parse(input: &str) -> IResult<&str, Instruction> { + let (rem, ins) = alt((parse_do, parse_dont, parse_mul))(input)?; + + Ok((rem, ins)) +} + +pub(super) fn part_two(input: &Path) -> anyhow::Result { + let mut reader = BufReader::new(File::open(input)?); + let mut buf = String::new(); + reader.read_to_string(&mut buf)?; + + let mut answer = 0; + let mut mul_enabled = true; + loop { + if buf.len() < 5 { + break; + } + + let (rem, ins) = match parse(&buf) { + Ok((rem, ins)) => (rem, ins), + Err(_) => { + buf.replace_range(0..1, ""); + continue; + } + }; + + buf = rem.into(); + + match ins { + Instruction::Do => mul_enabled = true, + Instruction::Dont => mul_enabled = false, + Instruction::Mul { left, right } => { + if mul_enabled { + answer += left * right + } + } + } + } + + Ok(answer) +} diff --git a/src/main.rs b/src/main.rs index 020cbe5..d8de2f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,12 @@ #![allow(clippy::comparison_chain)] +#![allow(clippy::while_let_loop)] use std::path::PathBuf; use clap::{Parser, Subcommand}; mod day1; mod day2; +mod day3; #[derive(Parser)] struct Args { @@ -22,6 +24,10 @@ enum DayArgs { #[arg(short)] input: PathBuf, }, + Day3 { + #[arg(short)] + input: PathBuf, + }, } fn main() -> anyhow::Result<()> { @@ -34,6 +40,9 @@ fn main() -> anyhow::Result<()> { DayArgs::Day2 { input } => { day2::solve(&input)?; } + DayArgs::Day3 { input } => { + day3::solve(&input)?; + } } Ok(()) -- 2.44.1