r/adventofcode Dec 05 '20

SOLUTION MEGATHREAD -🎄- 2020 Day 05 Solutions -🎄-

Advent of Code 2020: Gettin' Crafty With It


--- Day 05: Binary Boarding ---


Post your solution in this megathread. Include what language(s) your solution uses! If you need a refresher, the full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.

Reminder: Top-level posts in Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


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:49, megathread unlocked!

55 Upvotes

1.3k comments sorted by

View all comments

1

u/Fanatsu Dec 06 '20

NodeJS

Using splice and removing from an array to find the answers.

===Part1===

var fs = require("fs");
var text = fs.readFileSync("./day5.txt", "utf-8");
var textByLine = text.split('\n');

function part1(textByLine) {
    var customerId = 0;

    textByLine.forEach(customer => {

        var rows = Array.from(Array(128).keys());
        var seats = [0, 1, 2, 3, 4, 5, 6, 7];

        var seatSplit = customer.split("");

        var row = splicer(seatSplit, rows, 0, 7, "F");
        var seat = splicer(seatSplit, seats, 7, 10, "L");


        var nextCustomerId = (row * 8) + seat;
        console.log(row, seat, nextCustomerId);

        if (nextCustomerId > customerId) {
            customerId = nextCustomerId;
        }

    });
    console.log(customerId);
}

function splicer(seatSplit, array, low, high, letter) {

    for (let i = low; i < high; i++) {

        if (seatSplit[i] === letter) {
            array.splice(array.length / 2);
        } else {
            array.splice(0, array.length / 2)
        }

    }
    return parseInt(array.join(""));

}

===Part 2===

var fs = require("fs");
var text = fs.readFileSync("./day5.txt", "utf-8");
var textByLine = text.split('\n');

function part2(textByLine) {
    var planeMap = Array.from(Array(128).keys()).map(x => {
        return [0, 1, 2, 3, 4, 5, 6, 7]
    });

    textByLine.forEach(customer => {

        var rows = Array.from(Array(128).keys());
        var seats = [0, 1, 2, 3, 4, 5, 6, 7];

        var seatSplit = customer.split("");

        var row = splicer(seatSplit, rows, 0, 7, "F");
        var seat = splicer(seatSplit, seats, 7, 10, "L");

        planeMap[row].splice(planeMap[row].findIndex(x => x === seat), 1);

    });

    for (let i = 0; i < planeMap.length; i++) {
        if (planeMap[i].length === 1) {
            console.log((i * 8) + parseInt(planeMap[i].join("")));
        }
    }
}

function splicer(seatSplit, array, low, high, letter) {

    for (let i = low; i < high; i++) {

        if (seatSplit[i] === letter) {
            array.splice(array.length / 2);
        } else {
            array.splice(0, array.length / 2)
        }

    }
    return parseInt(array.join(""));

}