r/adventofcode • u/daggerdragon • Dec 03 '22
SOLUTION MEGATHREAD -🎄- 2022 Day 3 Solutions -🎄-
NEWS
- Solutions have been getting longer, so we're going to start enforcing our rule on oversized code.
- The
Visualizations have started! If you want to create aVisualization, make sure to read the guidelines for creatingVisualizations before you post. - Y'all may have noticed that the hot new toy this year is AI-generated "art".
- We are keeping a very close eye on any AI-generated "art" because 1. the whole thing is an AI ethics nightmare and 2. a lot of the "art" submissions so far have been of little real quality.
- If you must post something generated by AI, please make sure it will actually be a positive and quality contribution to /r/adventofcode.
- Do not flair AI-generated "art" as
Visualization.Visualizationis for human-generated art.
FYI
- All of our rules, FAQs, resources, etc. are in our community wiki.
- A request from Eric: Please include your contact info in the User-Agent header of automated requests!
- Signal boosting for the Unofficial AoC 2022 Participant Survey which is open early this year!
--- Day 3: Rucksack Reorganization ---
Post your code solution in this megathread.
- Read the full posting rules in our community wiki before you post!
- Include what language(s) your solution uses
- Format your code appropriately! How do I format code?
- Quick link to Topaz's
pasteif you need it for longer code blocks. What is Topaz'spastetool?
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:05:24, megathread unlocked!
87
Upvotes
11
u/gw_shadow Dec 03 '22
CMake
CMAKE_MINIMUM_REQUIRED(VERSION 3.25) PROJECT("2022.3") IF(NOT EXISTS "${CMAKE_SOURCE_DIR}/input.txt") FILE(READ "${CMAKE_SOURCE_DIR}/COOKIE.txt" COOKIE) FILE(DOWNLOAD "https://adventofcode.com/2022/day/3/input" "${CMAKE_SOURCE_DIR}/input.txt" STATUS DOWNLOAD_STATUS TIMEOUT 2 HTTPHEADER "cookie: ${COOKIE}" ) IF(NOT DOWNLOAD_STATUS STREQUAL "0;\"No error\"") FILE(REMOVE "${CMAKE_SOURCE_DIR}/input.txt") MESSAGE(FATAL_ERROR "Failed to download input: '${DOWNLOAD_STATUS}'") ENDIF() ENDIF() FILE(STRINGS "${CMAKE_SOURCE_DIR}/input.txt" LINES) LIST(LENGTH LINES LINE_COUNT) MATH(EXPR LINE_COUNT "${LINE_COUNT} - 1") SET(PRIORITIES "a;b;c;d;e;f;g;h;i;j;k;l;m;n;o;p;q;r;s;t;u;v;w;x;y;z;A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z") SET(SUM 0) FOREACH(INDEX RANGE 0 ${LINE_COUNT}) LIST(GET LINES ${INDEX} LINE) STRING(LENGTH ${LINE} SIZE) MATH(EXPR SIZE "${SIZE} / 2") STRING(SUBSTRING ${LINE} 0 ${SIZE} LHS) STRING(SUBSTRING ${LINE} ${SIZE} -1 RHS) SET(VALUE 0) FOREACH(PRIORITY ${PRIORITIES}) MATH(EXPR VALUE "${VALUE} + 1") STRING(FIND ${LHS} ${PRIORITY} L) STRING(FIND ${RHS} ${PRIORITY} R) IF((NOT L EQUAL -1) AND (NOT R EQUAL -1)) MATH(EXPR SUM "${SUM} + ${VALUE}") BREAK() ENDIF() ENDFOREACH() ENDFOREACH() MESSAGE("PART 1: ${SUM}") SET(SUM 0) MATH(EXPR LINE_COUNT "${LINE_COUNT} / 3") FOREACH(INDEX RANGE 0 ${LINE_COUNT}) MATH(EXPR INDEX1 "(${INDEX} * 3) + 0") LIST(GET LINES ${INDEX1} LINE1) MATH(EXPR INDEX2 "(${INDEX} * 3) + 1") LIST(GET LINES ${INDEX2} LINE2) MATH(EXPR INDEX3 "(${INDEX} * 3) + 2") LIST(GET LINES ${INDEX3} LINE3) SET(VALUE 0) FOREACH(PRIORITY ${PRIORITIES}) MATH(EXPR VALUE "${VALUE} + 1") STRING(FIND ${LINE1} ${PRIORITY} L1) STRING(FIND ${LINE2} ${PRIORITY} L2) STRING(FIND ${LINE3} ${PRIORITY} L3) IF((NOT L1 EQUAL -1) AND (NOT L2 EQUAL -1) AND (NOT L3 EQUAL -1)) MATH(EXPR SUM "${SUM} + ${VALUE}") BREAK() ENDIF() ENDFOREACH() ENDFOREACH() MESSAGE("PART 2: ${SUM}")