From 481270abaf4021ceeaa08d3987f2f3d85c1ee9d9 Mon Sep 17 00:00:00 2001 From: Felix Suchert Date: Fri, 13 Dec 2024 02:41:48 +0100 Subject: [PATCH] Start day 3 --- inputs/day_3.txt | 7 +++ src/main.rs | 3 +- src/solutions/day_3.rs | 109 +++++++++++++++++++++++++++++++++++++++++ src/solutions/mod.rs | 1 + 4 files changed, 118 insertions(+), 2 deletions(-) create mode 100644 inputs/day_3.txt create mode 100644 src/solutions/day_3.rs diff --git a/inputs/day_3.txt b/inputs/day_3.txt new file mode 100644 index 0000000..9890df6 --- /dev/null +++ b/inputs/day_3.txt @@ -0,0 +1,7 @@ +]select(23,564)/$!where()>%mul(747,16)*why()mul(354,748)how()$(when()<:mul(428,793) where()from()how()/how()]*?mul(156,996))what()!,what()~@((mul(976,569)]-,>$-~%;mul(426,703)/mul(948,128)>+?+>?%select()*mul(477,567)why()%select()?!(@~how(){mul(182,79),mul(203,707)?[mul(186,170)select(283,626)*/*when()mul(130,392)')^&when(),[;mul(563,902)where()}*}<$/)how()mul(953,129)!!what()#what()!who()mul(852,652)~)+mul(973,163)$?why()]where()mul(158,596)when()@}what(29,454)mul(968,252)<'^'how()when()<*^mul(617,885)when()) +&;'mul(264,456)/mul(713,804),-mul(803,862)mul(575,310)[ why(527,60) )from()mul(475,876)from()when()*^$@:do()mul(557,2)'{^:-*what()mul(611,157) >- when()mul(894,415)!mul(856,397)from(),where()mul(13,373),!where(),do() {how()select()^:(#select(622,699)[mul(395,375)-##>+[what()?mul(535,15)/(];)mul(115,296)mul(201,604)^+[>+do()&:}how()/:mul(34,586)?where(375,645)?:-who()select()'why()>mul(389,101)don't()<^}who()mul(501,691)'select()mul(551,120),]?from(545,381)?*%~mul(492,926),:(who() {$ when()mul(348,721)'?/)?!what(784,670)mul(811,483): where()why()why()>$[when()do(),~*# {/mul(312,382),}*what(944,486)?^{+%mul(224,412)~why();?<]who()*^mul(199,783)what()from()@why()where()what()?select()(}mul(267,247) mul(126,337)select()mul(534,156)($%%}+*@mul(103,848):;'%mul(237,35)<&-where()mul(423,484),!]where()#!mul(281,866)select(750,996)(( *{<^%who()mul(437,982)}:mul(357,682)@< mul(124,834)}~mul(668,671)mul(787,282)from():where()'[mul(985,702)*{: -&where()how()mul(180,738)(from()@mul(240,76),[:'#!:select() mul(822,179)*#how()~!%!who()who()how()where(184,532)#from() [mul(771,388)how()'~!^!@+mul(646,938)+,(({-mul(486,708)^%^from()-(;what()]mul(144,833)~why()%select()&<~how())mul(439,873)mul(677[[;{:?{>[ (mul(25,577))@:mul(727,412)why();?select()?what()};from()*mul(826,116)#*)/where()who();<@!where()~when()where(597,883)-mul(835,616)'((where(808,96)',mul(649,224)&/ mul(35,958)who(871,394) :!-who()where()where()(mul(322,104^what()%,}[why()what()**who()mul(983,838)mul(614,657)what()&,mul(238,871)-{},select()>who()#>mul(943,599)select()select(558,572)?^who() <:mul(572,265))who()[why()!$,-mul(454,326)mul(300,416)what()who()what()[;when()mul(786,381!&who()when()where(381,479) from()!?$,what(),who()mul(429,727)}from(401,661)>{?%mul(518,766)-@who()!mul(276,326),select()%{mul(211,710)/mul(414,532)@!-,>mul(494,611)?%((@)[&who()mul(547/why()]who()*% $'who(675,908)mul(90,974)}mul(427,683)how()[:;mul(443,135)*^+~^{when()who()}[mul(579,135)@:who()mul(267,452)[&!;;where()$}who()mul(662,85)~>what()mul(724,771)$!mul(206,909)@^%mul)when()select(567,468)mul(260,632)who()what()<}what()}@do()mul(866,137why()~)mul(13,816)^!*(mul(351,795)from()(?^ ;;,~'mul(313,157)?mul(222,186)!> &how()$mul(558,129)how()[select()from()'/&when()[^mul(927,606)@?<+how()-}(mul(749,285)!![%~>mul(919,804)+&-->where()!&$/mul(889,472):why() <]++from():)mul(597,828)!*~@mul(61,536)(why()what(): >why()*mul(50,308)mul(980,618){-! ?*why() *mul(506,77)#/where()^~';who()%who():mul(11,806)mul(226,600)?how()% /{//mul(601[><<&mul(70,238)select(176,735)mul(447,978)(^#mul(583,880)@[mul(722,873)>mul(110,728)/+mul(948,566)where(760,139)[ -*why()-mul(92*,:-$how(),where()select(),+mul(954,569)&?#+(mul(482,680)]mul how()&what()%-mul(455,447)-from()+>?[/where()!:mul(502,951)?~mul(953,617)[/]&>^when()mul(234,738who(134,419))&$-${)when()#why()/mul(266,78)&- ;mul(336,100)?$'#{'~:^mul(963,726)/&@mul(738,99){where(903,414)how()>)mul(249,770)$&@ ]from()$how()!select(521,465)mul(617,783)}<>):[,select(333,996)mul(416,179)[[don't()$]>why()@:mul(148,463)from()-do()when()-/how()when()&from(930,269)[mul(10,173)#mul(613,997)(>when(); *'@mul(724,407)don't()-when()why(533,992),')(:mul(472,652)?mul(350,214)!>where(401,417)/from()$where()mul(108,897)*where()<%select();,$mul(177,623)mul(674,586);mul(188,601)where()&/]mul(892,181)$from()?mul(904,15)why()(from()select()?@{?mul(832,651)<)mul(485,621)?%from()^);from();-who()mul(964,655);((select()mul(950,896)mul(103,475)when())/mul(165,476)(*},)where()!(@mul(417,206)&]?why()%-{[select(),mul(162,447)don't()why()!(/# from()~!mul(794,412)':&?why()mul(419,168);&who()%]who()mul(689,382)who(754,484)select())@)#[('mul/#/,select() why()??&mul(868,352)!from()!who(201,267)what(971,755)mul(245,790):!(^~&^where()--mul(334,775),why()why() when()'@?mulwhen()$?!]- '~@)mul(874,152)when()mul(771,202)(>@~;who()/]@mul(331,997)] select()%&mul(230,638)<%/mul(729,194)when()why()-@-select()select()'!&mul(669,652)/@%where()*;mul(799,156)from(){%why()mul(666,169)-#mul(381,557)&'??>+/>[what()mul(534,338)(mul(631,275),#[{when()~select()mul(847,310)when()]~}where()what(661,551)from()!mul(321,129);$where()@ why()where(616,873)-~(mul(888,479)who(),mul(229,502)])when()don't() mul(246,553)why()?select()',*?mul(242,385)}}where()mul(522/why();mul(445,61)mul(746)$why()from()don't()+mulfrom()#mul(521,245)]%?]}mul),[who()where()mul(784,56)!why(): when(893,294)&[mul(373,342)<$ *! {[why()mul(513,340)how()}where()'[^)^mul(967,981)^from(740,8)^{what()')mul(244,532)+ who()/*mul(686,40)>mul(178,177),][)-<([mul(595,777)<-how()>(,)}mul(210,328)?why() )$why()where()/['*%mul(654,641)select()$[select()mul(620,43)$from()!mul(573,638)<,#/>why(451,392)/how()mul(450,634) ,%@&,don't()select()~~}%select()when()@%@+!^%mul(621,328){@{)when()#)who(685,504)mul(179,100)select(){from()why()]>-mul(94,187);mul(509,291)-how()when()do()why()@) how()select()+mul(379,538)mul(830,641), /&+-@mul(180,491)how():why()!!'%%where()mul(266,732)when();select():+how()(;%mul+who()}{+do()mul(575,752))how()mul:&from()>who(854,820)mul(850,918)&select()(}+}mul(782,495))<'?(>){where(),mul(957,190)/when()what()who()mul(352,79)why()'mul(652,626)mul(973,815)>[)-];@'mul(327,736)$@mul(221,196)~;>,[what()mul(170,288)what()~:$what()'from(468,557)%&>~who()how()mul(847,27){where()?'mul(736,195)mul(663,417)%mul(301,538)don't()what()select();+[how()?+}/mul(271,885)[when()] select()mul(251,286)],{-why()how()-% what()mul(664,742)from()>^!&mul(583,557)%#{}where()why()))don't()[^[mul(636,736)mul(733,889)mul(415,907)#what()what()from()where()mul(746,170)what()how()%who()/mulhow()don't()*'why()[select(769,463)$'/from()mul(520,584)*who()from()~]>who(862,907)why()mul(423,117)why();mul(100,371)!@<{from()mul(434,31) +*why(816,889)& &:!#!/usr/bin/perl/<$!{ (mul(142,115)where()[}/'when()mul(444,725 ~*:why()& from()when(493,415)&mul(863,234)<@?$/when()'+,mul(722,893),%'don't()~where()why()^,;%;%mul(823,490})$<*$#mul(101,716)&from()%@who() what()mul(881,570)who()@+(/@mul(676select()when()*;@@]why()#mul(315,278)[%'select()[{[mul(237,596)mul(782%^<^+:/:~how()when()mul(565,260))~where()??mul(4,427)'where()(when()?-}select(314,151)mul(361,206)-what()!!mul(575,211)!@}select()% ;mul(996,776) how()!#'+)what(861,201)who()]when()how()what()#mul(862,926)when()*;#:['mul(189,238) :[how()who(971,4)~+<(mul(481,269)%mul(632,943)from(){{}'why()who(); select()mul(364,748){;,mul(214,990)from(){>$?:why())mul(206,633)where()what()]*@<$who()}+mul(14,141))mul(822,373)]? how()[from(){mul(978,657{,%]{^mul(688,489)##![why(){}do()#why(),];)when()who()mul(305,206)<]<~ ]don't()?mul(304,632) [mul(291,282)+}{when(),from()#mul(662,53)}mul(5,779)#mul(111,303)where()+!<*'%how(960,285);>mul>mul(852,693)%;)*$':;]who()mul(249,548)@)select()]mul(387,459):/'when()$-[>!mul(112,988)from()@~> $mul(191,655);mul(79,498)~)<@/$@;when()#mul(513,202)][*how()$[from()mul(224,73)&'-&,why()@#from()mul(737,275)who()-[from()mul(816,564)mul(66,866)'from(239,497)how()>>%/<%who()mul(846,687)how()how(854,968)don't()@@[*>where()!mul(268,28)+[#(? -mul(398,631)&?}}^-$&~mul(283,82)@what()don't() what()mul(720,734),? who()mul(380,889)%+select())*how()how();%[mul(48,988)$;mul(968,247)* mul(884,462)*#from()>mul(366}what()]((<+mul(133,244)*'{-where()mul(729,105)how(619,497);from()+from()when()when()when()from()mul(900,829)$mul(83,600)*how()why()mul(434,610)don't())(?@^,mul(691,992)}^:why(784,454)don't()$&#mul(892,279);':what(){~when()what(361,846)']mul(720,864),why()where()mul(303,491)&;[what()mul(720,732)$~,who();/!mul(407,549)-select()!mul(127,272)-%['???select()what(412,125)]don't()&where()]mul(322,71)@/+select()why()$?mul(650,186)&@>@[:![mul(887,229)why()]select(298,963)where()when()?where(992,533):mul(753,118)[what()what()$@(what()]mul(683,687)@[mul(471,355)why()who()$>^?how()mul(271,372)mul(68,707)#when(358,219)'&who()mul(811,52)who()<-:#(from()~mul(160,946)!'@>?[;what()mul(206,444)[$why();why()&:mul(299,200)?what()[what() (+mul(298,431)<$^'(mul(822,566)why()when()+how()-#]]?mul(401,194),mul(320,99)when()*$where()!+]mul(635,430)mul(474,531)%' }mul(896,524) +@who()$don't()who()#&##mul(245,843)mul(707,597):!,mul(864,931);$mul(605,621)&@mul(700,646)>%what(225,888)!%who()[:select()mul(297,15)]^%~]who()mul(87,793)?+{<-where()%mul(518,305)why()+,(~ /!mul(588,968)mul(295,237):-,}%:{+:who()mulwhat(){~*~/'<>mul(590,124)/mul(719,423)))?:+select()mul(902,901)when(){>mul(455,460)$,what()(&{}how()*{mul(24,765)/]mul(529){[#+>/select()?(+mul(19,771)where()+/+mul(380,138)who()when(){from()@mul~$/don't()))select()mul(333,659)< select(213,174)*!select()mul(310,90)mul(494,223)!$when()~how()@mul(656,481)!*!$mul(705,700)mul(7,569)!#;%}> '?mul(324,975) ^/who()$~^ from();do()~!mul(883,226)]mul(660,990)*select()select()[#}!^mul(426,750)when()')#(when()$select()#mul(790,641)what(196,61)&?where()what()mul(751,795)why()#what()/((*who()select(465,475)'mul(633,76)~%&don't()}/why()where()mul(320,249)who()^{how();?'mul(239;;[}]*(;//mul(51,601)%/~%+!'!how(665,519)mul(611,916)what();mul(174,972)when()-where()-how()('do()mul(108,242)?+mul(870,660)]when()%what()?<:mul(635,416)why(924,420)[<$from()>how(260,907)#where()mul(236,788),,@?from())>mul(97,620):select()!+mul(899,189):!}mul(566,974)'why()select()*where()what())why()~why()mul(908,528):)}^mul(71,100)from(296,15)when()from())&from()#how(869,278)mul(628,153)how()mul(765,691)*mul(186,21)how()/mul(487,725)do()##/::from(){what()%)mul(110,152)*&**,$ from()*mul(992,213)[*/!~mul(797,726)-from()mul(969,578)when(112,655)*how()when(),where()'(mul(440,56))*{'(from()-&mul(113,81)^^what()#!))')[mul;~mul(147,629)!@<#&^]@select()mul(993,659),{ mul(254,347<<>}why()(who()>!)how()mul(518,738)what(29,303)from()mul(160,896)do(){/;?what()where()where()how()mul(173,649)mul(969,370$~>mul(679,266)how()>@):@where()mul(206,774))[from()what()where()mul(950,797-) ,^%%mul(996,344)what();select()#mul(332,248)*!'<&~mul(408+how()mul(114,169)$@~select()@~who()mul(901,342)*mul(499,634)%!mul(996,710)~/$}select()mul(8,949)$;(!};from(): mul(860,166)(@-from()'',mul(739,160)$%who(265,676) mul(988,26) ,+/mul(293,416)where()+#}-from(){(@(mul(215,463)mul(152,776)+*]'#'mul(447,726)how()-#}select()what()select(892,487)% mul(182,115)^$<(why()mul(335,275)when(801,993)];select()}'>^how()>do()mul(704,863)+why()'*&mul(709,745)who(){select()mul(17,822)}from()!+;!+,where()]mul(280,107)~?}how()/'(?who()mul(288,599)%what() ]$)mul(463,772)@mul(992,279)what()!:why()>>who() (mul(348,687)*]mul(477,896)when(47,10)&where()?what()%[mul(804,970)from()from()select()+what()[who()-?mul(166,890)&-^mul(892,532)mul(905,17)*)/]>@ {*:mul(361,902)[$]~*mul(629{*<)how()$from()#({mul(924,910)'who()why()'&)}mul(322,152) &mul(503,282)^ )/what()what()%mul(678,160)when()!?&#<%^*mul(661,59)(*mul(936]';)];)who()&&mul(437,929)who()mul(298,239)<(%:>}do()mul(727,688)~(<)who(846,894)mul(720,201)!$@)}mul(139,406)~!$mul(123,17)&select(564,607)(]:who()where();^//mul(905,342)&%)mul(185,967)--~+$ when()@mul(594,891)#:[%,;mul(641,786),@>'how()don't(),mul(185,960)(mul(702,204)]]!(%$++# mul(24,904)!mul(109,193)+[{when()mul(579,493)%where():!mul(473,16)$from()]mul(33,997)mul(104,325)'mul(486,457)select()who()<-who()]&mul(720,711)&do()how()!/!%*^^mul(239,525),from()select()mul(77,963)how()]where(){what()+mul(452,303);/~mul(55,517)&>^~when()-mul(949,308)(@~@what()@**how()mul(185,245)+}how() select()mul(89,379)~&!@ where()-) + diff --git a/src/main.rs b/src/main.rs index 9a25a4b..31560dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,6 @@ use std::env; use std::str::FromStr; fn main() -> std::io::Result<()> { - println!("Hello, world!"); - let mut args = env::args(); if args.len() < 2 { eprintln!("Usage: {} [day] ...", args.next().unwrap()); @@ -15,6 +13,7 @@ fn main() -> std::io::Result<()> { match u8::from_str(&args.nth(1).unwrap()) { Ok(1) => solutions::day_1::run(args)?, Ok(2) => solutions::day_2::run(args)?, + Ok(3) => solutions::day_3::run(args)?, Ok(_) => eprintln!("Unknown day. Are you from the future?"), Err(_) => eprintln!("Unknown day format. Use integers to specify."), } diff --git a/src/solutions/day_3.rs b/src/solutions/day_3.rs new file mode 100644 index 0000000..d1bd143 --- /dev/null +++ b/src/solutions/day_3.rs @@ -0,0 +1,109 @@ +use std::env; +use std::fs::File; +use std::io::{BufRead, BufReader}; +use std::str::FromStr; + +enum ParserState { + Search, + ExpectU, + ExpectL, + ExpectLParen, + ReadNum1, + ReadNum2, +} + +pub fn run(mut args: env::Args) -> std::io::Result<()> { + let filename = args + .next() + .expect("You must provide a filename as argument"); + + let f = File::open(filename)?; + let reader = BufReader::new(f); + + let mut sum = 0; + let mut num_buf = String::with_capacity(3); + let mut a = 0; + + for line in reader.lines() { + let l = line.unwrap(); + if l.is_empty() { + continue; + } + + let mut reader_state = ParserState::Search; + + for c in l.chars() { + reader_state = match reader_state { + ParserState::Search => { + if c == 'm' { + ParserState::ExpectU + } else { + ParserState::Search + } + } + ParserState::ExpectU => { + if c == 'u' { + ParserState::ExpectL + } else { + ParserState::Search + } + } + ParserState::ExpectL => { + if c == 'l' { + ParserState::ExpectLParen + } else { + ParserState::Search + } + } + ParserState::ExpectLParen => { + if c == '(' { + ParserState::ReadNum1 + } else { + ParserState::Search + } + } + ParserState::ReadNum1 => { + if c.is_ascii_digit() { + num_buf.push(c); + if num_buf.len() > 3 { eprintln!("alarm! {num_buf}"); } + ParserState::ReadNum1 + } else if c == ',' && !num_buf.is_empty() { + // parse the number, assign to A, set to ParserState::ReadNum2 + a = u32::from_str(&num_buf).unwrap(); + num_buf.clear(); + ParserState::ReadNum2 + } else { + num_buf.clear(); + ParserState::Search + } + } + ParserState::ReadNum2 => { + if c.is_ascii_digit() { + num_buf.push(c); + if num_buf.len() > 3 { eprintln!("alarm! {num_buf}"); } + ParserState::ReadNum2 + } else { + if c == ')' && !num_buf.is_empty() { + // parse the number, assign to b, set to ParserState::ReadNum2 + let b = u32::from_str(&num_buf).unwrap(); + sum += a * b; + } + + // teardown in any case + num_buf.clear(); + a = 0; + ParserState::Search + } + } + }; + } + } + + // Task 1 + println!("Sum of all multiplications: {sum}"); + + // Task 2 + // TBD + + Ok(()) +} diff --git a/src/solutions/mod.rs b/src/solutions/mod.rs index 6f717ed..ee3e040 100644 --- a/src/solutions/mod.rs +++ b/src/solutions/mod.rs @@ -1,3 +1,4 @@ pub mod day_1; pub mod day_2; +pub mod day_3;