From 070f5ea6ee24ce8844dcf6cc862bcd0ab3c051be Mon Sep 17 00:00:00 2001 From: tristan Date: Tue, 3 Dec 2024 15:05:41 +0000 Subject: [PATCH] 2024 day 3 part 1 --- 2024/03/example.txt | 1 + 2024/03/flake.lock | 59 +++++++++++++++++++++++++++++++++++++++ 2024/03/flake.nix | 31 ++++++++++++++++++++ 2024/03/input.txt | 6 ++++ 2024/03/solution.nix | 18 ++++++++++++ 2024/03/solution.test.nix | 34 ++++++++++++++++++++++ 6 files changed, 149 insertions(+) create mode 100644 2024/03/example.txt create mode 100644 2024/03/flake.lock create mode 100644 2024/03/flake.nix create mode 100644 2024/03/input.txt create mode 100644 2024/03/solution.nix create mode 100644 2024/03/solution.test.nix diff --git a/2024/03/example.txt b/2024/03/example.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/2024/03/example.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)) diff --git a/2024/03/flake.lock b/2024/03/flake.lock new file mode 100644 index 0000000..dc94ab4 --- /dev/null +++ b/2024/03/flake.lock @@ -0,0 +1,59 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1732837521, + "narHash": "sha256-jNRNr49UiuIwaarqijgdTR2qLPifxsVhlJrKzQ8XUIE=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "970e93b9f82e2a0f3675757eb0bfc73297cc6370", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1703013332, + "narHash": "sha256-+tFNwMvlXLbJZXiMHqYq77z/RfmpfpiI3yjL6o/Zo9M=", + "path": "/nix/store/50bgi74d890mpkp90w1jwc5g0dw4dccr-source", + "rev": "54aac082a4d9bb5bbc5c4e899603abfb76a3f6d6", + "type": "path" + }, + "original": { + "id": "nixpkgs", + "type": "indirect" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs", + "tix": "tix" + } + }, + "tix": { + "inputs": { + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1704309534, + "narHash": "sha256-U9qeMl0WBlpy335gL06WFGvPxLBTd+ToiiCHUHWrgR0=", + "ref": "refs/heads/master", + "rev": "5007770e88c4d5f67c3790761a1125904a9ef257", + "revCount": 20, + "type": "git", + "url": "https://git.tristans.cloud/tristan/tix" + }, + "original": { + "type": "git", + "url": "https://git.tristans.cloud/tristan/tix" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/2024/03/flake.nix b/2024/03/flake.nix new file mode 100644 index 0000000..4eb8f54 --- /dev/null +++ b/2024/03/flake.nix @@ -0,0 +1,31 @@ +{ + description = "A very basic flake"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable"; + tix.url = "git+https://git.tristans.cloud/tristan/tix"; + }; + + outputs = { self, nixpkgs, tix }: let + pkgs = import nixpkgs {system = "x86_64-linux";}; + solution = import ./solution.nix pkgs; + in { + + part1example = (solution {file = ./example.txt;}).part1result; + part1 = (solution {file = ./input.txt;}).part1result; + + part2example = (solution {file = ./example.txt;}).part2result; + part2 = (solution {file = ./input.txt;}).part2result; + + packages.x86_64-linux.watch = tix.watch { + cmd = '' + nix run .\#test --impure --extra-experimental-features pipe-operators + ''; + }; + + packages.x86_64-linux.test = tix.run [ + ./solution.test.nix + ]; + + }; +} diff --git a/2024/03/input.txt b/2024/03/input.txt new file mode 100644 index 0000000..34046ac --- /dev/null +++ b/2024/03/input.txt @@ -0,0 +1,6 @@ +}mul(417,770)%why(){/':mul(187,313)<@*select()mul(908,713)who()$mul(156,598)#from()* from()^-mul(233,874)&when()mul(346,616)what()(select(),when()where()when()when()&}mul(814,171)+;how() ?)when()?mul(68,188),[select(336,87)^%when(108,692)#mul(621,712){}when()how(69,755)mul(273,27)+:'?( /what()who()from()mul(135,680)-]{:;mul(7,723)where()@{mul(626,107))how()//when(),~+mul(834,113)&!where()$mul(951,76) /why(767,720)mul(107,588)mul(632,977)mul(55,47))[select()']what()mul(803,299)&%#mul(684,214)]}{-}[mul(806,141),mul(166,98)(select()[*'mul(619,823)mul(633,178)-how()from()?from()*#what()'mul(806,961)what()!?mul(529,544)?+[@$where()mul(498,66)?where()why()who()when()why()'from(742,433)mul(623,31)what()&{>]when()who()$/don't()-^!)where()select()mul(761,938) }}mul(799,944)]~what()mul(872,175) &]-@[mul(59,669)-:,:[{mul(457,618)&),(-[< / mul(490,407)>who()$do()who()!+^mul(579,603);>mul(365,610)!#<^;mul(802,476)}')select()what()don't()$%]}'*what(661,765)>+mul(506,360)*#&!mul(484,725),#~how()~select()don't()?what()mul(16,825)mul(732,513)##mul(990,859) <*,what()%$*?;what()mul(98,564)mul(312,793)~select()where()}$%mul(173,805)}what()/$##}mul(369,326):why(421,566)#mul(357where(305,979)what()*)*mul(775,55){when()%select()~/what() /mul(332,608)-who(),mul(592,150)~/#&#}]mul(453,71>~'from()~%mul(540,100)$/:}#(@ mul(789,978)%$](#{>]?mul(473,873)when()where() {@]why()mul(606,201)who()how()mul(212,663)^;#:when())#don't()where()what()where()*why()why(836,629)@%mul(95,601) +~%'{;mul*,mul(88,716)^ don't()how()when()?%$}when()]?mul(565,119*> $from()-~?mul(980,419)'}from():what()mul(877,181)why()~-, from();~~(mul(347,867)[mul(259,40)?&^/mul(707,897)!!why(589,359)+*>>?@!mul(657,687)/from()who()%-?>?who()mul(795,56) ;<))},mul(3,638)-)mul(243,985)$?where()[})what()mul(487,217)+~//;:mul(449,151)$#(select()why()$%}mul(201,260)select()([[)what()];mul(773,545)~;mul(859,808)mul(235,354)mul(388,265)mul(479,697)from()when()what()select()!^[from()mul(366,310)'*when()/[![(mul(288,12),!,#how()^}}mul(173,948))~why()/!mul(461,403)& where()mul(268,357) +when(578,754)mul(12,923)# /+who();&'^mul(874,174))from()] mul(294+$$[when()where()+mul(118,975)mul(954,230)(+when()mul(579,726)why()$+]$mul(796,252):@ ^?mul(108,275) $)who()why()^mul(782,400);*@~what()'-mul(712,375)[!who(791,378)mul(163,311-]:@!#[select()%%,mul(375,461)*''from()><},@{( ;mul(361,764)@mul(71,571)?%~from()what()[ +mul(384,899)@:]mul(627,870select():@!]/mul(676,247)?$what()%who()&from()from(228,681) mul(974@why()*mul(101,691)^do()[why()how()+&mul(228,575)%$where()&#why()from()mul(601,339)#:>@(from()mul(9,628)~{$],why(412,785)):mul(921,60)?when()$mul(230,472)%where(){?}@{mul(57,287)mul(620,392)who()))how()')}mul(920,110)) how()why()>*)from()'mul(66,955)who()mul(339,162)how()from()#mul(63,609)?+how()<&#where() mul(917,834)(mul(845,751)/mul(803,539)~>&:##?:why()']%^what():mul(462,669)select(),##;:&(mul(211,530)select()from()when()$mul(470,942){-select()what()> mul(934,811where(),mul(424,644))~-{mul(265,791)where()who())mul(403,892)select():<~-when()who(59,475)^mulselect()^mul(265,57)mul(465,609)mul(286,417)select()mul(208,44)#}:)}(+mul(997,988), mul(701,669)where()-from()mul(90,543)-[mul(609,511)>,%-why()why()(what()from()don't()[@;~mul(18,991){;/!^'}-mul(87,567))mul(571,648)-how()#~mul(191,910)}?<&&#~mul(324,407)$(#mul(354,471)^}^#&',)&mul(441,628)'mul(553,282)select(),select()where(){mul(485,690)what()?*]#when()$:>mul(939,289)'mul(482,608)'mul(14,642) how()<@^when(805,793)/mul(132,58)how()($}?where()mul(420,564)mul(542,105)&what()} !~!'why(){mul(380,891from()~where()]what(570,293){mul(255,57)@who(288,308)what()?[how()/mul(838,593)'#+who()what()mul(796,32)&why()who()when()when()^'how())?{(mul(821,630)$@mul(706,250)}+,/why(588,457)'>mul(451,548)where()]where()&?select()# %mul(194,6)how()how()why()( )/~from()&mul(519,83)&from(),mul(462,510),>,-?{)how()[mul(840,412)where()!;why()#where()/<)why()mul(452,646)?}?!$+!,/mul(502,96)^~(^mul(278,460)mul(478,959)where()-,select(233,181)why()>-mul(391,238)mul(59,361)when()-why()select();why()+:@who()[from()]mul(490,50;who()((when()!why()mul(667,140)what()mul]*from()!^}why()}>:do()~ {(select():why()*mul(691,254)select()why()mul(911,232)where()]#;'don't() *^when()]{}mul(479,259)select()'!}@ *?mul(673,636)how();#how()%^ mul(428,280) }}')*+mul(13,237 why()*/:/)mul(128,243)(mul(691,104)-$@mul(622,807)[:mul(489,852))what(55,333)where()#&[how()#mul(502,795)[?mul(820how()@'why()((mul(620,867)mul(507,4)$^- mul(206,143)- mul(10,462)/what()where()what()^what()what()mul(734,952)%how()@mul(730,42)what()]why()when()mul(80,180)how()~?{[:where()?+where(),mul(261,682);$>^mul(946,198)*-~{select()?where()]mul(331,301){@()how()don't()from():mul(105,560)when(378,815)+*):%^mul(613,583)where()##~mul(761,592)select()@[$why(),}{({mul(727,677) /do()#(what()%@$why()%/mul(488,910)who()who();mul(385,653)}mul+ mul(340,374))#(mul(853,934)#&??why()why()mul(764,68)what(){mul(759,904)*;%mul(398who()]}:@'$mul(126,742)how()how()+')@when(){mul(585,684)%$>)<)where()mul(463,855)how(222,976)mul?$#><&$;from()when()when()mul(100,686)!when()what(),>;mul(814,643/@>]>!]mul(835,81)&~select()how()+))+mul(155,781)'%<,;-@'who()mul(709,919) [#{select()(<,where()mul(269,144)what()'>~where()mulwhy()mul(329,330)&from()$:*from()how())mul(483,838)mul(51,282)-+@>++{mul(717,169)($)~]why()^%how(68,594)mul(51,386)where()from()why(424,17))@$mul(230,225)when()&why()*mul(472,522)^what(602,992) mul(169,570)what()/:who()['why(102,867)#'@ $mul(274,92)what(){from()from()from()who() {-mul(372,694){how()select()>mul(15,627)~'who()[what()from()mul(338,200%?*&'!<)mul(477,26)why()*+what()$who()>how()mul(945,996)why()[do()>;who()>why(289,937){!%who()mul(355,685)!mul(644,668)?/ }^}mul(300 [!how()*?when()why() mul(141,672)+&];$)mul(921,47)), {{]>where()from()select()mul(622,890);@$-mul(195,976)$[:when()where()/mul(887,64))mul(799,568),select() { @*>mul(535,258)?why(567,793)^{'*;]mul(202,214)(%when()^<]$-$mul(951,841)![ ];+how()mul(925,412)$;)%']mul(303,460)$where()(mul(369,207)why()>how()%@when()-~-do():+select()#!/usr/bin/perl>#}:mul(110,876'~%from()!{select()mul(924,516)from(){::[#)!&[mul(316how()!from()>mul(570,999) +'}who()&%;}#mul(541,894),'[mul(35,499);select()where()^$@}?mul(239,128)~mul(750,487);how()what(),what()why()]mul(842,927)'&{;what()mul(231,35)+why())who()from()how()why()select()mul(117,134)[*%;~+@/(?mul(184,337)mul(751,262;;mul(71,403),!]/what():*mul(49,974);%mul(556,780)#&;#>when(809,477))}how()mul(934,699)@{!mul(687,51)(where(319,422)why()how():'!mul(340,635) &where() when()-select())>mul(816,984)*;who(267,941){why()#]mul(999,933+ ]$ mul(706,15)!mul(797,760) )who()mul(577,413)(*don't()^@,+mul(551,920)(%@mul%* what()mul(107,870)$?when()how()mul(291,96)>>who()-{]<]'mul(607,186)why()select()mul(239,162) from()how(8,248)+*mul(603,94)*how();];!+mul(937,437)select()[+mul(720,689)@ where()[*<$mul(176,240)mul(488,6)mul(884,356)#what()(#mul(357,216)$why(981,545)mul(15,621):]when(384,172)%?from()/mul(556,42);/[/when()where()%select()mul(911,748);('mul(634,654)mul(187,910):;>mul(671,55)/>why()*mul(487,182) &mul(823,339^[*when()!~$do()mul(752,615)select()}select();) ;mul(891,463)why()who(749,89)&$who() *}-where()mul(483,646)>}-[/mul(463,928)) #when()*who(818,29)where()(&don't()when()%;mul(913,774)} mul(822,254)@]}-mul(949,6)where()from()when();;when()>@@(mul(631,409)where()select()who()~}mul(492,930)mul(388,777) }']select(441,164)!mul(957{{how()$]/%mul(626,541)~:do();mul(796,50)%^~mul(992,764)what(460,353))[what()how()>@;where()mul(944,226)(/?who()*@what()mul(986,707)!who() #++:mul(712,362)@)what()what(377,883)who(){who()~why()mul(570,45)-*why()what()!~<#mul(873,695)}{~ :~;'$+mul(526,676)?),/mul(106,52); ;%*how()from(165,744)what()from()do():?+ >;mul(151,167)mul~-!mul(873,682)%;;!~?mul(13,903)[$* }mul(188,728)#})(<mul(206,992)mul(112,681)who();from()why()mul(998,665)why(52,375)!mul(146,638){mul(783,242from(907,600))[why()@what()select()^ %*don't()mul(949]what(),,what()-<(%mul(519,474)who()~-)mul(640,389)%where()/~@ <&]why()mul(113,424)when()[mul(234,782;,(;-where()why()how()mul(838,733)why()mul(15,790)mul(239,592)^,select()!^*]mul(537,113) + )}>;why()#{ {mul(469,192)?#,'what()mul%!{>+&what()mul(392,708)'mul(156,225)?]+mul(436^who()why();@#!why()how()when()&mul(506,544)why()when()mul(211,662)what()/-)](from()(]?mul(530,199))}why()> mul(833,595)+?$ (mul(98,477)*/};{why()what(966,350)#'mul(44,78)why()!%]% ~}/mul(376,267)/why()'^&&;do()^{/+~how()mul(174,478);what() %{[+!mul}/<)where()@mul(825,710)[ ]mul(60,655)+mul(538,818)-*mul(917,316)!from()&[)mul ?why()'^mul(32,228)~@-~^^mul(803,610)$((;~when()how()mul(622,388)^from()why()mul(291,294)select()+ mul(61,978)mul(986,197)why(405,935)where() where()#{%mul(383,280)%#]*@mul(266select()<>mul(459,359)*&$who()-&+&from()mul(122,271)#mul(85,195)^>select()where()?&mul(953,467 @,^@}@@[mul(297,412)^mul(845,508)where()who()how()}[?when()~%why()mul(320,30)-mul(348,650)*~;&mul(694,123)#+why()mul(516,744)mul(873,547)-select()]mul(194,499)how()+what()>mul(542,764)who()%when()~mul(150,709)how()}where()@!{)from(773,172)mul(568,873)!+;mul(730,244)from()^ where()*[where()select()mul(554,163)(when()+$~where()*}@+don't()from()~select()who(421,469)&:)mul(279,687)who()%#^when() -select()}mul(8,389):)}why()do()when(984,806);!@:(&mul(59,374)&who():}:$ :mul(919,44)#)(!']why()select()mul(820,819){why(){*^#mul(439,942)>select()]/;select()/'mul(175,719)select()mul(890,515)+;)mul(611select()-mul(881,340)>~?mul(984,156)mul(330,398)/;why()^ :who()mul(677,667)what()&*what()what()mul(686,640)):-when(),(+mul(40,933)what(423,54)/&#+when()mul(914,37);how()!*~!when()/mul(456,51)when()when()!+]!what()#what()don't()what(225,210),from()~^[@>mul(370,841)what()from()@~-/mul(67,351)(&}mul(59,921)$--^*}mul(566,572)!how()+*mul(365,948)/$!@&mul(782,899)}/!}~how()mul(73,229);~what()when()*#&where(751,442)who()mul(487,586)when()mul(258,912)@~{what()#]>when()when()mul(124,236)<%(-from()mul(65,560)*when(73,56)mul(675,80)}:^don't()how()mul(32,556){select()%!where(500,153)@<&mul(445,212)from()&'who()who(910,924)}@~}what()mul#)why()(!)who(339,797)what()mul(151,749)how()%#>:select(761,257)]when()]mul(866,572)%@'what()~?where()&mul(544,274)-select())mul(159,997)mul(844,233)from(): +who()%what()select()%]do()%where()mul(226,633)/?]where()(when()?don't()$!~+![@mul(645,560)/who()from(715,999)?-mul(13,127)>#%'{mul(439,817)where(331,588)?+[mul(224,797)mul(464,432)mul(35,360)>when()%who()who()select()$#mul(356,503)#[mul(813,62){-<-where()##mul(241,813):[)mul(338,295)what(384,596)%;^who()mul(462,430)mul(182,412)+*/mul(616,721):;mul(10&&where()[^when(),@;{mul(954,803){])what()mul(23,586)mul(897,119)}]^&<{*mul(986,79)#??,,mul(613,13){from()when()mul(866,149)mul(772,870)(<:&who()*what()mul(515,452)what()why()$who()where()]mul(878,410)*mul(594,420)/}[where()>[+)mul(924,312)(#+select()$mul(310,382)$(mul(853,276)#$select()&]-:'when()mul(574,694):*mul(548@'why()[[mul(705,329)@why()#what();((where()mul(754,401)$:#*:#+%mul(958,635)@[)/select()@;why()(mul(648,81)!)from() +mul(792,904)'@@^}mul(987,166)>+?mul(282,196),[how()%!{?@+mul(486,579)%+who()'~why()mul(976,618)when()mul(763,421)mul(421,343)^!,/,-][@^mul(895,484)*how()(why()/from()mul(527,80)]-*:{%-%mul(568,730)-mul(923,402)(!how()%)?+what()^where()mul(744,416) why()mul(824,613)! who(339,629),mul(474,127)>^#select()select()>{from(955,780)mul(961,300)why();:how()*where(674,394)why()''mul(220,654)->)why())^-mul(352,662)?where()/from()mul(755,837)@::@why()mul(173,195){,#mul(369,738)^$?;how()do()@what()where()?@)select()$>$mul(610,930)mul(692,271)}(^@when()[@mul(825,772)),[$;what(364,840)how()&@mul(514,944)*(#^mul(600,592);why()&mul(360,432)>>$!when())from()why()mul(771,433)/who() $what()^^mul(759,696)mul(794,141)?*what();what()how()/mul(575,123)how()([!;],&mul(690,157){$~why()]why()don't()what()#@))~'[<)!from()what(){[]mul(901,549)'what()}from()why()when()how()<*mul(416,637)mul(518,747)!?when()why()?>don't()]]??]<;*^mul(946,35)what()[where()what()/[**/mul(60,183)/{how()~>>why()*mul(510,950)/%who()%{](,!'mul(871,310))]!mul(824,101)who(666,710))what()why()what()$how()%who()mul(96,822)how()who()mul(689,448)mul(469,609)!@^mul(977,145)& mul(836,253)?+where(42,895)?who()%mul(390,417)mul(641,894)what(312,406)(?when()how()?$mul(968,385)when()^^;mul(779,772)>/->(,where()mul(921,632):(select()(where(228,976)why():']mul(381,906) diff --git a/2024/03/solution.nix b/2024/03/solution.nix new file mode 100644 index 0000000..55c3209 --- /dev/null +++ b/2024/03/solution.nix @@ -0,0 +1,18 @@ +{lib, ...}: {file}: rec { + content = builtins.readFile file; + + muls = memory: memory + |> builtins.split ''mul\(([0-9]{0,3}),([0-9]{0,3})\)'' + |> builtins.filter builtins.isList + ; + + mul = builtins.foldl' + (acc: n: let + nums = map lib.strings.toInt n; + in acc + ( builtins.elemAt nums 0 ) * ( builtins.elemAt nums 1 )) + 0 + ; + + part1result = content |> muls |> mul; + +} diff --git a/2024/03/solution.test.nix b/2024/03/solution.test.nix new file mode 100644 index 0000000..8df9f9f --- /dev/null +++ b/2024/03/solution.test.nix @@ -0,0 +1,34 @@ +{describe, it, ...}: +let + pkgs = import {}; + solution = import ./solution.nix pkgs; + example = (solution {file = ./example.txt;}); + td = '' + xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5)) + ''; + tm = [ + ["2" "4"] + ["5" "5"] + ["11" "8"] + ["8" "5"] + ]; +in [ + (describe "part 1" [ + (it "gets content" { + actual = example.content; + expected = td; + }) + (it "finds muls" { + actual = example.muls td; + expected = tm; + }) + (it "multiplies" { + actual = example.mul tm; + expected = 161; + }) + (it "gets result" { + actual = example.part1result; + expected = 161; + }) + ]) +]