-
Notifications
You must be signed in to change notification settings - Fork 0
/
22.js
96 lines (82 loc) · 2.33 KB
/
22.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
// const input = `..#
// #..
// ...`;
const input = `.#.##..##...#...#.....##.
#.......###..#...#.#.....
##.###..#....#.##.###.##.
##...#.#.##..#.#.###.....
.#....#..#..#..#..#...###
##.####....#...#...###...
#.#########.####...##..##
...###....#.##..##.#...##
##.###....#...#.##.######
.#.##.###.#.#..####..#..#
###.....##..##.##.#.#...#
....#.##.#.#.####.#...#..
....#...#..#...######.##.
##........###.###..#####.
....#.#.#..#######...##..
###.....####..#..##..####
#...##.#....####..##.#...
.##.#.#.....#.#.#..##..##
.#.#.#.##...##.###...####
.#..#.#...#......#...#..#
##.....##...#..####...###
..#####.#..###...#.#.#..#
.####.#....##..##...##..#
#.##..#.##..#.#.##..#...#
##.###.#.##########.#####`;
const gridInput = input.split(/\n/)
.map(row => row.split(''));
class Grid {
constructor(grid) {
this.grid = {};
this.dir = {x:0,y:-1};
this.pos = {x:0,y:0};
this.newInfectCount = 0;
let b = (grid.length - 1) / 2;
for (let y = 0; y < grid.length; y++)
for (let x = 0; x < grid.length; x++)
this.set(x - b, y - b, grid[y][x]);
this.newInfectCount = 0;
}
set(x, y, char) {
let key = x + ',' + y;
this.grid[key] = char;
}
infect(x, y) {
this.set(x, y, '#');
this.newInfectCount++;
}
clean(x, y) {
this.set(x, y, '.');
}
isInfected(x, y) {
let key = x + ',' + y;
return this.grid[key] === '#';
}
rotate(vec, ang) {
ang = -ang * (Math.PI/180);
var cos = Math.cos(ang);
var sin = Math.sin(ang);
let result = [Math.round(10000*(vec.x * cos - vec.y * sin))/10000, Math.round(10000*(vec.x * sin + vec.y * cos))/10000];
return { x: parseInt(result[0]), y: parseInt(result[1]) };
}
burst() {
let isInfected = this.isInfected(this.pos.x, this.pos.y);
if (isInfected)
this.dir = this.rotate(this.dir, -90);
else
this.dir = this.rotate(this.dir, 90);
if (isInfected)
this.clean(this.pos.x, this.pos.y);
else
this.infect(this.pos.x, this.pos.y);
this.pos.x += this.dir.x;
this.pos.y += this.dir.y;
}
}
let grid = new Grid(gridInput);
for (let i = 0; i < 10000; i++)
grid.burst();
console.log(grid.newInfectCount);