Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ================================================================
- * AdventOfCode2021-Day3.js - goes in your \Documents\My Games\EXAPUNKS\some-player-id\custom\ folder
- * ================================================================
- */
- // For the latest Axiom VirtualNetwork+ scripting documentation,
- // please visit: http://www.zachtronics.com/virtualnetwork/
- function getTitle() {
- return "ADVENT OF CODE 2021 DAY 3";
- }
- function getSubtitle() {
- return "POWERED BY EXAS® (and javascript)";
- }
- function getDescription() {
- 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!";
- }
- const INPUT_DATA = [/* PASTE YOUR INPUT DATA HERE*/];
- /* To format your input data, replace every newline with the following string:
- ", "
- including the quote marks, then a quote at start and end. Intent is to make every binary number a string.
- */
- function initializeTestRun(testRun) {
- var splitDigits = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
- var digitRegs = [];
- var targetHost = createHost("SYSDIAG", 5, 0, 12, 3);
- for (var i = 0; i < 12; i++) {
- var regName = "";
- if (i < 10) { regName = "DG0" + i; } else { regName = "DG" + i; }
- var reg = createRegister(targetHost, (5 + i), 2, regName);
- digitRegs[i] = reg;
- }
- //unfortunately, because scope-ness, these have to individually be declared, one by one. ugh.
- setRegisterReadCallback(digitRegs[0], function() {var ret = splitDigits[0]; return ret;});
- setRegisterReadCallback(digitRegs[1], function() {var ret = splitDigits[1]; return ret;});
- setRegisterReadCallback(digitRegs[2], function() {var ret = splitDigits[2]; return ret;});
- setRegisterReadCallback(digitRegs[3], function() {var ret = splitDigits[3]; return ret;});
- setRegisterReadCallback(digitRegs[4], function() {var ret = splitDigits[4]; return ret;});
- setRegisterReadCallback(digitRegs[5], function() {var ret = splitDigits[5]; return ret;});
- setRegisterReadCallback(digitRegs[6], function() {var ret = splitDigits[6]; return ret;});
- setRegisterReadCallback(digitRegs[7], function() {var ret = splitDigits[7]; return ret;});
- setRegisterReadCallback(digitRegs[8], function() {var ret = splitDigits[8]; return ret;});
- setRegisterReadCallback(digitRegs[9], function() {var ret = splitDigits[9]; return ret;});
- setRegisterReadCallback(digitRegs[10], function() {var ret = splitDigits[10]; return ret;});
- setRegisterReadCallback(digitRegs[11], function() {var ret = splitDigits[11]; return ret;});
- var inputRegister = createRegister(targetHost, 5, 0, "INPT");
- setRegisterWriteCallback(inputRegister, function(value) {
- for (var i = 0; i < 12; i++) { splitDigits[i] = Number(value.substr(i, 1)); }
- });
- setFileInitiallyCollapsed(createNormalFile(getPlayerHost(), 300, FILE_ICON_DATA, INPUT_DATA.concat(["000000000000"]))); //part 1, the 000000000000 at the end is essential
- setFileInitiallyCollapsed(createNormalFile(getPlayerHost(), 301, FILE_ICON_DATA, INPUT_DATA)); //part 2
- //printConsole("101000010000 = " + parseInt("101000010000", 2).toString(10) + ", 010111101111 = " + parseInt("010111101111", 2).toString(10));
- //using printConsole to simplify conversion of binary results into decimals, your answer will be different!
- //and now for part 2, we need an entire new host
- var partTwoHost = createHost("LFESUPRT", 5, -5, 5, 3);
- createLink(targetHost, 800, partTwoHost, -1);
- /*
- * targetBit - 0 to 11, which bit to be thinking about. writing to this register also resets count registers.
- * targetBitOnes/ZeroesCount - internal, keeps track of what's what
- * tiebreakerBit - only evaluated if OnesCount == ZeroesCount
- * mostBit - whichever of OnesCount or ZeroesCount is *currently* in the lead
- *
- * eval register - write a candidate, and will return 1 if it contains mostBit in the targetBit position
- *
- * no need to do a most/least mode, and here's why:
- * - in "keep most" (oxy rating), remove item if eval returns 0 (targetBit is not mostBit)
- * - in "keep least" (co2 rating), remove item if eval returns 1 (targetbit IS mostBit, which we don't want)
- */
- //configure by default to begin with bit position zero, with tiebreaker 1 (oxy rating mode)
- var targetBit = 0, targetBitOnesCount = 0, targetBitZeroesCount = 0, tiebreakerBit = 1, mostBit = 0, evalState = 0;
- var numEntriesRead = 0;
- var targetBitRegister = createRegister(partTwoHost, 5, -3, "TRGT");
- setRegisterReadCallback(targetBitRegister, function() { return targetBit; });
- setRegisterWriteCallback(targetBitRegister, function(value) {
- targetBit = value; targetBitOnesCount = 0; targetBitZeroesCount = 0; numEntriesRead = 0; //reset count on set
- });
- var inputRegister = createRegister(partTwoHost, 5, -5, "INPT");
- setRegisterReadCallback(inputRegister, function() { return numEntriesRead; }); //for sanity check
- setRegisterWriteCallback(inputRegister, function(value) {
- if (value.substr(targetBit, 1) == "1") { targetBitOnesCount++; } else { targetBitZeroesCount++; }
- if (targetBitOnesCount > targetBitZeroesCount) { mostBit = 1;
- } else if (targetBitOnesCount < targetBitZeroesCount) { mostBit = 0;
- } else { //equal, tiebreak
- if (tiebreakerBit == 1) { mostBit = 1; } else { mostBit = 0; }
- }
- numEntriesRead++;
- });
- var tiebreakerBitRegister = createRegister(partTwoHost, 9, -5, "TIEB");
- setRegisterReadCallback(tiebreakerBitRegister, function() { return tiebreakerBit; });
- setRegisterWriteCallback(tiebreakerBitRegister, function(value) { tiebreakerBit = value; });
- var mostBitRegister = createRegister(partTwoHost, 9, -3, "MSTB");
- setRegisterReadCallback(mostBitRegister, function() { return mostBit; });
- var evalRegister = createRegister(partTwoHost, 7, -5, "EVAL");
- setRegisterReadCallback(evalRegister, function() { return evalState; });
- setRegisterWriteCallback(evalRegister, function(value) {
- if(value.substr(targetBit, 1) == mostBit.toString()) { evalState = 1; } else { evalState = 0; }
- });
- //printConsole("111000001101 = " + parseInt("111000001101", 2).toString(10) + ", 010101101101 = " + parseInt("010101101101", 2).toString(10));
- //using printConsole to simplify conversion of binary results into decimals, your answer will be different!
- }
- function onCycleFinished() {
- }
- /*
- EXAcode for part 1:
- -- XA --
- GRAB 300
- LINK 800
- VOID M;//WAIT 4 REPL
- MARK LOOP
- COPY F #INPT
- JUMP LOOP
- -- XB --
- LINK 800
- @REP 10
- REPL FOO@{0,1}
- @END
- COPY -1 M
- HALT
- @REP 10
- MARK FOO@{0,1}
- COPY 0 X
- MARK LOOP@{0,1}
- ADDI X #DG0@{0,1} X
- JUMP LOOP@{0,1}
- @END
- -- XC --
- LINK 800
- REPL FOO10
- REPL FOO11
- HALT
- MARK FOO10
- COPY 0 X
- MARK LOOP10
- ADDI X #DG10 X
- JUMP LOOP10
- MARK FOO11
- COPY 0 X
- MARK LOOP11
- ADDI X #DG11 X
- JUMP LOOP11
- 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.
- EXAcode for part 2:
- -- XA --
- GRAB 301
- LINK 800
- LINK 800
- COPY 1 #TIEB;//CHANGE 4 CO2
- MARK LOOP
- SEEK -9999
- MARK INPUT
- COPY F #INPT
- TEST EOF
- FJMP INPUT
- SEEK -9999
- MARK REMOVEBAD
- COPY F #EVAL
- COPY #EVAL X
- TEST X = 0;//KEEP MOST //<-- for co2 rating, change 0 to 1 to keep LEAST
- FJMP REMOVENEXT
- SEEK -1
- VOID F
- MARK REMOVENEXT
- TEST EOF
- FJMP REMOVEBAD
- SEEK -9999
- ADDI #TRGT 1 #TRGT
- JUMP INPUT
- skip forward to the last SEEK -9999, repeatedly, until only one entry is left in the file. this is your oxy rating.
- for co2 rating, make the indicated change to keep the least-common bits instead.
- then convert both numbers to decimal, multiply, and that's your part 2 answer.
- */
Add Comment
Please, Sign In to add comment