Giter Club home page Giter Club logo

advent-of-code-2018's Introduction

Paste these solutions directly into the dev console (ctrl+shift+i) at the input website (e.g. https://adventofcode.com/2018/day/3/input )

Day 1

eval(document.body.textContent);

Part 2

set = new Set();
document.body.textContent
  .repeat(1000)
  .trim()
  .split("\n")
  .map(x => +x)
  .reduce((sum, b) => {
    if (set.has(sum)) throw sum;
    set.add(sum);
    return sum + b;
  });

Day 3

Part 1

claims = document.body.textContent.trim().split("\n");
claimed = new Set();
dupes = new Set();
for (const claim of claims) {
  const coords = claim.match(/(?<x>\d+),(?<y>\d+): (?<w>\d+)x(?<h>\d+)/).groups;
  for (const k in coords) coords[k] = +coords[k];
  for (let x = coords.x; x < coords.x + coords.w; x++) {
    for (let y = coords.y; y < coords.y + coords.h; y++) {
      const inx = x * 1e6 + y;
      if (claimed.has(inx)) dupes.add(inx);
      else claimed.add(inx);
    }
  }
}
dupes.size;

Part 2 + Part 1

claims = document.body.textContent.trim().split("\n");
claimed = new Map();
dupes = new Set();
candidates = [];
for (const claim of claims) {
  const coords = claim.match(
    /#(?<id>\d+) @ (?<x>\d+),(?<y>\d+): (?<w>\d+)x(?<h>\d+)/
  ).groups;
  for (const k in coords) coords[k] = +coords[k];
  let broken = false;
  for (let x = coords.x; x < coords.x + coords.w; x++) {
    for (let y = coords.y; y < coords.y + coords.h; y++) {
      const inx = x * 1e6 + y;
      if (claimed.has(inx)) {
        dupes.add(inx);
        broken = true;
      }
      claimed.set(inx, coords.id);
    }
  }
  if (!broken) candidates.push({ id: coords.id, count: coords.w * coords.h });
}
console.log(dupes.size);
for (const { id, count } of candidates) {
  let hasc = 0;
  for (const oid of claimed.values()) if (oid === id) hasc++;
  if (hasc === count) throw id;
}

Day 4

sleepysum = {};
sleepytime = {};
guard = 0;
for (p of document.body.textContent
  .trim()
  .split("\n")
  .sort()) {
  [_, minute, tag, gid] = p.match(/:(..)\] (.)(?:uard #(\d+))?/);
  if (gid) guard = gid;
  if (tag == "f") start = +minute;
  if (tag == "w") {
    end = +minute;
    sleepysum[guard] = (sleepysum[guard] || 0) + (end - start);
    tgt = sleepytime[guard] || (sleepytime[guard] = []);
    for (let i = start; i < end; i++) tgt[i] = (tgt[i] || 0) + 1;
  }
}
[sleepyid] = Object.entries(sleepysum).sort((a, b) => b[1] - a[1])[0];
min = sleepytime[sleepyid].indexOf(
  Math.max(...sleepytime[sleepyid].filter(p => p))
);
console.log(sleepyid * min);
// part 2
[mostsle, max, min] = Object.entries(sleepytime)
  .map(([gid, mins]) => {
    x = Math.max(...mins.filter(p => p));
    return [gid, x, mins.indexOf(x)];
  })
  .sort((a, b) => b[1] - a[1])[0];
console.log(mostsle, min);

Day 5

collapse = txt => {
  r = Array(26)
    .fill(0)
    .map((_, i) => String.fromCharCode("a".charCodeAt(0) + i))
    .map(c => c + c.toUpperCase())
    .map(x => [
      x,
      x
        .split("")
        .reverse()
        .join("")
    ])
    .flat()
    .join("|");
  r = RegExp(r, "g");
  b = c = txt;
  do {
    b = c;
    c = b.replace(r, "");
  } while (c.length != b.length);
  return c.length;
};
console.log(collapse(document.body.textContent.trim()));

// part 2
orig = document.body.textContent.trim();
Math.min(
  ...Array(26)
    .fill(0)
    .map((_, i) => String.fromCharCode("a".charCodeAt(0) + i))
    .map(c => collapse(orig.replace(RegExp(c, "gi"), "")))
);

Day 6

inp = document.body.textContent
  .trim()
  .split("\n")
  .map(x => x.split(", ").map(p => +p))
  .map(([x, y], i) => ({ i, x, y }));
// a = Array.from({length:1000}, _ => Array.from({length:1000}, _ => 0))
m = inp.map(_ => 0);
nearest = (cx, cy) =>
  inp.reduce(
    ({ best, dist }, { x, y }, aft) => {
      ndist = Math.abs(cx - x) + Math.abs(cy - y);
      if (ndist == dist) return { best: -1, dist };
      if (ndist < dist) return { best: aft, dist: ndist };
      else return { best, dist };
    },
    { best: -1, dist: Infinity }
  ).best;

for (let x = 0; x < 1000; x++) {
  for (let y = 0; y < 1000; y++) {
    let n = nearest(x, y);
    if (n >= 0) m[n]++;
    if (x == 0 || y == 0 || x == 999 || y == 999) m[n] -= 1e6;
  }
}
console.log(Math.max(...m));

n2 = 0;
for (let x = 0; x < 1000; x++) {
  for (let y = 0; y < 1000; y++) {
    total = inp.reduce(
      (sum, { x: cx, y: cy }) => sum + Math.abs(cx - x) + Math.abs(cy - y),
      0
    );
    if (total < 1e4) n2++;
  }
}
console.log(n2);

advent-of-code-2018's People

Contributors

phiresky avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.