Skip to content

Instantly share code, notes, and snippets.

@evilj0e
Last active December 4, 2017 07:13
Show Gist options
  • Save evilj0e/ceed1f10da4b824ad45f852f9b9fc738 to your computer and use it in GitHub Desktop.
Save evilj0e/ceed1f10da4b824ad45f852f9b9fc738 to your computer and use it in GitHub Desktop.

Revisions

  1. evilj0e revised this gist Dec 4, 2017. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions aoc-4-2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    var fs = require('fs');
    var text = fs.readFileSync('input.txt', 'utf8');
    var data = text.split('\n');

    function isValid(password) {
    var parts = password.split(' ');
    var isValid = true;
    var hash = {};

    for (var i = 0; i < parts.length; i++) {
    var part = parts[i].split('').sort().join('');

    if (hash[part] && hash[part] === 1) {
    isValid = false;
    break;
    } else {
    hash[part] = hash[part] ? (hash[part] + 1) : 1;
    }
    };

    return isValid;
    }

    function checkArray(data) {
    return data.reduce(function (acc, item) {
    return acc + isValid(item);
    }, 0);
    }

    console.log(checkArray(data));
  2. evilj0e created this gist Dec 4, 2017.
    30 changes: 30 additions & 0 deletions aoc-4-1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,30 @@
    var fs = require('fs');
    var text = fs.readFileSync('input.txt', 'utf8');
    var data = text.split('\n');

    function isValid(password) {
    var parts = password.split(' ');
    var isValid = true;
    var hash = {};

    for (var i = 0; i < parts.length; i++) {
    var part = parts[i];

    if (hash[part] && hash[part] === 1) {
    isValid = false;
    break;
    } else {
    hash[part] = hash[part] ? (hash[part] + 1) : 1;
    }
    };

    return isValid;
    }

    function checkArray(data) {
    return data.reduce(function (acc, item) {
    return acc + isValid(item);
    }, 0);
    }

    console.log(checkArray(data));