aboutsummaryrefslogtreecommitdiff
path: root/2015/puzzle-6.js
blob: 62c0382dea42bafa986e5d175580ce0568c4faec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const fs = require('node:fs/promises');

let arr = []

function count_1() {
  return arr.filter(x => x).length;
}

function count_2() {
  return arr.reduce((x, y) => x + y);
}

async function read_file() {
  const data = await fs.readFile("6-input", {encoding: 'utf8'});
  return data;
}

function parse_line(line) {
  let [type, ...rest] = line.split(" ")
  let obj = {type: "", bottom: [], top: []};
  if (type === "toggle") {
    obj.type = "toggle";
  } else {
    obj.type = rest[0];
    rest = rest.slice(1);
  }
  obj.bottom = rest[0].split(",").map(x => parseInt(x));
  obj.top = rest[2].split(",").map(x => parseInt(x));
  return obj
}

function execute_inst_1(inst) {
  for (let i = inst.bottom[0]; i <= inst.top[0]; ++i) {
    for (let j = inst.bottom[1]; j <= inst.top[1]; ++j) {
      if (inst.type === "on") {
        arr[(i * 1000) + j] = true;
      } else if (inst.type === "off") {
        arr[(i * 1000) + j] = false;
      } else {
        arr[(i * 1000) + j] = !arr[(i * 1000) + j];
      }
    }
  }
}

function execute_inst_2(inst) {
  for (let i = inst.bottom[0]; i <= inst.top[0]; ++i) {
    for (let j = inst.bottom[1]; j <= inst.top[1]; ++j) {
      if (inst.type === "on") {
        arr[(i * 1000) + j] += 1;
      } else if (inst.type === "off") {
        arr[(i * 1000) + j] = Math.max(0, arr[(i * 1000) + j] - 1);
      } else {
        arr[(i * 1000) + j] += 2;
      }
    }
  }
}

(async () => {
  let lines = (await read_file()).split(/\n/);
  const insts = lines.map(parse_line);

  for (let i = 0; i < 1000000; ++i) {
    arr.push(false);
  }
  for (let i = 0; i < insts.length; ++i) {
    execute_inst_1(insts[i]);
  }
  console.log("Round 1:", count_1());

  for (let i = 0; i < 1000000; ++i) {
    arr[i] = 0;
  }
  for (let i = 0; i < insts.length; ++i) {
    execute_inst_2(insts[i]);
  }
  console.log("Round 2:", count_2());
})()

// Local Variables:
// compile-command: "node puzzle-6.js"
// End: