From c685dcddfb707723ca8133c346c51f6145702444 Mon Sep 17 00:00:00 2001 From: Pratchaya Khansomboon Date: Tue, 3 Dec 2024 16:43:38 +0100 Subject: [PATCH] aoc24: day03 tokenizer --- CMakeLists.txt | 30 +++---- aoc.cpp | 140 ++--------------------------- aoc/24/01.hpp | 0 aoc/aoc.hpp | 4 + dat/24/ex/03.txt | 1 + dat/24/re/03.txt | 6 ++ sol/24/01/CMakeLists.txt | 15 ++++ sol/24/01/entry.cpp | 61 +++++++++++++ sol/24/02/CMakeLists.txt | 15 ++++ sol/24/02/entry.cpp | 7 ++ sol/24/03/CMakeLists.txt | 15 ++++ sol/24/03/entry.cpp | 186 +++++++++++++++++++++++++++++++++++++++ sol/24/CMakeLists.txt | 3 + 13 files changed, 328 insertions(+), 155 deletions(-) delete mode 100644 aoc/24/01.hpp create mode 100644 dat/24/ex/03.txt create mode 100644 dat/24/re/03.txt create mode 100644 sol/24/01/CMakeLists.txt create mode 100644 sol/24/01/entry.cpp create mode 100644 sol/24/02/CMakeLists.txt create mode 100644 sol/24/02/entry.cpp create mode 100644 sol/24/03/CMakeLists.txt create mode 100644 sol/24/03/entry.cpp create mode 100644 sol/24/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index b2afc2b..69eef59 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,8 @@ if (NOT MSVC) "-Werror" # fmt warnings "-Wno-unknown-attributes" + # ctre warning + "-Wno-missing-template-arg-list-after-template-kw" ) else() set(BASE_OPTIONS @@ -89,21 +91,21 @@ endif() set(HEADERS aoc/aoc.hpp ) -set(SOURCES "") -add_library(aoclib OBJECT ${HEADERS} ${SOURCES}) -target_include_directories(aoclib +set(SOURCES "aoc.cpp") +add_library(aoc OBJECT ${HEADERS} ${SOURCES}) +target_include_directories(aoc PUBLIC ${PROJECT_SOURCE_DIR} PRIVATE ${PROJECT_SOURCE_DIR}/aoc ) -target_compile_features(aoclib PRIVATE cxx_std_23) -target_compile_options(aoclib PRIVATE ${BASE_OPTIONS}) -target_compile_definitions(aoclib +target_compile_features(aoc PRIVATE cxx_std_23) +target_compile_options(aoc PRIVATE ${BASE_OPTIONS}) +target_compile_definitions(aoc PRIVATE ${BASE_DEFINITIONS} ) -target_link_libraries(aoclib +target_link_libraries(aoc PUBLIC fmt utf8cpp @@ -113,16 +115,4 @@ target_link_libraries(aoclib ) source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES}) -set(HEADERS "") -set(SOURCES aoc.cpp) -add_executable(aoc ${HEADERS} ${SOURCES}) -target_include_directories(aoc PRIVATE ${PROJECT_SOURCE_DIR}) -target_compile_features(aoc PRIVATE cxx_std_23) -target_compile_options(aoc PRIVATE ${BASE_OPTIONS}) -target_compile_definitions(aoc PRIVATE ${BASE_DEFINITIONS}) -target_link_libraries(aoc - PRIVATE - aoclib -) -source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES}) - +add_subdirectory(sol/24) diff --git a/aoc.cpp b/aoc.cpp index 024b902..01575d4 100644 --- a/aoc.cpp +++ b/aoc.cpp @@ -1,142 +1,12 @@ -#include -#include -#include -#include -#include -#include -#include - +#include "aoc/aoc.hpp" #include "fmt/format.h" -using levels_t = std::vector; -using reports_t = std::vector; - auto main([[maybe_unused]]int argc, [[maybe_unused]]char const* argv[]) -> int { - // constexpr auto filename = "./dat/24/ex/02.txt"; - constexpr auto filename = "./tmp.txt"; - std::ifstream strm{filename, std::ios::in | std::ios::binary}; - if (!strm.is_open()) { - fmt::print("Error opening file: {}\n", filename); + try { + aoc::entry({argv, std::next(argv, argc)}); + } catch (std::exception const& e) { + fmt::print(stderr, "Error at entry: {}\n", e.what()); return 1; } - - reports_t reports{}; - - levels_t tmp{}; - std::string str{}; - while (strm) { - auto const c = char(strm.peek()); - if (std::isdigit(c)) { - str += char(strm.get()); - continue; - } - if (c == ' ' && !str.empty()) { - tmp.emplace_back(std::stoi(str)); - str.clear(); - } - if (c == '\n' && !str.empty()) { - tmp.emplace_back(std::stoi(str)); - reports.emplace_back(std::move(tmp)); - str.clear(); - } - strm.get(); - } - - auto diff = reports | std::views::transform([](auto const& levels) { - return levels | std::views::adjacent_transform<2>([](auto const& a, auto const& b) { - return b - a; - }); - }); - - // condition: all increasing or all decreasing. - auto incdec = diff | std::views::filter([](auto const& levels) { - auto const n = std::ranges::all_of(levels, [](auto v) { return v < 0; }); - auto const p = std::ranges::all_of(levels, [](auto v) { return v > 0; }); - return n || p; - }); - - // condition: Any two adjacent levels differ by at least one and at most three. - auto adjdiff = incdec | std::views::filter([](auto const& levels){ - return std::ranges::all_of(levels, [](auto v) { - return std::abs(v) <= 3; - }); - }); - - auto const safe_reports = std::ranges::distance(adjdiff); - - fmt::print("Part A: {}\n", safe_reports); - - auto lefts = std::views::zip(diff, reports) | std::views::filter([](auto const& pair) { - auto const& [d, l] = pair; - auto const n = std::ranges::all_of(d, [](auto v) { return v < 0; }); - auto const p = std::ranges::all_of(d, [](auto v) { return v > 0; }); - return !(n || p); - }) | std::views::transform([](auto const& pair) { - auto const& [d, l] = pair; - return l; - }); - - auto masks = diff | std::views::filter([](auto const& vec) { - auto const n = std::ranges::all_of(vec, [](auto v) { return v < 0; }); - auto const p = std::ranges::all_of(vec, [](auto v) { return v > 0; }); - return !(n || p); - }) | std::views::transform([](auto const& vec) { - auto const p = std::count_if(std::begin(vec), std::end(vec), [](auto const& v) { return v >= 0; }); - auto const n = std::count_if(std::begin(vec), std::end(vec), [](auto const& v) { return v < 0; }); - auto mask = vec | std::views::transform([&](auto const& v) { - if (p > n) return v >= 0 ? 1 : 0; - else return v < 0 ? 1 : 0; - }) | std::ranges::to(); - mask.push_back(1); - return mask; - }); - - auto const remain_reports = std::views::zip(masks, lefts) - | std::views::filter([](auto const& pair) { - auto const& [mask, level] = pair; - auto const n = std::count_if(std::begin(mask), std::end(mask), [](auto const& v) { return v == 0; }); - return n == 1; - }) - | std::views::transform([](auto const& pair){ - auto const& [mask, level] = pair; - - return std::views::zip(mask, level) | std::views::filter([](auto const& pair) { - auto const& [m, v] = pair; - return m == 1; - }) | std::views::transform([](auto const& pair) { - auto const& [m, v] = pair; - return v; - }) | std::ranges::to(); - }) - | std::ranges::to(); - - auto rem_diff = remain_reports | std::views::transform([](auto const& vec) { - return vec | std::views::adjacent_transform<2>([](auto const& a, auto const& b) { return b - a; }); - }); - - auto safe_rems_vec = rem_diff | std::views::transform([](auto const& vec) { - return vec | std::views::filter([](auto const& a) { - return std::abs(a) <= 3; - }) | std::ranges::to(); - }) | std::ranges::to(); - - auto safe_rems = safe_rems_vec | std::views::filter([](auto const& vec) { - return vec.size() > 0; - }) | std::views::filter([](auto const& vec) { - auto const n = std::ranges::all_of(vec, [](auto v) { return v < 0; }); - auto const p = std::ranges::all_of(vec, [](auto v) { return v > 0; }); - return n || p; - }); - - auto const safe_reports_rem = std::ranges::distance(safe_rems); - - // for (auto rep : safe_rems) { - // for (auto v : rep) { - // fmt::print("{} ", v); - // } - // fmt::print("\n"); - // } - fmt::print("Part B: {}\n", safe_reports + safe_reports_rem); - return 0; } diff --git a/aoc/24/01.hpp b/aoc/24/01.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/aoc/aoc.hpp b/aoc/aoc.hpp index 7fd7377..bb923f8 100644 --- a/aoc/aoc.hpp +++ b/aoc/aoc.hpp @@ -1,7 +1,11 @@ #ifndef AOC_AOC_HPP #define AOC_AOC_HPP +#include +#include + namespace aoc { +auto entry([[maybe_unused]]std::vector const& args) -> void; } #endif // !AOC_AOC_HPP diff --git a/dat/24/ex/03.txt b/dat/24/ex/03.txt new file mode 100644 index 0000000..f274bda --- /dev/null +++ b/dat/24/ex/03.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/dat/24/re/03.txt b/dat/24/re/03.txt new file mode 100644 index 0000000..024e5b3 --- /dev/null +++ b/dat/24/re/03.txt @@ -0,0 +1,6 @@ +'{}mul(339,896)>^+!)^mul(799,303)don't()>mul(188,763)'<};who()select()%;+mul(924,355)mul(492,757) what()mul(582,171)][*+select()#mul(840,899){!when()from()%where()mul(318,527)} :!-'mul(530,886)?}>mul(937,475) $;),%:}mul(201,723)where()select()mul(673,729)why()who()^'who()mul(673,694)[+mul(295,161)[!how(88,740)*mul(364,904)how()<]when()+where()mul(329,432)when()mul(499,11)who(238,444))#~mul(775,866);,[)':where()%{[mul(835,890)+&&select()&[when()why(783,259) select()mul(735,871)!)when()'what()[/:mul(952,728)mul(633,505)@ -(?mul(176,469)*%what()>what()who()@{+do()'mul(117,634)-?(^^%:mul(234,514)where()@%mul(291,507)#from()*!*mul(668,282)@&)>,:select()>{%mul(195,300)-why()select()+&~>/^from()mul(801,834)why()~mul(463,630){*, from()$}:@mul(280,83)when()[mul(358,910)[;'why()where()mul(242,569)from()#<>from()&mul(553,455)%who())mul(284,63)%%*+?mul(437,226)* }how()when()~%'mul(57,491)]select(918,666)where()$when()why()'from()?]mul(321,301)'~:mul(619,356):mul(78,106)what()}!+~mul(609,442); $where()$who()mul(996,918)mul(217,653)@##:#mul(998,408))~<#where()from()who()who()what()(mul(305,980)-~(:>where();when()#mul(721,412)how()'< { mul(143,735){:]why(){#),@mul(670,301)$when(),}why()]?why(839,544)mul(120,681){when()$[?@-)mul(805,510)>from()))when();?'#mul(104,633)%<$%}why()mul(555,387)@$+mul(850,237)!^where()<}from()select()from()<@mul(298,559)who():from()+what();mul(556,540)$%<&(%don't()$/':'*)(mul(976,624)!~*/%why()mul(790,645):~^from()[{+*!mul(153,86)+select(){#!from()how()$mul(980,956)>from()select()}<}@}?~mul(151,20)select()mul(703*(){+]who()what()mul(827,322)+](}mul(531,132what()where()+mul(933,2){&$how()%#;]don't())[]mul(845,519)how(),]when()^mul(518,563)#,++$#mul(500,591)(#/what()where()how()from()mul(243,908);mul(574,691)/who(),who()how()&mul#{where()when()]!@mul(534,43)}do(),}/from()when()~{&@mul(92%what()~}mul(496,669)^(!+ ^~mul(28,334)mul(621,688)]mul(627,561))mul(206,37)]~^&mul(288,740,<@mul(540,77)<&:who(594,229)&'*who(){mul(923,453)mul(733,228)where()how()mul(104,17)/!why()~what()*@}mul(500,830)#'(&%{select()*?mul(301,211)]>@@,mul(21,358) ?mul(285,542)how()from())mul(361,19)(who()%}select(){*mul(362,324)<[]'&when()'mul,why()mul(352,273)mul(742,91)>mul(624,723)) ;@+mul(14,149)(from()%%,(mul(547,492)~+mul(712?@@@&{{mul(972,531) +]&%where()~}who()[how()]mul(602,51)how()+&>,{>] #do()from()~{,*[-mul(862,742)how()why()]%mul(432,72)what(){:do()%@!}-mul(663/+,what()--(&?mul(384,302)'@(mul(649,348)+from()%mul(184,596)~+}~mul(719,53)mul(634,179){-:where()mul(684,320where()when(395,300){who()how()^/;where()mul(849,756)!mul(530,108)#*+}what()^(]select()mul(333,615)[why()%?]~$how()mul(314,366)}where()mul(222,364)<){*[mul(449,95){:who(844,554)<;why()$who() mul(831,201))$mul(408,650)who()what()}<[do()~how()select()!]'why()]how(289,983)* <+%&mul(836,460)%mul(339,868)why():from()from()%mul(91,296)!+^,*when()who()from()-$mul(6,37)when()when()mul(69,574)who(),from(),how()mul(431,678~+how()]mul(644,184)-(?why(571,97)])why()from()mul(516>select()mul(67,86)+~%^!~what();mul(526,440)!+>?<:&mul(81,534)&}'mul(64,25)[-;mul(828$<>*mul(157,667)@[ *who()mul(356,285)select()(~*do()how()';why()&^?mul(165,944)select()mul(980,979)<:!~%mul(15why()$ mul(109,665)&-!why()]<'&mul(887,673)]mul(906,700)#mul}@-where()/{{ -mul(935,960),)''[{mul(533,431)what())'@mul(63,509)@why(464,997)$]mul(164,971)select()~where()how()#>' when()>mul(301,62) +;'+what()}!->mul(722,492))!'mul(262,457):@when()-/mul(902,705)~#(mul(640,550)/*$$#select()where(905,349)!&&don't()when()mul(998,104)select()from()select()'when()mul(37,27)!where()$:do()}mul(160,45)mul(716,642~,{+&+!}[}mul(281,768)who()-?;);%mul(270,620) mul(793from()(![(! : who()mul(481,293)?mul(264,360)where(){from();(select()~!from()mul(748,940)[~]why()$[+how(709,453)mul(590&!+*why()]when()mul(182,631)(how()?(select():;&]{mul(83,366)%when()when()&mul(878,366)why()[:,]mul(77,997);%/$&%]mul(827,204)mul(919,654)>,where()%+mul(678,952)who()@select()}*(mul(344,894)where()mul(408,29)#*!{}*~where(906,182)mul(144,162)!&#select()how()&why()~*#don't()-()]~:how()mul(803,649)]@?#;mul(170,978)mul(263,974)!@why()$how()@mul(155,265)&/%^/mul(571,825)$where()mul(507,171)from()^(~*mul(437,680)from()who()>select()}mul(332,921) where()mul(218,74)})from()/mul(470,570)why()@?who()don't()@({*mul(931,767)mul(486,567):&])%/{]%mul(901,942),' ]why()^where()do(){#,what()mul(331,184)how()when()how()*{:^){mul(339,48){'(what(545,390)mul(818,891)who()mul(828,226), how()where()'#,%?mul(798,324))';mul(337,760)[#mul(350,889)~#how()mul(859,480){}^?&select()where()do()~); who()^mul(640,455}why()#mul(744,51)'[who()/> select()mul^where()select()^}^mul(450,596))select()what()}&%;?mul(218,957)+*who(),}do()from()^} when()+[select(938,490))select()mul(406{how()(+who()()select()mul(329,937)&!mul(693,766)<{+}<[@mul)]%why()when()){~[ who()mul(888,144)~$:,mul(517,97)@) ~mul(394,320)why()when()who()(,%mul(761,855) +mul(22,362)('from(886,421)]mul(730,655)[@,how()(mul(692,165)]&$when()!}from()%mul(481,375)where()~mul(954,570)?why()-+mul(338,656)who()~ <}from()mul(616,31)where()/]:select())?from()mul(113,2):?$mul(295,905)];mul(410,181)@%${^how()>select()where()-mul(779>+what();^who(),>)mul(599,200)%~][select()>+>mul(486,481)*!who(693,495)-$mul(237,686)? how()! -@#do()#<(where()-&'>&what()mul(321,434)}@what()~/from()do()?who()$where()mul(328,792)select()how()mul(82,296)#,who())/when(637,168)mul(465,709)mul(208,775)^[@when()>>##<>mul(379,29)%mul(826,43){when()?who()*why()do()&[):@mul(411,966)^mul(24,557)<;where()mul(391,794)#;mul(592,819)+,}'%'mul(210,928)%mul(29,613);$who()why()!]who()mul(56,646)*@]-{~+:mul(425,457)>mul(896,578)%(how()](*where()when()select(237,23)mul(895,482)~<{mul(432,547)who(471,124){mul(483,785)*mul(422,876)^>& ;(^where()~#mul(709,114)(:;where()select()?%mul(263,276)&?;from()&&~(mul(113,694)who()mul(228,70): >[:@ ;@mul(707,104);:mul(423,229)&$[]>who()mul(194,895)><&&when()%%mul(836,144)<^!~)/;#who()>mul(786,723)?!#[mul(287;why()mul(734,761)!who()/<)mul(520,746)where(){!>>select()how()mul(185,986)mul(566,786)why()when()[~do(),}mul(188,610)/+^%-how()[mul(671,105)*[}[mul(403,996)mul(214+($?{when()mul(268,651),[>mul(660,864)%-/how()->([~mul(769,53)?from(197,675)^[-mul(83,519)where()select()don't()[:from()@{who() ?mul(305,335)[when()when();where(751,621)what()mul(395,86)how()?,who():>mul(349,362)how()?*select()when()from()who()where()''mul(414,725)*)when()select()]+mul(180,197)$who()why()&%'}mul(531who()#{mul(370,295)who()%mul(121,586)*^^%?>{,when()mul(944,189)&[/)select()>&^mul(222,28)@!where()'mul(449,827)^[mul(289,78)$how(287,947)mul(337,811)why()''-what()[/when(370,472)from()>mul(865,636){#mul(524,198)why(714,875)!*%mul(181,23)^)why()?:what(630,704)+}mul(569,165);when()')where(597,70):$where()why(),mul(15,411)* ) &don't()mul(124,709)$;/[+select()({mul(50,277))*;+*<#do()!@mul~mul(19,630),,,*+{mul(404,379)mul(72,663)when()don't()where(221,302)from()^>mul(942,445)+^from()]from()*{)mul(83,601)-+what(): where()what()from(),mulwhy()where(856,731)&/+mul(777,574)!+when()don't()*^mul(680,66)how()$mul(361,449):,how(766,248)#}&}[mul(869,603);;where()what()mul(385,816)[!' <-~from();mul(298,605*from()^mul(573,375)when()@(where()%mul(871,907)mul(718,918)?mul/~ ~what()-!select()~do()why():mul(682why()]mul(585,886)(,?+*?%!mul(684,834)what(786,470)mul(443,590))where(228,285) !/%?mul(815,879)#!/usr/bin/perl@mul(444,941)$select(687,764)%'(where()>-/mul(180,328) +[/:@how()*what()!+mul(911,368)?/what()~(+]mul(843?$,who()mul(865,234)@]/-from()mul(397,906)^mul(806,349)]how()where()^)+%select()when()~mul(827,131)]don't()@+/mul(44,818)[,<,mul(295,441)what()/select()]^mul(756,90) [mul(67,416){mul(230,994)select()how()who()/mul(66,226);<~!when()mul(325,467),mul(6,370)mul(619,21)~mul(699,668))^:mul(755,644)<%?#)mul(46,923!mul(730,880)]~]how()-why()'&mul(952,543)from()>!-mul(123,880){(mul(555,437)*mul(692,73)&*when()~>mul(465,602)(mul(471,204){%from();don't()$mul(945,735)from()select(520,626)>@who()]who()mul(615,73)):(;^^when(793,925)*&do(){mul(431,683)*+#select()where()from()?+mul(254,617)#%where()>;%don't()$who()#how()^[how()why()*mul(907where()select()(!:'!?@mul(208,995)}/:when()how(415,229)^-'from()mul;mul(22,79),']^?(mul(583,536):mul(355/-where()?~%,mul(990,358)//+&how() do()?who()%!}]mul(603,599)@mul(285,652){&@@mul(808,857when()when()%select()/'$@mul(585,541)from()@mul(136*#from()@mul(710,522) #*when()*}/mul(801,485) >/mul(393,477)where()(mul(13,599)what()when()(*%>@?];mul(808,562)>mul(407,85)mul(244/$&!+where()mul(67,663)-mul(934,570)]mul(857,473)who()mul(585,495)where()mul(45,904)!when()where())-:mul(747,283)why()#where();what()how(){'from()mul(405,574)[,?what()why()-([mul}where()select(450,140) /@who():@who(),>~mul(104,734)$]#-#who()mul(760,886)what()&where(352,510)->(don't()mul(863,264)$<)where()*/(@'mul(756,795)^)]mul(278,155):&!%$select()mul(189,750)[$#-/mul(549,580))^how(152,70):$mul(28,530)-],;]mul(33,157);!/+?what(253,786)%what()mul(841,40)+&when()why()^mul(898,936)!])mul(891,523);>mul(312,16)@how()*where()'where()<@?mul(967,420)/}why()~,mul(581,636)/ [mul(673,139)who()>mul(578,980):,%mul(75,107)+)how()-mul:/where()why()mul(315,687)!{%'what()mul(110,111)+ #:%!mul(731,760) +(+&mul(887,468)$::)],mul(765,973)'from()from()*mul(810,344)?what()mul(768,468)~'+)select()where()(select()where()$mul(576,358)%??'mul(41,789how()when()&-mul(606,191)!when()'~mul]]how():;~{how()mul(15,34)>%%*^how(54,122)$@mul(739,223)how()~*@!don't()]:{~'@>why()-]&how() **;}mul(900,428)select()]$'mul(874,363)what()@mul(892,45)^*+mul(387,178)?mul(823,845){where()mul(854,982);;/how()-why()mul(899,363)[where()who()what()>$]['do()<'how())mul(201,507)select(): >select()select()what()/where()*do()}@when(392,773)?mul(231,610){- why()*from()when():select():mul(334,751)from()how()~+who()-mul(811,647){^mul(116,805)^where()@}mul(691,631) when()&%from(),@^mul(336,461) ,what())who()when(540,382)'mul(549,430)%]from())@mul(339,808)?mul(264,497)'when()what()~who()~@how()$+mul(965,916)('who())%~from()from()%mul(776,506)/select()mul(385,184)*##select()mul(691,451)]$$mul(303,437);!when()&/<>]^mul(524,315)#^)mul(42,992who()}&-select()mul(902,182)(!''where()'+mul(48,755)@~what(644,7)$select()&%who()-mul(629,650)!^mul(822,985){/select()why()where()!who()%%mul(102,630)why()-{don't(),mul(166,527)'where()mul(245,921)select()-select()mul~>select()'%who()<+,mul(795,941))%$,what()where()>mul(414,585)from(26,999){mul(293,208)when()?#?mul(99,672)why()(select()+]when()mul(300,836)[}where()/mul(425,328))mul(702,532)?#(mul(45,856) ]/$mul(220,616)^when(){^* ?where():,mul(931,398)$;-mul(471,783)why(){:who()what()]>mul(276,590)select()when() /$(^<'mul+*&do()&)>;mul(785,175)mul(131,573),]from():(mul(833,970)! ><<(mul(496,285)&from()~select(){mul(296,374)?[#&from()from(){{when() mul(718,993)@who()&>mul(639,708)why()where()/-<{[ how()&mul(187,633)#:]:?mul(872,562)/who()[-who()(>$%}don't()!{:*mul(82,739)select()]+ ?$?@when()mul(830,429)+;:$>mul(390,30)(&@%&'} {%mul(208,444)mul(854,207);:@where(774,785)mul(120,222)mul(885,372)$ '<[mul(476,77)select()from()mul(305,758)}#[>;,@where()~how()(why(){mul(2,348)%@(~}how():mul(148,153)where()^:'why(907,374)+]mul(375,986);{]where()(!@what()]:mul(254,345))*)where()select()@?^#what()mul(94?what()!@when()select(),$?select()mul(966,420)select()/+{[;!(>what()mul(688,942)&]'^)'#!mul(363,573):$,from()select()mul(260,171)#mul(116,728)'(?mul(51,309)[!^mul(400,128))select()$-^how(740,875)where()@from()mul(319,269)select(844,13)who()#/mul(431,542)mul(794,709)- who()*do()*:)select()mul(12,579)@%])[what()&/mul(361,146)why()>:'-^&;mul(465,576))select()@%mul(101,476)who()%}' [#mul(13,38);:how()from()from()#}#&-mul(30,350)what()/+select()+]don't()!#:@;,[when()mul(303,869)when()where()]/'^!^ mul(938,614) {who()?!what(363,886)mul(439,873)>who()what()-+when()where()[why()mul?from()what()> where()who()!mul(510,226) {how()mul(353,498)]select()why()?[why()'*}(mul(709,649)~?select()*($>[from()mul(144,790)!%!mul(653,286)[<$)*+from()what(),from()mul(373,21))] select()?;+mul(601,965)what(), ,:mul(970,654)~why()(~*<+&mul(19,700)what()$what() <{({,)'how()-@mul(629,950)how()when()who()what(623,556)mul(891,30)(!don't()>who()who()%+ [)when()mul(808,962)+?<({~$#+do()}what()how()(what()*-@mul(296,108)>]^+&mul(592,463)~['%%& mul(733,447),#how()where()select()how()who()>mul(496,360)(&%*{+what()!mul(615,52)#&*,mul(807,92)&]mul(544,513){/+who(868,321)from()mul(164,401)how()][%what()where()(+[>mul(815,703)from()(when(),*select() {what()mul(437,310)who(226,447)mul(45,389)#&who()*[mul(659,114))what()#:+[%mul(686,605)<',,{who()^@-mul(127,293)& ;@mul(608,869)?%+*select(74,799)who()when()/^select()don't()where()$why()>why()when()%mul(752,203)-}'{^#;[%^mul(307,633)%when(970,30)mul(265,251)what(42,790)#mul(188,777)<:*& when():/when()?mul(94,809)mul(621,327)+;/*,,[select();~do()^(when()mul(896,407),$mul(280,745)*:){^~:({where(796,413)mul(262,847)why()&& const& args) -> void { +// constexpr auto filename = "./dat/24/re/01.txt"; +// std::ifstream strm{filename, std::ios::in}; +// if (!strm.is_open()) { +// fmt::print("Error opening file: {}\n", filename); +// return 1; +// } +// +// std::vector a{}; +// std::vector b{}; +// +// std::string str{}; +// while (strm) { +// auto const c = char(strm.peek()); +// if (std::isdigit(c)) { +// str += char(strm.get()); +// continue; +// } +// if (c == ' ') { +// if (!str.empty()) { +// a.emplace_back(std::stoi(str)); +// str.clear(); +// } +// } +// if (c == '\n') { +// if (!str.empty()) { +// b.emplace_back(std::stoi(str)); +// str.clear(); +// } +// } +// [[discard]]strm.get(); +// } +// +// fmt::print("a: {}, b: {}\n", a.size(), b.size()); +// +// std::sort(std::begin(a), std::end(a)); +// std::sort(std::begin(b), std::end(b)); +// +// auto diff_view = std::views::zip(a, b) | std::views::transform([](auto const& p) { +// auto const [x, y] = p; +// return x > y ? x - y : y - x; +// }); +// auto const sum = std::accumulate(std::begin(diff_view), std::end(diff_view), 0); +// +// fmt::print("Part A: {}\n", std::abs(sum)); +// +// auto values = a | std::views::transform([&b](auto v) { +// return std::pair{v, b | std::views::filter([v](auto c) { +// return v == c; // Filter elements in `b` equal to `v` +// }) | std::ranges::to()}; +// }) | std::ranges::to(); +// +// auto const meow = std::accumulate(std::begin(values), std::end(values), 0, [](auto const acc, auto const& pair) { +// return acc + std::int32_t(pair.first * pair.second.size()); +// }); +// +// fmt::print("Part B: {}\n", meow); +// } diff --git a/sol/24/02/CMakeLists.txt b/sol/24/02/CMakeLists.txt new file mode 100644 index 0000000..dc03b67 --- /dev/null +++ b/sol/24/02/CMakeLists.txt @@ -0,0 +1,15 @@ +set(HEADERS "") +set(SOURCES entry.cpp) +add_executable(sol2402 ${HEADERS} ${SOURCES}) +target_include_directories(sol2402 PRIVATE ${PROJECT_SOURCE_DIR}) +target_compile_features(sol2402 PRIVATE cxx_std_23) +target_compile_options(sol2402 PRIVATE ${BASE_OPTIONS}) +target_compile_definitions(sol2402 PRIVATE ${BASE_DEFINITIONS}) +target_link_libraries(sol2402 + PRIVATE + aoc +) +set_target_properties(sol2402 PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" +) +source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES}) diff --git a/sol/24/02/entry.cpp b/sol/24/02/entry.cpp new file mode 100644 index 0000000..4cc1e53 --- /dev/null +++ b/sol/24/02/entry.cpp @@ -0,0 +1,7 @@ +#include "aoc/aoc.hpp" +#include "fmt/format.h" + +auto aoc::entry([[maybe_unused]]std::vector const& args) -> void { + fmt::print("Hello, World!\n"); +} + diff --git a/sol/24/03/CMakeLists.txt b/sol/24/03/CMakeLists.txt new file mode 100644 index 0000000..78b6b5f --- /dev/null +++ b/sol/24/03/CMakeLists.txt @@ -0,0 +1,15 @@ +set(HEADERS "") +set(SOURCES entry.cpp) +add_executable(sol2403 ${HEADERS} ${SOURCES}) +target_include_directories(sol2403 PRIVATE ${PROJECT_SOURCE_DIR}) +target_compile_features(sol2403 PRIVATE cxx_std_23) +target_compile_options(sol2403 PRIVATE ${BASE_OPTIONS}) +target_compile_definitions(sol2403 PRIVATE ${BASE_DEFINITIONS}) +target_link_libraries(sol2403 + PRIVATE + aoc +) +set_target_properties(sol2403 PROPERTIES + RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}" +) +source_group(TREE "${CMAKE_CURRENT_LIST_DIR}" FILES ${HEADERS} ${SOURCES}) diff --git a/sol/24/03/entry.cpp b/sol/24/03/entry.cpp new file mode 100644 index 0000000..3f85a7f --- /dev/null +++ b/sol/24/03/entry.cpp @@ -0,0 +1,186 @@ +#include +#include +#include + +#include "aoc/aoc.hpp" +#include "fmt/format.h" +#include "ctre.hpp" + +#define ENUMERATOR_AOC_TOKENS \ + ENUMERATOR_AOC_TOKEN(mul , operator_ ) \ + ENUMERATOR_AOC_TOKEN(invalid , invalid ) \ + ENUMERATOR_AOC_TOKEN(numeric_literal, number ) \ + ENUMERATOR_AOC_TOKEN(newline , punctuation) \ + ENUMERATOR_AOC_TOKEN(paren_open , punctuation) \ + ENUMERATOR_AOC_TOKEN(paren_close , punctuation) \ + ENUMERATOR_AOC_TOKEN(comma , punctuation) \ + ENUMERATOR_AOC_TOKEN(identifier , identifier ) + +enum class token_type : std::uint32_t { +#define ENUMERATOR_AOC_TOKEN(type, category) type, + ENUMERATOR_AOC_TOKENS +#undef ENUMERATOR_AOC_TOKEN + _count +}; + +enum class token_category : std::uint32_t { + operator_, + invalid, + number, + punctuation, + identifier, + _count +}; + +auto token_type_str(token_type type) -> char const* { + switch (type) { + using enum token_type; +#define ENUMERATOR_AOC_TOKEN(type, category) case type: return #type; + ENUMERATOR_AOC_TOKENS +#undef ENUMERATOR_AOC_TOKEN + default: return "invalid"; + } +} + +auto token_type_category(token_type type) -> token_category { + switch (type) { + using enum token_category; +#define ENUMERATOR_AOC_TOKEN(type, category) case token_type::type: return category; + ENUMERATOR_AOC_TOKENS +#undef ENUMERATOR_AOC_TOKEN + default: return token_category::invalid; + } +} + +class token { +public: + token(std::string const& str, token_type type, token_category category, std::size_t row, std::size_t col) + : m_type(type) + , m_category(category) + , m_value(str) + , m_row(row) + , m_column(col) { } + + auto type() const -> token_type { return m_type; } + auto category() const -> token_category { return m_category; } + auto value() const -> std::string const& { return m_value; } + + auto row() const -> std::size_t { return m_row; } + auto col() const -> std::size_t { return m_column; } + + auto str() const -> std::string { + using namespace std::string_literals; + std::string str{"token {"}; + str += " type: "s + token_type_str(m_type) + ","s; + str += " value: \""s + m_value + "\","s; + str += " row: "s + std::to_string(m_row) + ","s; + str += " col: "s + std::to_string(m_column); + str += " }"; + return str; + } + +public: + inline static auto is_identifier(std::string_view const& str) -> bool { + return ctre::match<"^[a-z]+$">(str); + } + +private: + token_type m_type; + token_category m_category; + std::string m_value; + std::size_t m_row; + std::size_t m_column; +}; + +enum class lexer_error { + eof, + unknown +}; + +class lexer { +public: + lexer(std::filesystem::path const& source) + : m_strm(source, std::ios::in | std::ios::binary) + , m_line(1), m_col(1) { + } + + auto tokenize() -> std::vector { + std::vector tokens{}; + auto tk = next_token(); + while (tk) { + tokens.emplace_back(std::move(tk.value())); + tk = next_token(); + } + return tokens; + } + +private: + auto next_token() -> std::optional { + if (!has_next()) return {}; + if (peek() == '\n') { + peek_consume(); + m_line = m_line + 1; + m_col = 0; + } + + std::string str{}; + if (peek() == 'm') { + auto const col = m_col; + auto const is_valid_identifier_char = [](auto const c) { + return c >= 'a' && c <= 'z'; + }; + while (is_valid_identifier_char(peek())) str += peek_consume(); + auto const& type = token::is_identifier(str) ? token_type::identifier : token_type::invalid; + return token(str, type, token_type_category(type), m_line, col); + } + + if (peek() == '(') { + auto const col = m_col; + str += peek_consume(); + return token(str, token_type::paren_open, token_type_category(token_type::paren_open), m_line, col); + } + + if (peek() == ')') { + auto const col = m_col; + str += peek_consume(); + return token(str, token_type::paren_close, token_type_category(token_type::paren_close), m_line, col); + } + + if (peek() == ',') { + auto const col = m_col; + str += peek_consume(); + return token(str, token_type::comma, token_type_category(token_type::comma), m_line, col); + } + + auto const col = m_col; + str += peek_consume(); + return token(str, token_type::invalid, token_type_category(token_type::invalid), m_line, col); + } + + auto peek() -> char { + return static_cast(m_strm.peek()); + } + auto peek_consume() -> char { + ++m_col; + return static_cast(m_strm.get()); + } + auto has_next() const -> bool { + return !m_strm.eof(); + } + +private: + std::fstream m_strm; + std::size_t m_line; + std::size_t m_col; +}; + +auto aoc::entry([[maybe_unused]]std::vector const& args) -> void { + lexer lexer{"./dat/24/ex/03.txt"}; + + auto const tokens = lexer.tokenize(); + + for (auto const& tk : tokens) { + fmt::print("{}\n", tk.str()); + } +} + diff --git a/sol/24/CMakeLists.txt b/sol/24/CMakeLists.txt new file mode 100644 index 0000000..9f95ab8 --- /dev/null +++ b/sol/24/CMakeLists.txt @@ -0,0 +1,3 @@ +# add_subdirectory(01) +# add_subdirectory(02) +add_subdirectory(03)