From 68ffec35a49c311657922227a4e0d8e2d8b82ff4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20=C3=81lvaro?= Date: Mon, 23 Dec 2024 16:28:34 +0100 Subject: [PATCH] Advent of Code 2024 - Day 3 --- .github/workflows/tests.yml | 2 +- README.md | 31 +++++++++-- lib/puzzles/2024/day03/README.md | 52 +++++++++++++++++ lib/puzzles/2024/day03/day03.rb | 13 +++++ lib/puzzles/2024/day03/input.txt | 6 ++ lib/puzzles/2024/day03/part1.rb | 60 ++++++++++++++++++++ lib/puzzles/2024/day03/part2.rb | 44 +++++++++++++++ sig/puzzles/2024/day03.rbs | 20 +++++++ sig/test/puzzles/2024/day03_test.rbs | 17 ++++++ test/puzzles/2023/day05/day05_test.rb | 2 +- test/puzzles/2023/day10/day10_test.rb | 2 +- test/puzzles/2024/day03/day03_test.rb | 62 +++++++++++++++++++++ test/puzzles/2024/day03/test_data_part1.txt | 1 + test/puzzles/2024/day03/test_data_part2.txt | 1 + 14 files changed, 304 insertions(+), 9 deletions(-) create mode 100644 lib/puzzles/2024/day03/README.md create mode 100644 lib/puzzles/2024/day03/day03.rb create mode 100644 lib/puzzles/2024/day03/input.txt create mode 100644 lib/puzzles/2024/day03/part1.rb create mode 100644 lib/puzzles/2024/day03/part2.rb create mode 100644 sig/puzzles/2024/day03.rbs create mode 100644 sig/test/puzzles/2024/day03_test.rbs create mode 100644 test/puzzles/2024/day03/day03_test.rb create mode 100644 test/puzzles/2024/day03/test_data_part1.txt create mode 100644 test/puzzles/2024/day03/test_data_part2.txt diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6bf8686..13000ef 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,7 +7,7 @@ on: pull_request: env: - SKIP_SLOW_TESTS: true + RUN_SLOW_TESTS: 0 jobs: test: diff --git a/README.md b/README.md index 00cc136..1dbf22c 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,22 @@ This repo contains my solutions to the [Advent of Code](https://adventofcode.com) challenges, written in the [Ruby](https://www.ruby-lang.org) programming language. -The project is compose of two main parts: +The project is structured as follows: -- A library, located in the [`lib/`](lib/) directory, containing the code for the solutions. -- A set of tests, located in the [`test/`](test/) directory, containing the tests for the solutions. +- A script [`bin/create-files.rb`](bin/create-files.rb) to create the basic files for a new puzzle. +- A library, located in the [`lib/`](lib) directory, containing the code for the solutions. +- A set of tests, located in the [`test/`](test) directory, containing the tests for the solutions. +- The [`sig`](sig) directory, which contains the signatures of the project. + +## Create Files + +When starting a new puzzle, you can use the script [`bin/create-files.rb`](bin/create-files.rb) to create the basic files for the puzzle. + +```bash +bin/create-files.rb --day 25 [--year=2024] [--force] +``` + +## Run Tests Tests are written using the [minitest](https://github.com/minitest/minitest) framework. @@ -16,10 +28,12 @@ To run the tests, simply run the following command: ```bash bundle install -SKIP_SLOW_TESTS=1 bundle exec rake test +bundle exec rake test ``` -## Puzzles +Some tests are marked as slow, and they are skipped by default. To run them, you can set the environment variable `RUN_SLOW_TESTS` to `1`. + +## Solved Puzzles @@ -39,7 +53,12 @@ SKIP_SLOW_TESTS=1 bundle exec rake test 2️⃣ Red-Nosed Reports - lib/puzzles/2024/day01 + lib/puzzles/2024/day02 + 🌟🌟 + + + 3️⃣ Mull It Over + lib/puzzles/2024/day03 🌟🌟 diff --git a/lib/puzzles/2024/day03/README.md b/lib/puzzles/2024/day03/README.md new file mode 100644 index 0000000..539a6ef --- /dev/null +++ b/lib/puzzles/2024/day03/README.md @@ -0,0 +1,52 @@ +# [Day 3: Mull It Over](https://adventofcode.com/2024/day/3) + +## Part One + +"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](https://adventofcode.com/2020/day/2). 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 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: + +`x` `mul(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 the results of the multiplications?** + +To play, please identify yourself via one of these services: + +Your puzzle answer was `173,529,487`. + +**The first half of this puzzle is complete! It provides one gold star:** 🌟 + +## 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: + +`x` `mul(2,4)` `&mul[3,7]!^` `don't()` `_mul(5,5)+mul(32,64](mul(11,8)un` `do()` `?` `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 the results of just the enabled multiplications?** + +Your puzzle answer was `99,532,691`. + +**Both parts of this puzzle are complete! They provide two gold stars:** 🌟🌟 diff --git a/lib/puzzles/2024/day03/day03.rb b/lib/puzzles/2024/day03/day03.rb new file mode 100644 index 0000000..95b62d7 --- /dev/null +++ b/lib/puzzles/2024/day03/day03.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative "part1" +require_relative "part2" + +module AdventOfCode + module Puzzles2024 + ## + # {include:file:lib/puzzles/2024/day03/README.md} + module Day03 + end + end +end diff --git a/lib/puzzles/2024/day03/input.txt b/lib/puzzles/2024/day03/input.txt new file mode 100644 index 0000000..bf042da --- /dev/null +++ b/lib/puzzles/2024/day03/input.txt @@ -0,0 +1,6 @@ +~-mul(858,892)?@#mul(380,985)what()[^what()%mul(340,11)~*}don't())/~-mul(849,387)%-why()when():how()>-,what()mul(605,504)what()~:]what();how()who()];mul(771,783)?^ who()}~?>mul(111,830))@ ~mul(329,797)%'why()why()mul(125,409)-*/where()()@&!why()mul(390,37)when(974,538)#/when()){/don't()/mul(20,990)}?%{(who()mul(627,567)(how()'<(#%how()mul(387,315){;%who()%#from()mul(868,680)}^$mul(862,19)mul(847,689)>((#@when()}mul(339,156)+/{}@{^%[why(231,704)mul(236,754)]'^where(978,690)mul(460,872)#'*mul(518,944)>mul(301,886))mul(477,933)^mul(685,909)who())]what(288,791)mul(170,434){ &where()~(@where()mul(191,104)mul(637,600)&select()why())~select()mul(534,617)?mul(763,508){from()when(22,266)+who() when()mul(204,352)when()mul(426,122)-<*where()'$ why()mul(192,980)[(#[!$-}mul(798,208)''[!where()what()select()mul(295,727))#)}($,$%where(539,126)what()when()select()'@mul(311,541),?don't()#select()!}!(}&?mul(100,142)),-}%mul(222,856)~$;>!from()where()mul(758,901)mul(757,796)mul(976,686);@'~+-{#who()mul(26,971);mul(404,362)^~who(417,106)what()#mul(186,691)from() %#+{/mul(67,433)-who()!-+!mul(184,469)*when()[&when(458,221)<$mul(163,556)^)]?,'}&+who()mul(514,239)~)(mul(143,441)'how(741,776)~who(766,575)when();>*^mul(281,143)+!>;+:mul(767,44)# when()why()don't()how()from()[from()from(){mul(436,519)what() *who();@{,>do()%'mul(19,540)when()mul(643,539)~when(812,543)mul(527,639)-!mul(409,379)where()/(mul(168,491)#$>]mul(432,333)@%>>&who()(mul(113,494)%where()why()when()mul(659,592)#(/'mul(72,670)when()^^mul(369,596)mul(631,168)-why()()[*}mul(330,968) $why()where()mul(230,139)don't()@)@where())%}where()&,}mul(875,661)~don't()':why(){mul(793*:mul(399,702)!://select()@when()why()do()@mul(488,581{?{&how()&select()!<'mul(912,311)why()! })@+}mul(128*mul(626,752)%mul(49,60)[,who()what()select())@@]*]why(937,788)from():mul(945,658)<]don't()where()how(515,644)@+'select()how()mul(369,665)/],mul(263,185)where()^why()why()@what()mul(82,407)mul(464,537)[mul(871,333)^mul(560,227)-why()mul(966,203)$what()+where()#mul(101,21)}%how(),)^who()>,select()mul(667,565)('what(),%select()(@mul(500,204:''<~'why(477,559))[where()mul(698,648);how(){&?what()^@[%mul(280,395)>~, mul-#/%:where()>where():mul(170,746)don't()}($:@mul(998,876)*~<[mul(121,366)-?#;how()>from() ::mul(804,883)^-[ (:mul(100,354who(544,766)+,]>!['/;mul(949,115) +[^!})from()&mul(617,518)*how()(:@who()what()/mul(591,163):what()?{'do()!/when()!$mul(394,797)]how(620,741)!:!mul&%mul(523,862) ~}where()why()}from()! @do()mul(270,205)from()[&^mul(577,474)?{*/why()*what()mul(656,30)where()+#,@mul(295,616)why()/mul(8,267))-^when()who(610,661)'mul(182,139)select()}select()!when();;mul(492,992)?how()~($who(716,562)%/mul(702,654))*>mul(126,386)[mul(351,400)select()%$[!}{+mul(98,266)mul(924,5)*'(^:[}!?mul(112,163)+!?where()mul(987,791)mul(943,488)how()mul(698,312)&:@from(442,439)~%~:mul(235,520)%mul(248,221&>/;select()what()) ~@from()mul(546,261)'mul(956,953why(458,937)when()~!where();mul(442,916)%;)&(mul(410,237)where()~why()[+?[mul(169,337)what()who()&what()mul(901/mul* don't()mul(999,662)*]&/'#<+,^}'mul(365,260){{?,,}from(520,861)where()what()mul(325,208): when()+>{!;mul(335,726)#',:>@?mul(48,443![mul(130,626)!{^don't()*?-*[%>mul(934,995),how()@from()'#mul(433,843)why()<}$}how()-mul(754,464)when()why()mul(992,113);]/{%mul(87,293^]how()why()~&when()*#mul(589,979)@-&select()/%mul(218,248);mul(536,581)when()mul(215,212)/%when(66,857)@what()+mul(958,271) '%#who()&{mul(239,197)}what()$>{*'select()mul(365,443)>why()how()what()%mul(885,496how()when()}{@/ mul(589,42)^:;who()where()why() !how()(mul(574,947)*;where():&where()%]<{mul(572,4)$mul(232,716)when()when()mul(928,697)^(where()mul(301,501)-mul(100,436)#~?mul(708,770): @:@)!*mul(89,177)><@+ {when()+mul(926,998);>,)?,$)mul(813,382)}from(728,403)^mul(497,820)where()(?why(454,153)!select()}*!don't()what()from()}&}?how()mul(432,895)$!+mul(743,149)$!why(704,998)when()mul(229,683)((when()from()#&{mul(217,869)^)?)?what() select()}mul(765,209)who():}%{mul(448,553)[:-,don't()what()<,}from()where())mul(543,719)from(415,153)why()!'?^~mul(666,540)mul(922,877)mul(416,636)+#;$select()'mul(971,485)from(964,539)where()^ *who()/mul(35,594)<:mul(207,585)~*}'[]#mul(815,776){?%&*@?mul(385,133)['!:who() )mul(439,846)&)mul(529;?>;#mul(677,906)^& ()mul(75])how())mul(481,206)who()why()$^ from();what()'why()do()(%^&)mul(277,454)>where()where()$-#select()+/don't()what()select()]mul(295,126)why();from()mul(203,301){#mul(572,420){%(-*mul(929,786)do()mul(107,813)mul(921,668)'^+++!:[;mul(587,921)mul(242,920)mul(933,463)why()mul(340,196)'--']-mul(623,675)select()what()mul(656,927)<$)from()where(473,933)]~mul(408,566)!>$mul(373,541)}%*^)mul(346,14)>&how()@^-(&;mul(993,735)select()*{mul(990,221)*from():[ mul(940,96)/how()^>#;select()/#?mul(624,249)>mul(160,974)]^mul(589,900)&$[?[<-!select()mul(512,717)^/&mul(226,478)* ';'+from()?@~mul(224,929)!when()^what()where()'@'>~mul(451,87) +when()%how()where()how()what() mul(59,36)how()where() )>mul(270,950)--mul(864,193)$!!from()[+when()do()?&]from(){?who()mul(576,443)}>*what()mul(60,617)]where(){*}what(16,936)*:mul(613,575) @$%%'mul(930,241)!~when()<+what()&{mul(189,41)don't()how()%who()how()mul(79,681){']{({ mul(615,354)~#&{mul(863,397)@?(mul(469,991)!mul(97,649)'%{where()/{+*}when()mul(429,913)[ >where()>](when(654,639)),mul(763,148):!~do()%+*',mul(646,831)~*$!?>mul(632 @?from()[mul(165,564)#mul(464,289)from(),mul(197,442)~[&mul(739,935)how()where()!],mul(583,831){mul(4,501)mul(912,584):select()(when()>&& &what()mul(836,498)where(987,400)*${mul(983,648)+~]don't()&,how()'^[*({mul(180,898)mul(982,907): select()($what()mul(294,259)do()where();>mul(130,154)mul(566,682)how()-:from()/ )^mul(50,250)~@select()(->mul(65,434)mul(865,288)+$don't()#+-mul(875,123)@@>?mul(606,377);do();!where(){mul(267,543):]','# #*why()mul(31,575)#;who() *<$mul(797,419)'what()}when():what()/mul(627,73)'how(218,429)who()]*$,$from()mul(221,471)$ ;what()mul(38,106)?-&!~&from()mul(117,669):how()mul(422,348)(:/]mul(568,980)when(){~where(490,375)<(& mul'*)'where();##:}mul(34,144)mul(352,352) select()-when()/where(365,161)mul(571,634)mul(373,66)}how()don't()where()@>$$(!&mul(532,260))!mul(304,873)select(676,286)?#<,why(),^~do()%@~where()mul(56&+select()>select()what()mul(670,288)< $:+[$~what(){@!%mul(422,126)(who()?from()}*}mul(459,923)mul(38,243)^)!who()::#from()/mul(845,589)^/when()'#when()~,'select()mul(454,166)who()~>when()^when()?'@where()mul(237,855)~from()from()why()mul(233,606)]@mul(947,750)!@*}who()(@%mul(252,951)[from(995,363))}),when()who()+where()mul(368,442)>from()where()select()from()what()^?mul(190,689)mul(337#who()*when()/*mul(652,631);,*why()+select()>}where()%mul(839,296)>&%,@}$mul(723,530)who(685,511) %~where(782,449)mul(36,917);]]{mul@where()!&*%why()mul(191,759):why()what()$mul(900,773):$who() *{mul(426,740) why(396,306)/from()why()*%}'+do()@!'how()%^/mul(970,462)when()who()!-mul(535,35)}from(377,342)when()/(how()who()-}:mul-what()/how()?*^how()mul(217,447)/]?%!mul(495,690)}#<{?,do()+-why()mul(3,559)who()+what()mul(942,139)})when()who()<-]'what()what()mul(218,316)how()mul(669,389),<+mul(861,165)why(){^}[mul(594,386what()who(){mul(801,662)mul(852,2)-mul(458,479)//mul{don't()mul(826,480)>mul(954,968)$;mul(871,184)from()select()**<]!mul(503,290)#select()where()?do()mul(154%[&how()?mul(279,673)-[don't()$]why()-what(754,13)mul(841,495)where()}{mul<-'##!/usr/bin/perl@~mul?what()<^-@/>from()!mul(573,383)where()-{#mul(420,579)when(977,697)when() &@##from()+}mul(314,487) +who()+from()where()mul(878,982)]~mul(812,80)?select()don't()how()];mul(986,548)/how(311,658)/select()(don't()(select()select(533,328)<^+from()what()why()@mul(786,152)*<[ }},)mul(30,285)mul(721,12)#(-{what()mul(70,496)^when()-/how(420,87)select()what()]$mul(645,406);-~where()>do()~&^(,}$#$mul(993,357)?/select()}do()+from()~@mul(661,590))what()*!?]*!'mul(19,345)-why())select(){%(% don't():*#when()~select()mul(910,416)}mul(550,400)when()]~mul(515,93)&where()mul(412,99);(;+mul(611,500%what()%,from()when()mul(822,769),-*(([$(mul(153,856)why(){mul(476,25)do()-mul?from()<<[why()&mul(859,60)what()]+ :how()what(434,726):^from()do()}*?}?mul(432,641)>%?!mul(93,484)++}?mulwho()#>who()select(): why()mul(889,212)>]?where()&mul(808,71)<*/'-+select()mul(523,619)+#where()mul(324,306)why()';why()}mul(337,315)~:what():^?-$from()mul(924,137)mul(444,59)why()[from()how()who()mul/how()>mul(304,707)'select() select()mul(224,915)mul(991,306)[>$! &how()>/mul(719,679)how()select()mul(65,620)from()#'~:]+-from()]mul(971,857)how()#[~~]?%mul(230,961)from()from())# } where()mul(330,14)>mul(411,981)}}<)mul(304,453)how()from()$who()mul(573,848)% ;:from()(~select()!mul(542,790)}'#mul(530,502)%$(-#$mul(358,540)mul(10,361)when()where()<}&where()mul(429+!*$~)mul(446,812)%when()don't()>~>^what()select()why()from()where()mul(330,214) -[mul(731,164)/mul(776,235)mul(240,20)who()^what()']@!-who()%don't()(( )'!how()why();>mul(941,911)'?where()from()how()/mul(489,746(select(725,995)-from()?;when()%]%mul(555,315),how()#%!>?mul(664,596how()+'-who()from(51,408)mul(605,672)?!%from()) #}don't():}mul(579where()mul(243,699)mul(321,902)*!/mul(465,704)*when()[*how()mul-!mul(15,80)}#}mul(413,156)(;mul(71,288)/$;'don't(),how()+[;$who()%$when()mul(516,863)mul(268,600)(mul(602,939)who())>)<&'!mul(323,967)?mul(673,398)):&#}>>{!mul(501,484)<;who(969,453)-who(624,921)-)<-mul(72{-//!mul(576,751)mul(318,331)mul(707,186)+how()mul(660#how()!+#?where()mul(547,453) +[what()?->+[mul(266,969)what()from()%^!how()?mul(236,335) )'<>,&!-where()mul(563-select()(mul(405,969)when()[%;why():$]mul(266,763)what()~#-{+don't()~+-mul(68,150)#{{?%:mul(422,966)select()mul(143,33);mul(917,142))]'>mul(23,457);'what()why()-where()}/>mul(66,911)&&':(>why()>mul(413,27)mul(772,64)mul(266,512)%$*[why():where(),@%^',mul(750,295)*]:mul(17,60)!!}*from(975,930)mul(763,134)mul(463,381)when()(select()where()+{?what()mul(755,843)!why()+>mul(24,584){mul(105,734)$why()@where()!+what()%what()mul(5,111)#^;?{^/?^~mul(24@>when()what()where()#from(284,9)when(437,59)mul(978,337))+:(select()mul(996,373)(what(188,513)@ ]from()}mul(370,765)~$mul(490,904))]/%who()why()/mul(123,978how()why()[who()~%how(180,982)@:mul(343,157)![/*^mul(157,976)who()from() select()]+?+do();)&)mul(734,420)$%(select()>'mul(143]mul(4,351)mul(279,828)'}^mul(108,132) #how()why()}mul(573,602)}@mul(747,126)where()'@mul(247>why())when()+::^&from()mul(843,212)^mul(612,743)[mul(204,899)$mul(174,265)%]mul(864,804)-from()how()%%mul(674,431)^^#[(@:&*mul(619,935)select()//~^mul(70,941)why()select(405,825)why()who()@mul(112,658)'+(@!$who(){select()do()from()from()@how(846,925)mul(417,888)~{: who()&-;mul(285,186)-+what(){/+-mul(24,944''mul(935,370)why()?~ +mul(468,38)'why()(/mul(704,512)%mul(946,591,<);from()select(290,495)*{:['%mul(554,965)(~mul(308,534)mul(6,373)^how()@%&how()~mul(669,386)when() 'mul(33,652)mul(950,268)::mul(98,181)]^how()'!mul(982,613)how()&where()]what()how()do()[from()#[? mul(929,452)<$$ mul(506,408)-mul(670,674)how()mul(645,104)do()(>[%from()@'who()&%mul(45,884)mul(895,714)mul(871,6)},?&;!select()#where(779,295)^mul(113,370)why())%[select()[,^' mul(976,960)][%mul(828,993){}}mul(135,443)>what()mul(344,20)>where()/) :@mul(564,715){([,]'mul(675,478)#*@who()>]$*+mul(94,992}from()mul(288,445)mul(72,57)*/})how()[how()!mul(543**%who():what()mul(345,778){mul(462,244)from()[@*who(),+mul(591,870)#select()%mul(779,654)$+%%@ mul(920,934)?-select()-}mul(247,709)@$why()mul(179,824)(when(989,252)?mul(422,816)$[from()#who()@,when(),mul(922,47)@mulwhy()-mul(890,397)-^/+'select()[&!who()mul(547,6)-]>:^;mul(870,938),~?[%mul(8,689!>*@!]mul(311,244)->how()$when()'}mul(213,766)$:!+ mul(620,644)%where()mul(430,127)%{[mul(682,585)mul(245,26)don't()mul(510,688)where()]-mul(844,443)/@+who()?who()when()mul(453,182)mul(928,131)?&'{[+@?,mul(55,18)mul(795,739):}what())@$]'mul(377,34)&,~*why()/@who()mul(467,127))&how(28,925)mul(955,519)}where(830,382)$when()[-why()/+mul(310,139)mul(217,931)!mul(574,122)!mul(227,82)where()mul(940,851) {mul(545,758):from()%~/mul(937,897)mul(665,919)^&/ &what()mul(23,37),select()%*mul(98,952);mul(171,967)who()$+]when()%$mul(426,870)<<;;why()[do()@mul(28,286)-what()([+select()why()do()<^+what()^?~-mul(246,992)/mul(938,936)'$>when()mul(18,736)%?how()-what()from() when()select()mul(37,387)}<[}?+how()#&?mul(779,369)select()mul(750,510)%[,'from()>who()mul(13,97)what(547,477)why(859,962)?,where()mul(806,879)mul(577,179)#from()+>where(705,292)what(712,121))when(227,970)*mul(942,336)& who())!*mul(992,750):from()<:+-mul(938,672)mul(55,872)mul(354,183)>+(+mul(520,932)#from():}{who()$mul(675,973)@^@%mul(58,468)/select()what()->]mul(343,375)^ ,%mul(480,300)where()(/*$mul(695,676)how()where(),-!:from()mul(363,212)~(!where()-[what()don't()mul(614,594))mul(569,802)'mul(995,471)~&$:^:how()how()-^mul(853,428)-;-%what()(from()+do()'where()() !{^?#mul(376,780)select())where()select()&{-,{,mul(894,646)select()*< @}*[}}mul(332,665)[ \ No newline at end of file diff --git a/lib/puzzles/2024/day03/part1.rb b/lib/puzzles/2024/day03/part1.rb new file mode 100644 index 0000000..b1f6685 --- /dev/null +++ b/lib/puzzles/2024/day03/part1.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +module AdventOfCode + module Puzzles2024 + module Day03 + ## + # Class for solving Day 3 (2024) - Part 1 puzzle + class Part1 + ## + # The program memory + # @return [String] the memory + attr_reader :memory + + ## + # @param file [String|nil] file with puzzle input + def initialize(file: nil) + file ||= "#{File.dirname(__FILE__)}/input.txt" + init_contents File.readlines(file, chomp: true) + end + + ## + # Compute the answer for the puzzle. + # @return [Integer] answer for the puzzle + def answer + ops = find_valid_ops + compute_ops(ops) + end + + protected + + ## + # Initialize the class' contents from the file contents. + def init_contents(file_contents) + @memory = file_contents.join + end + + ## + # Find all valid operations in the memory + # @return [Array] the valid operations + def find_valid_ops + matches = memory.scan(%r{mul\(\d{1,3},\d{1,3}\)}) + matches.map(&:to_s) + end + + ## + # Sum the results of all operations + # @param ops [Array] the operations to sum + # @return [Integer] the sum of all operations + def compute_ops(ops) + result = 0 + ops.each do |op| + result += ::Regexp.last_match(1).to_i * ::Regexp.last_match(2).to_i if op =~ %r{(\d+),(\d+)} + end + + result + end + end + end + end +end diff --git a/lib/puzzles/2024/day03/part2.rb b/lib/puzzles/2024/day03/part2.rb new file mode 100644 index 0000000..59bcaf7 --- /dev/null +++ b/lib/puzzles/2024/day03/part2.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require_relative "part1" + +module AdventOfCode + module Puzzles2024 + module Day03 + ## + # Class for solving Day 3 (2024) - Part 2 puzzle + class Part2 < Part1 + protected + + ## + # Find all valid operations in the memory + # @return [Array] the valid operations + def find_valid_ops + matches = memory.scan(%r{mul\(\d{1,3},\d{1,3}\)|do(?:n't)?\(\)}) + filter_ops matches.map(&:to_s) + end + + ## + # Filter valid operations regarding the enabled flag, + # which is set by the do() and don't() operations. + # @param ops [Array] the operations to filter + def filter_ops(ops) + valid_ops = [] + enabled = true + ops.each do |op| + if op == "do()" + enabled = true + next + elsif op == "don't()" + enabled = false + next + end + + valid_ops << op if enabled + end + valid_ops + end + end + end + end +end diff --git a/sig/puzzles/2024/day03.rbs b/sig/puzzles/2024/day03.rbs new file mode 100644 index 0000000..2ef1f8e --- /dev/null +++ b/sig/puzzles/2024/day03.rbs @@ -0,0 +1,20 @@ +module AdventOfCode + module Puzzles2024 + module Day03 + class Part1 + attr_reader memory: String + + def initialize: (file: String?) -> void + + def answer: () -> Integer + def init_contents: (Array[String]) -> void + def find_valid_ops: () -> Array[String] + def compute_ops: (Array[String]) -> Integer + end + + class Part2 < Part1 + def filter_ops: (Array[String]) -> Array[String] + end + end + end +end diff --git a/sig/test/puzzles/2024/day03_test.rbs b/sig/test/puzzles/2024/day03_test.rbs new file mode 100644 index 0000000..031612b --- /dev/null +++ b/sig/test/puzzles/2024/day03_test.rbs @@ -0,0 +1,17 @@ +module AdventOfCode + module Test + module Puzzles2024 + module Day03 + class Part1Test < MiniTest::Test + def test_part1_answer_real_set: -> untyped + def test_part1_answer_test_set: -> untyped + end + + class Part2Test < MiniTest::Test + def test_part2_answer_real_set: -> untyped + def test_part2_answer_test_set: -> untyped + end + end + end + end +end diff --git a/test/puzzles/2023/day05/day05_test.rb b/test/puzzles/2023/day05/day05_test.rb index 6ff5ff5..45ba97e 100644 --- a/test/puzzles/2023/day05/day05_test.rb +++ b/test/puzzles/2023/day05/day05_test.rb @@ -106,7 +106,7 @@ def test_answer_test_data_set end def test_answer_input_set - skip "Takes too long to run" if ENV["SKIP_SLOW_TESTS"] + skip "Takes too long to run" unless ENV.fetch("RUN_SLOW_TESTS", 0).to_i == 1 puzzle = AdventOfCode::Puzzles2023::Day05::Part2.new diff --git a/test/puzzles/2023/day10/day10_test.rb b/test/puzzles/2023/day10/day10_test.rb index 392e5b5..b20c968 100644 --- a/test/puzzles/2023/day10/day10_test.rb +++ b/test/puzzles/2023/day10/day10_test.rb @@ -91,7 +91,7 @@ def test_answer_test_data_set5 end def test_answer_input_set - skip "Takes too long to run" if ENV["SKIP_SLOW_TESTS"] + skip "Takes too long to run" unless ENV.fetch("RUN_SLOW_TESTS", 0).to_i == 1 puzzle = AdventOfCode::Puzzles2023::Day10::Part2.new diff --git a/test/puzzles/2024/day03/day03_test.rb b/test/puzzles/2024/day03/day03_test.rb new file mode 100644 index 0000000..52286bb --- /dev/null +++ b/test/puzzles/2024/day03/day03_test.rb @@ -0,0 +1,62 @@ +# frozen_string_literal: true + +require "test_helper" +require "puzzles/2024/day03/day03" + +module AdventOfCode + module Test + module Puzzles2024 + module Day03 + ## + # Tests Day 3 (2024) - Part 1 + class Part1Test < Minitest::Test + def setup + # Do nothing + end + + def teardown + # Do nothing + end + + def test_part1_answer_test_set + input_file = "#{File.dirname(__FILE__)}/test_data_part1.txt" + puzzle = AdventOfCode::Puzzles2024::Day03::Part1.new(file: input_file) + + assert_equal 161, puzzle.answer + end + + def test_part1_answer_real_set + puzzle = AdventOfCode::Puzzles2024::Day03::Part1.new + + assert_equal 173_529_487, puzzle.answer + end + end + + ## + # Tests Day 3 (2024) - Part 2 + class Part2Test < Minitest::Test + def setup + # Do nothing + end + + def teardown + # Do nothing + end + + def test_part2_answer_test_set + input_file = "#{File.dirname(__FILE__)}/test_data_part2.txt" + puzzle = AdventOfCode::Puzzles2024::Day03::Part2.new(file: input_file) + + assert_equal 48, puzzle.answer + end + + def test_part2_answer_real_set + puzzle = AdventOfCode::Puzzles2024::Day03::Part2.new + + assert_equal 99_532_691, puzzle.answer + end + end + end + end + end +end diff --git a/test/puzzles/2024/day03/test_data_part1.txt b/test/puzzles/2024/day03/test_data_part1.txt new file mode 100644 index 0000000..2e1a90a --- /dev/null +++ b/test/puzzles/2024/day03/test_data_part1.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)) \ No newline at end of file diff --git a/test/puzzles/2024/day03/test_data_part2.txt b/test/puzzles/2024/day03/test_data_part2.txt new file mode 100644 index 0000000..b774ec9 --- /dev/null +++ b/test/puzzles/2024/day03/test_data_part2.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)) \ No newline at end of file