From eee2d9f7d20c998964fb19c4605c287a5eda3692 Mon Sep 17 00:00:00 2001 From: SylivanKenobi Date: Tue, 3 Dec 2024 17:17:04 +0100 Subject: [PATCH] solve day 3 --- 2024/day1/day1.go | 3 --- 2024/day2/day2.go | 3 --- 2024/day3/day3.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ 2024/inputs/3_1.txt | 6 +++++ 2024/main.go | 18 ++++++++----- 5 files changed, 80 insertions(+), 12 deletions(-) create mode 100644 2024/day3/day3.go create mode 100644 2024/inputs/3_1.txt diff --git a/2024/day1/day1.go b/2024/day1/day1.go index dac2606..2b78d7d 100644 --- a/2024/day1/day1.go +++ b/2024/day1/day1.go @@ -2,7 +2,6 @@ package day1 import ( "adventofcode/utils" - "fmt" "math" "sort" "strconv" @@ -28,7 +27,6 @@ func Part1(file string) int { dist := leftList[i] - rightList[i] totalDist = int(math.Abs(float64(dist))) + totalDist } - fmt.Println(totalDist) return totalDist } @@ -54,6 +52,5 @@ func Part2(file string) int { } similarity += (left * matches) } - fmt.Println(similarity) return similarity } diff --git a/2024/day2/day2.go b/2024/day2/day2.go index 1c98a38..42e7490 100644 --- a/2024/day2/day2.go +++ b/2024/day2/day2.go @@ -2,7 +2,6 @@ package day2 import ( "adventofcode/utils" - "fmt" "math" "strconv" "strings" @@ -22,7 +21,6 @@ func Part1(file string) int { validLine += 1 } } - fmt.Println(validLine) return validLine } @@ -46,7 +44,6 @@ func Part2(file string) int { validLine += 1 } } - fmt.Println(validLine) return validLine } diff --git a/2024/day3/day3.go b/2024/day3/day3.go new file mode 100644 index 0000000..d0377d2 --- /dev/null +++ b/2024/day3/day3.go @@ -0,0 +1,62 @@ +package day3 + +import ( + "adventofcode/utils" + "fmt" + "regexp" + "strconv" + "strings" +) + +func Part1(file string) int { + lines := utils.ReadFileByLine(file) + content := strings.Join(lines[:], ":") + + re_mul := regexp.MustCompile(`mul\([0-9]+,[0-9]+\)`) + multiplications := re_mul.FindAllString(content, -1) + + re_num := regexp.MustCompile(`[0-9]+`) + result := 0 + for _, mul := range multiplications { + numbers := re_num.FindAllString(mul, -1) + num1, _ := strconv.Atoi(numbers[0]) + num2, _ := strconv.Atoi(numbers[1]) + result += (num1 * num2) + } + fmt.Println(result) + return result +} + +func Part2(file string) int { + lines := utils.ReadFileByLine(file) + content := strings.Join(lines[:], ":") + + re_mul := regexp.MustCompile(`mul\([0-9]+,[0-9]+\)|do\(\)|don\'t\(\)`) + inputs := re_mul.FindAllString(content, -1) + + do := true + var multiplications []string + for _, input := range inputs { + if input == "don't()" { + do = false + continue + } else if input == "do()" { + do = true + continue + } + + if do { + multiplications = append(multiplications, input) + } + } + + re_num := regexp.MustCompile(`[0-9]+`) + result := 0 + for _, mul := range multiplications { + numbers := re_num.FindAllString(mul, -1) + num1, _ := strconv.Atoi(numbers[0]) + num2, _ := strconv.Atoi(numbers[1]) + result += (num1 * num2) + } + return result +} diff --git a/2024/inputs/3_1.txt b/2024/inputs/3_1.txt new file mode 100644 index 0000000..c154b5c --- /dev/null +++ b/2024/inputs/3_1.txt @@ -0,0 +1,6 @@ +}}+{where()mul(873,602) mul(954,447)^where()~mul(548,799)-#why():$from()(!mul(660,615):where()mul(596,918)how()[what()how()who()]@mul(594,699)$>$&why()mul(951,813)mul(948,41)]mul(699,225)>mul(840,275):@~^@%^select()mul(695,594)what()->-mul(409,636)@ {++mul(999,290)when(339,689),[+[+?mul(558,848)how(),what()&}]{mul(344,337)$)$]+,$,,why()do()::select()^:mul(796,16)mul(526,718) mul(66,417)#@>~do()why()from()/select()when():&mul(8,111);!where()do()}@mul(433,239)*}how() {^!do()#^mul(167,905)-&%(mul(979,711)when()]who()'?mul(523,172)[^what(844,455)'what()[/~@mul(928,776)where()]&/mul(587,380)who()/when()how()from(),}#}mul(97,952)select(837,505)~@>~%mul(486,198)^!mul(968,779)do()[mul(206,321);,where()where())how()}/mul(6,867)-&what()(mul(683,583)^(mul(244,555)^where()(how()what()?-[,who()mul(827,305);&,!$*?mul(793,452)^ )+(%-where()who()from()@who(){*mul(592,919)/:what():who()mul(213,139)what()$!?*how(934,24)$-who()(mul(661,7)?from(),}/who()mul(876,585)how()why():when()[@@mul(433,918)why()why(),*{who()when()who()mul(144,14/#(don't()@(@{~why()[who()mul(394,492)where()/mul(508,797)-mul(150,832)+$@why()(why()~mul(527,236)!:@!^&!mul(324>?!from()where())mul(731,184)#(}who()'mul(666,377)how()(;mul(346,141)^select()/@[$how()@]?mul(702,502,^where()<}[mul(872,308)+^!<@from() {:mul(537,974)%when()when()%mul(563,805)select()>how()>^';mul(643,555))where(290,606)who()?mul(656,803),?$/*?when()}@ mul(228,902)~/from(){where()what()>@~mul(849,93)~+select()?-^}mul(115,425)${what()mul(219,305)how()])from()select()~]~mul(41,659)<'{where():#+)$mul(447,137)where(120,771)$@why()#mul(457,857)<$from()$(who()@?mul(644,914)}mul(936,442);{*(why()mul(804,26);don't()why(),+*how() @:mul(668,778)%select() who() (,where()mul(66,974)^)when():$who()mul(832,615)#mul(572,646)where()how()what()when(673,330)where()when()&/mul(820,446)$why()where()mul(86,788)^~what()'mul(76,413)&'how()mul(616,424)what()select()}from()(/;mul(651,36)!{ mul[*mul(469,702)who()?;mul(331,879)?*from(54,751)who()#+[ mul(182,296)where(607,734){mul(920,190) :mul(660,732)^who()who()*'!mul(3,615)+{why(168,603)when(), mul(254,713); where()*from()~ !!#mul(897,311)]mul(629;what(280,32)>mul(194,563)^&mulwho()when()'!what()why()?'}}!mul(414,213)$when()]+{why()mul(606,917)how()*?^,mul(292,543)>mul(326,145)*(&!;^:!#'mul;#@~where()%;,from()select()mul(39,372)~[when()what()!+#who()]~mul(905,968from(843,581)(from()from()~what()how()when()mul(768,210)*mul(68,62)mul(777,345%{/select()%mul(458,251));what())how()where()how()when()mul(810,199)<~mul(252,749)don't()mul(915,327)(: mul(608,270)<%[!}-&mul(328,665)from()>*)how()mul(836,471)mul(270,799);(why();&~~mul(598,772)from()>mul(409,297)$mul(469,982)mul(457,665):@>{don't()why()<+,mul(293,172select(816,987)}why()mul(372,420)why()%mul(105,735),<]{mul?{$;>from()what()-$;mul(348,90)from()what()~mul(634,662)^do()$@]*)~,&mul(226,996)when()%]what()!@/&mul(322,135);who() &^select()*&}mul(967,104)from()}'who(701,7)~%[mul(687,163)@:+:mul(382,128)mul(831,450)mul(485,444)%^what()what()[$what()@}do()-%?'mul(204,152);$'/mul(63,376)mul(387,70)~@'{,':why()(%{how()who()]',from()'how()why()mul(42,665)%'# [}mul(684,645){{%'mul(115,930)mul(552,181)mul(762,163)mul(202,193)+*[mul(525,337)+}-:](?^#mul(466,861)/do()'(;mul(860,319);mul(48,137){when() ~:< from()mul(3,497)?when()< #?^@do()>;/?:mul(522,894)+{+mul(419,20)mul(139,677)where(119,273)*what()[^?mul(563,549)how()<@'%mul(560,434)what()where(),mul(886,302)mul(632,393)*what()!}{mul(769,651)'(mul(267,514)-[how()^:mul(64,902)when()mul(356,352# ;'mul(267,302)#(mul@;~+mul(471,643)!from())!what()from()~mul(429,997)/-mul(401,220)>{]what()what()@select()},mul(294,298)how()mul(230,3)/&what()]>]!how()/mul(888,907)](#>~}+#mul(17,816)/[+when()>from()]^:mul(94,85) +#)'{mul(531,103){ {where()what()from()mul(127,712))what()!%from()'/how()*when(940,337)mul(259,916)+}!-}[mul(703,780++;select()?why()[{mul(221,199)+mul(303,268)$don't()'[what();^mul(542,561);who()&,mul(68,800)[<'{}why()?^from()mul(26,194)?]who():;mul(641,921)#&where()mul(539,571)select()]~%%]when())mul(833,642);mul(209,564)}<^)when()mul(648,430/(#)/?(';where()what()mul(818,526) ]!what()do() <;:mulfrom() @-mul(764,627)#/mul(887,607), do()$(},#^}}from()how()mul(184,287)where()$^mul(859,310){who()how()mul(692,592){>?&mul(223,912)what()^@ {{^@/mul(736,965)$)]];-#@&mul(600,145)~/}*-}how()mul(893,646):where()}mul(205,416)what()from()mul(35,844)mul(94,245))/<#+mul(189,780)from()how()#< ^don't()why()&who()where(){ >:mul(638,986)*+< ;mul(131,393),how(419,400)*@how()mul(856,729)who()$$[$[select()~{(when()$mul(613,175)}mul(503,337)~)what() mul:!-*}?~who()-mul(649,886)+how(738,973)when()[(mul(593,702)where()%%^(]who()mul(456,697)%~^'when()how()mul(789,446)mul(737,439)mul(830,910):>why()when()how()'?]why()>do()#'&why()'from()mul(972,441):mul(157,521)mul(43,224):$who()>select()how()mul(52,740)'mul(136,716)select()mul(613,433)]>$^when()why()where()how();mul(978,314))$mul(695,29)}mul(778,660){how()<+$?mul(402,606)select()+~$do()mul(262,94)how()why()))#'^$@mul}mul(201,86)*select()mul(876,626)why()mul(174,750)where()(from()(mul(433,335)]@mul(29,940) :~what()$}where()select()mul(416,920)^&%<~^mul(276,648)mul(636,17);why()}-}mul(810,73)<@} how():from(900,831)what()mul(241,497)select()when()'&{mul(603,108):[,mul(941,454) $@mul>@<}'[, $mul(510,523)where(), ~];mul(584,102)' do()why()<&select(812,485)!mul(538,458)who()why()^'select(){{why()mul(773,196)when()why(713,26){,!do()?why()!when(),?mul(234,541)mul(558]!(%;}'-mul(721,55) +(<%@$?mul(856,705]>!#'++why() >#!/usr/bin/perlfrom()mul(147,188)where()what()mul(221,687)/)from()'[{mul(9,723)[{/?who()where()mul(113,227)> ~>$)mul(663,198)&[>#mul(567,600)]when()$@select())(%<&mul(472,423)#when()>(/]select()$;how(511,981)mul(785,371}-how():$where()(?~mul(731,455)mul(810,191);who()(@>,? !$mul(534,231&don't();]why();,#;where()[,mul(190,923) }when()mul :mul(525,573<{what() *$?from()from(){mul(885,340)why()>mul(398,414)?[where()$why()from())mul(988,667)mul(828,197)'why()/%*when()/*mul(799,415)where()]<~ ?>'-don't()^[(' ]%^~%mul(122,243)why();[how(951,959)how(435,882)~*-#don't()why()[what()what()'mul(218,944)!)&>'(where()+mul(686,595)#&when()mul(916,990))what()mul(414,411)>why()&select()!}why() ?>mul(820,386)select()mul(324,473)mul(690,413)!>$why()mul(23,907)who()%mul(579,551)where()when()how()?~&@-why()>mul(359,283)[don't() }mul(238,664)!]~^^#(why()mul(128,702)mul(750,811)what()',do()'@where()select()from(),/+&mul(640,613),&%+why()^from()from()*mul(314~${~*how()?what() mul(593,269)who()*mul(865,609)what()@~$+mul(784,462))],;;'what(){mul(902,580)when()<{~ ?>~how()mul(22,360)]mul(650,584)#select()!(why()/;+when()mul(173,450)where()mul(126,232)(?@/^how()why()who()mul(148,272)&?when()where()]mul(93,105)?!}!:@!mul(428,270)- where(179,717)>@!-,how()why()don't()!where()who()$:mul(472,276)<'&where()mul(334,580)'/-?+;(-mul(78,731)select()mul(584,89)*/>don't()/;''~how()?# how(261,707)$],mul(983,656)*> mul(340,628)]*][[/mul(193,206)(})~mul*/-#mul(976,903)%#why()$}-&mul(734,303)what()[from()*-/why()do()&why()!*]@from()&when()mul(339,902)from()([,[*^?how()mul(2,449)when()how()why()mul(799,379)-!#-mul(704,107)mul(205,807) :mul(787,710)mul(575,659)when(),];~'!mul(755,669){+&%$-,where()}# mul(555,644);^who()!}mul(341:@#{mul(135,278)where()how()what();where()mul(439,633),]$how()why()mul(708,508)where()%{why(744,659)mul(700--(why()*%/where()#<@don't()$?mul(39what(),#})$ select()how()~mul(847,132){)what(134,971)what(336,162))&/what()mul(470,625)#mul(275,789)where()<]mul(15,846)when()from()mul(21,575)%from()^when()when() ~(+select()what()mul(554,360)from()]~mul(785,136)}%#:?mul(117,254)who()do()% %;%-from(747,86)how()^/mul(313,607)why()mul(678,806),(mul(226,595)#%mul(415,823)']mul(873,709)#;select()'<> *mul(121,496)why()who()what()?where()mul(590,916)',/@what()how()?%mul(477,943)when()who()/';%mul(654,383)select()*why()'^select()]select()&mul(433,312);//~++what()&![]mul(532,452) + +*<@;mul(470,836why()who()select()#when()select()where()from(812,299)mul(469,324) select()why(){mul(891,321);'&$%@{mul(792,73)$$[who()>[[from():mul>why()?how()mul(522,233)([%select()mul(195,940)&mul(368,872)~}{who();!!>^^mul(920,394)^,select():?!%[mul(477,578): what()~!/select())who()/mul(976,739)>;who()who(43,925)&when()>>;+mul(857,247) mul(442,620){;mul(393,289)-!*mul(848,511)?how()}@mul(290,212))(~+#~:mul(150]*/>who()[>?:<]*[;+what()where()mul(307,698);;from()~%;where(84,170)[${mul(158,165):[what()^select()mul(977,383)'*@>/;:mul(920,657)*how()when()mul(816,293)select()~$+^+mul(420,667);(^where());mul(402,31)]mul(272,313)^>mul(161,164)@$$mul(260,136)&@}* > -mul(380,442)^#*!don't())?(;@mul(209/%(>,how()^mul(423,410)from()-mul(381,141)+~-)mul(674,158)^:what()>(^)who()mul*^,mul(74,315)!-),mul(341,503)where()/;*)why()),what()&mul(624,435)mul(880,482)}where():]why()mul(367,134)when()(when()mul(166,251)@+,who(),mul(610,271)+>)(mul(455,461)>&@-()from()+]mul(232,944)&,@when()@]mul(523,215)mul(876,836) mul(335,254)<#-{from()^mul(153,522)what(52,828)who()where()from()from()@&>$mul,!&what()^>*{'mul(670,505)$mul&&/(mul(672,267)mul(540,120)<$[->!}mul(234,539)!%]who(){mul(30,143)*mul(545,761)where()'!how(935,4)who()%'{~mul(673,752why()what()^:mul(111,407)from()}mul(860,90)#who()/^!how()-!{!mul(217,327)what()!'where()mul(729,143)where()!how()#mul(362,874)$how()how()->who()~^mul(315,718)!'-@~mul(206,449):/[??#mul(185,796)]~mul(597,268);) >select()from()?)<*mul(516,734)when()what()select()#$select()-?,$mul(366,627)%*{mul(62,39)^/$from()+mul(250+~select()!from(){mul(981,151)#}mul(92,876))how()who(363,963)mul(827,341)::how()~&mul(407,705)#+>mul(576,849)mul(482,236)(^* who(),what()mul(544%who()('>*!$@,}mul(113,286)when()'from(685,677)do()%from(614,566)-select()$,-mul(234,452)}>[&who(){#from()&don't()why()select(645,338):why(840,833)+#mul(874,529)}>what()select()why()mul(168,89)}{*when()}do()when()*?$^;?)]mul(811,383)!mul(770,725):!*#[from()from()from(487,367);:mul(564,858)(mul(248,563)*:;'mul??mul(254,676)+where()from()?'[){ how()mul(250,339))'!')where()}from()why()-*!mul(740,619)~$when(370,81)where()%-#;^mul(291;^<-mul(266,422)@%select()/why() [mul(794,513)where()>{+mul(944,970)!why();mul(968,484)/what()who()mul(285,45)when()who()^when(),mul(728,164),#/[)mul(568,114)%@:select() /*!mul(763,536)?@mul(506,503))do()@)^/mul(763,386)/why()how()from(417,323)>what() where()mul(260,628)?:'+$why()+@&mul(697,677)+]when():mul(246,278) + $mul(48,81)mul(266,777)who())@*~!?)# mul(843,750}mul(940,128)mul(477,389)who()}:when())<[when()mul(838,845)~mul(464,153)why(166,536){/&,~mul(282,701) -!?from();mul(101,631)from()/mul(236,985)why(458,58)do()&*&>how()mul(310,399) :{}^]don't()->when()from()+ mul(389,902)why(301,687) select()}> who()mul(265,840)/mul(652,469)~where()what(){<>~mul(792,402);> $+@select(747,467)[mul(720,836)%what(984,399)[select()-+{}}mul(757,143)-)%from(),)+where()mul(810,426)#who()^@mul,]-'(^^>mul(30,409),)from() mul(916,685what()how(){#/mul(494,862)-&mul(814,480)!&what()*mul(315,311)!when() @}*%#mul(571,122)~*mul(717,593))/mul(633,314)<^{[%{when()-,{mul(571,721):?,@~]select(){mul(275,614),select()/{[@mul(965,299)what()?$where()(#,(?mul(642,519),+~*]mul(830,103)/]:mul(61,609)from()where()from()([mul(808,331)$mul(885,259)))}mul(902,561)$*:mul(320,613)[where()}mul(374,288)~mul(720,106)*~$mul(967,879),who()[what()}when()/#$-mul(81,187)mul(368,22);];<}- @{mul(882,60)#select()when()mul(144,567);what(),,#don't(){+from()'! mul(608,319)(mul(225,187)%%(<,@#+%select()mul(130,278)!select()}'@mul(353,117)]%'-^/?^mul(799,639)who()}-[>mul(495,249)where()*,)~mul(768,867)why()mul(305,695)don't()?]where() ?#@(who()mul(748,367)why()+[mul(643,18)})mul(343,964){'mul(287,548)?%do(),from();/?';select()mul(97,411)!!{?mul(105,759)how())how()@mul(663,882)~[mul(709,411)@;]what()when()mul(267,470)&mul(857,988)}/]^-select()]^:where()do()why()where()]-*mul(566,77)-#mul(217,308)what()~~(select()&mul(462,131)<+]%how()select()mul(242,763)'select()why()mul(316,185)[when()/ mul(584,440)from() diff --git a/2024/main.go b/2024/main.go index bca4aa2..4276dee 100644 --- a/2024/main.go +++ b/2024/main.go @@ -3,6 +3,7 @@ package main import ( // "context" day1 "adventofcode/day1" day2 "adventofcode/day2" + day3 "adventofcode/day3" "fmt" ) @@ -11,12 +12,17 @@ type AdventOfCode struct{} // Executes the puzzle for all days func main() { fmt.Println("day 1 part 1") - day1.Part1("1_1.txt") + fmt.Println(day1.Part1("1_1.txt")) fmt.Println("day 1 part 2") - day1.Part2("1_1.txt") + fmt.Println(day1.Part2("1_1.txt")) - fmt.Println("day 1 part 1") - day2.Part1("2_1.txt") - fmt.Println("day 1 part 2") - day2.Part2("2_1.txt") + fmt.Println("day 2 part 1") + fmt.Println(day2.Part1("2_1.txt")) + fmt.Println("day 2 part 2") + fmt.Println(day2.Part2("2_1.txt")) + + fmt.Println("day 3 part 1") + fmt.Println(day3.Part1("3_1.txt")) + fmt.Println("day 3 part 2") + fmt.Println(day3.Part2("3_1.txt")) }