WackoMcGoose

AoC 2021 EXA Edition: Day 3

Dec 4th, 2021 (edited)
596
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ================================================================
  3.  * AdventOfCode2021-Day3.js - goes in your \Documents\My Games\EXAPUNKS\some-player-id\custom\ folder
  4.  * ================================================================
  5.  */
  6.  
  7. // For the latest Axiom VirtualNetwork+ scripting documentation,
  8. // please visit: http://www.zachtronics.com/virtualnetwork/
  9.  
  10. function getTitle() {
  11.     return "ADVENT OF CODE 2021 DAY 3";
  12. }
  13. function getSubtitle() {
  14.     return "POWERED BY EXAS® (and javascript)";
  15. }
  16. function getDescription() {
  17.     return "https://adventofcode.com/2021/day/3 \nParse the binary numbers in *file 300* using the registers, counting how many 1s appear in each column. 500+ gives a 1 in that column in the final result. Convert that number to base 10, and also bit-invert then convert to base 10. Then multiply those together.\nFor part 2, use *file 301* instead, write candidates to *#INPT* to count bits in the *target bit* position, then delete entries if *#EVAL* says they (don't/do) contain the \"most bit\", incrementing *#TRGT* each full cycle through the file. Don't forget to change the *#TIEB*reaker bit and swap behavior for the CO2 rating!";
  18. }
  19.  
  20. const INPUT_DATA = [/* PASTE YOUR INPUT DATA HERE*/];
  21.  
  22. /*  To format your input data, replace every newline with the following string:
  23.    
  24.     ", "
  25.    
  26.     including the quote marks, then a quote at start and end. Intent is to make every binary number a string.
  27. */
  28.  
  29. function initializeTestRun(testRun) {
  30.     var splitDigits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  31.     var digitRegs = [];
  32.    
  33.     var targetHost = createHost("SYSDIAG", 5, 0, 12, 3);
  34.    
  35.     for (var i = 0; i < 12; i++) {
  36.         var regName = "";
  37.         if (i < 10) { regName = "DG0" + i; } else { regName = "DG" + i; }
  38.         var reg = createRegister(targetHost, (5 + i), 2, regName);
  39.         digitRegs[i] = reg;
  40.     }
  41.     //unfortunately, because scope-ness, these have to individually be declared, one by one. ugh.
  42.     setRegisterReadCallback(digitRegs[0], function() {var ret = splitDigits[0]; return ret;});
  43.     setRegisterReadCallback(digitRegs[1], function() {var ret = splitDigits[1]; return ret;});
  44.     setRegisterReadCallback(digitRegs[2], function() {var ret = splitDigits[2]; return ret;});
  45.     setRegisterReadCallback(digitRegs[3], function() {var ret = splitDigits[3]; return ret;});
  46.     setRegisterReadCallback(digitRegs[4], function() {var ret = splitDigits[4]; return ret;});
  47.     setRegisterReadCallback(digitRegs[5], function() {var ret = splitDigits[5]; return ret;});
  48.     setRegisterReadCallback(digitRegs[6], function() {var ret = splitDigits[6]; return ret;});
  49.     setRegisterReadCallback(digitRegs[7], function() {var ret = splitDigits[7]; return ret;});
  50.     setRegisterReadCallback(digitRegs[8], function() {var ret = splitDigits[8]; return ret;});
  51.     setRegisterReadCallback(digitRegs[9], function() {var ret = splitDigits[9]; return ret;});
  52.     setRegisterReadCallback(digitRegs[10], function() {var ret = splitDigits[10]; return ret;});
  53.     setRegisterReadCallback(digitRegs[11], function() {var ret = splitDigits[11]; return ret;});
  54.    
  55.     var inputRegister = createRegister(targetHost, 5, 0, "INPT");
  56.     setRegisterWriteCallback(inputRegister, function(value) {
  57.         for (var i = 0; i < 12; i++) { splitDigits[i] = Number(value.substr(i, 1)); }
  58.     });
  59.    
  60.     setFileInitiallyCollapsed(createNormalFile(getPlayerHost(), 300, FILE_ICON_DATA, INPUT_DATA.concat(["000000000000"]))); //part 1, the 000000000000 at the end is essential
  61.     setFileInitiallyCollapsed(createNormalFile(getPlayerHost(), 301, FILE_ICON_DATA, INPUT_DATA)); //part 2
  62.    
  63.     //printConsole("101000010000 = " + parseInt("101000010000", 2).toString(10) + ", 010111101111 = " + parseInt("010111101111", 2).toString(10));
  64.     //using printConsole to simplify conversion of binary results into decimals, your answer will be different!
  65.    
  66.     //and now for part 2, we need an entire new host
  67.    
  68.     var partTwoHost = createHost("LFESUPRT", 5, -5, 5, 3);
  69.     createLink(targetHost, 800, partTwoHost, -1);
  70.    
  71.     /*
  72.      *  targetBit - 0 to 11, which bit to be thinking about. writing to this register also resets count registers.
  73.      *  targetBitOnes/ZeroesCount - internal, keeps track of what's what
  74.      *  tiebreakerBit - only evaluated if OnesCount == ZeroesCount
  75.      *  mostBit - whichever of OnesCount or ZeroesCount is *currently* in the lead
  76.      *
  77.      *  eval register - write a candidate, and will return 1 if it contains mostBit in the targetBit position
  78.      *
  79.      *  no need to do a most/least mode, and here's why:
  80.      *      - in "keep most" (oxy rating), remove item if eval returns 0 (targetBit is not mostBit)
  81.      *      - in "keep least" (co2 rating), remove item if eval returns 1 (targetbit IS mostBit, which we don't want)
  82.      */
  83.    
  84.     //configure by default to begin with bit position zero, with tiebreaker 1 (oxy rating mode)
  85.     var targetBit = 0, targetBitOnesCount = 0, targetBitZeroesCount = 0, tiebreakerBit = 1, mostBit = 0, evalState = 0;
  86.     var numEntriesRead = 0;
  87.    
  88.     var targetBitRegister = createRegister(partTwoHost, 5, -3, "TRGT");
  89.     setRegisterReadCallback(targetBitRegister, function() { return targetBit; });
  90.     setRegisterWriteCallback(targetBitRegister, function(value) {
  91.         targetBit = value; targetBitOnesCount = 0; targetBitZeroesCount = 0; numEntriesRead = 0; //reset count on set
  92.     });
  93.     var inputRegister = createRegister(partTwoHost, 5, -5, "INPT");
  94.     setRegisterReadCallback(inputRegister, function() { return numEntriesRead; }); //for sanity check
  95.     setRegisterWriteCallback(inputRegister, function(value) {
  96.         if (value.substr(targetBit, 1) == "1") { targetBitOnesCount++; } else { targetBitZeroesCount++; }
  97.         if (targetBitOnesCount > targetBitZeroesCount) { mostBit = 1;
  98.         } else if (targetBitOnesCount < targetBitZeroesCount) { mostBit = 0;
  99.         } else { //equal, tiebreak
  100.             if (tiebreakerBit == 1) { mostBit = 1; } else { mostBit = 0; }
  101.         }
  102.         numEntriesRead++;
  103.     });
  104.    
  105.     var tiebreakerBitRegister = createRegister(partTwoHost, 9, -5, "TIEB");
  106.     setRegisterReadCallback(tiebreakerBitRegister, function() { return tiebreakerBit; });
  107.     setRegisterWriteCallback(tiebreakerBitRegister, function(value) { tiebreakerBit = value; });
  108.     var mostBitRegister = createRegister(partTwoHost, 9, -3, "MSTB");
  109.     setRegisterReadCallback(mostBitRegister, function() { return mostBit; });
  110.     var evalRegister = createRegister(partTwoHost, 7, -5, "EVAL");
  111.     setRegisterReadCallback(evalRegister, function() { return evalState; });
  112.     setRegisterWriteCallback(evalRegister, function(value) {
  113.         if(value.substr(targetBit, 1) == mostBit.toString()) { evalState = 1; } else { evalState = 0; }
  114.     });
  115.    
  116.     //printConsole("111000001101 = " + parseInt("111000001101", 2).toString(10) + ", 010101101101 = " + parseInt("010101101101", 2).toString(10));
  117.     //using printConsole to simplify conversion of binary results into decimals, your answer will be different!
  118. }
  119.  
  120. function onCycleFinished() {
  121. }
  122.  
  123. /*
  124.     EXAcode for part 1:
  125.    
  126.     -- XA --
  127.     GRAB 300
  128.     LINK 800
  129.     VOID M;//WAIT 4 REPL
  130.     MARK LOOP
  131.     COPY F #INPT
  132.     JUMP LOOP
  133.    
  134.     -- XB --
  135.     LINK 800
  136.     @REP 10
  137.     REPL FOO@{0,1}
  138.     @END
  139.     COPY -1 M
  140.     HALT
  141.     @REP 10
  142.     MARK FOO@{0,1}
  143.     COPY 0 X
  144.     MARK LOOP@{0,1}
  145.     ADDI X #DG0@{0,1} X
  146.     JUMP LOOP@{0,1}
  147.     @END
  148.    
  149.     -- XC --
  150.     LINK 800
  151.     REPL FOO10
  152.     REPL FOO11
  153.     HALT
  154.     MARK FOO10
  155.     COPY 0 X
  156.     MARK LOOP10
  157.     ADDI X #DG10 X
  158.     JUMP LOOP10
  159.     MARK FOO11
  160.     COPY 0 X
  161.     MARK LOOP11
  162.     ADDI X #DG11 X
  163.     JUMP LOOP11
  164.    
  165.     solution is to go through each of the 12 exas still hanging around (should be XB:0 through XB:9, then XC:0 and XC:1), if the value in the X register is >= 500, put a 1, otherwise put a 0. for the other number, bit-invert. convert to decimal, multiply the two together, and that's your answer to part 1.
  166.    
  167.    
  168.     EXAcode for part 2:
  169.    
  170.     -- XA --
  171.     GRAB 301
  172.     LINK 800
  173.     LINK 800
  174.     COPY 1 #TIEB;//CHANGE 4 CO2
  175.     MARK LOOP
  176.     SEEK -9999
  177.     MARK INPUT
  178.     COPY F #INPT
  179.     TEST EOF
  180.     FJMP INPUT
  181.     SEEK -9999
  182.     MARK REMOVEBAD
  183.     COPY F #EVAL
  184.     COPY #EVAL X
  185.     TEST X = 0;//KEEP MOST      //<-- for co2 rating, change 0 to 1 to keep LEAST
  186.     FJMP REMOVENEXT
  187.     SEEK -1
  188.     VOID F
  189.     MARK REMOVENEXT
  190.     TEST EOF
  191.     FJMP REMOVEBAD
  192.     SEEK -9999
  193.     ADDI #TRGT 1 #TRGT
  194.     JUMP INPUT
  195.    
  196.     skip forward to the last SEEK -9999, repeatedly, until only one entry is left in the file. this is your oxy rating.
  197.     for co2 rating, make the indicated change to keep the least-common bits instead.
  198.     then convert both numbers to decimal, multiply, and that's your part 2 answer.
  199.  */
  200.  
Add Comment
Please, Sign In to add comment