-
Notifications
You must be signed in to change notification settings - Fork 0
/
22b.js
134 lines (113 loc) · 3.18 KB
/
22b.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// const input = `..#
// #..
// ...`;
const input = `.#.##..##...#...#.....##.
#.......###..#...#.#.....
##.###..#....#.##.###.##.
##...#.#.##..#.#.###.....
.#....#..#..#..#..#...###
##.####....#...#...###...
#.#########.####...##..##
...###....#.##..##.#...##
##.###....#...#.##.######
.#.##.###.#.#..####..#..#
###.....##..##.##.#.#...#
....#.##.#.#.####.#...#..
....#...#..#...######.##.
##........###.###..#####.
....#.#.#..#######...##..
###.....####..#..##..####
#...##.#....####..##.#...
.##.#.#.....#.#.#..##..##
.#.#.#.##...##.###...####
.#..#.#...#......#...#..#
##.....##...#..####...###
..#####.#..###...#.#.#..#
.####.#....##..##...##..#
#.##..#.##..#.#.##..#...#
##.###.#.##########.#####`;
const gridInput = input.split(/\n/)
.map(row => row.split(''));
class Grid {
constructor(grid) {
this.states = ['.', 'w', '#', 'f'];
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;
}
key(x, y) {
return x + ',' + y;
}
set(x, y, char) {
this.grid[this.key(x,y)] = char;
}
get (x,y) {
return this.grid[this.key(x,y)];
}
infect(x, y) {
this.set(x, y, '#');
this.newInfectCount++;
}
clean(x, y) {
this.set(x, y, '.');
}
weaken(x, y) {
this.set(x, y, 'w');
}
flag(x, y) {
this.set(x, y, 'f');
}
isInfected(x, y) {
return this.get(x, y) === '#';
}
isClean(x, y) {
let d = this.get(x, y);
return !d || d === '.';
}
isWeakened(x, y) {
return this.get(x, y) === 'w';
}
isFlagged(x, y) {
return this.get(x, y) === 'f';
}
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 isClean = this.isClean(this.pos.x, this.pos.y);
let isWeakened = this.isWeakened(this.pos.x, this.pos.y);
let isFlagged = this.isFlagged(this.pos.x, this.pos.y);
let isInfected = this.isInfected(this.pos.x, this.pos.y);
if (isClean) {
this.dir = this.rotate(this.dir, 90);
this.weaken(this.pos.x, this.pos.y);
}
else if (isWeakened) {
this.infect(this.pos.x, this.pos.y);
}
else if (isInfected) {
this.dir = this.rotate(this.dir, -90);
this.flag(this.pos.x, this.pos.y);
}
else if (isFlagged) {
this.dir = this.rotate(this.dir, 180);
this.clean(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 < 10000000; i++)
grid.burst();
console.log(grid.newInfectCount);