-
Notifications
You must be signed in to change notification settings - Fork 0
/
day06.js
151 lines (139 loc) · 3.79 KB
/
day06.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var fs = require('fs')
// input contains 50 points
const ids ="abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXY"
function load(file) {
var input = fs.readFileSync(file, 'utf8')
let points = []
var i = 0
input.split("\n").forEach(line => {
let items = line.split(",")
points.push({
name : ids.charAt(i),
x : +items[0],
y: +items[1]
})
i++
})
return points
}
function initWorld(points) {
var minX = 9999999
var minY = 9999999
var maxX = 0
var maxY = 0
points.forEach(point => {
if (minX>point.x) {
minX=point.x
}
if (minY>point.y) {
minY=point.y
}
if (maxX<point.x) {
maxX=point.x
}
if (maxY<point.y) {
maxY=point.y
}
})
width = maxX + 3
height = maxY + 3
var grid = []
for (let y = 0; y < height; y++) {
grid[y] = []
for (let x = 0; x < width; x++) {
grid[y][x] = {owner:'*', distance:999}
}
}
return {
minX: minX,
minY: minY,
width: width,
height: height,
grid: grid
}
}
function draw(world) {
for (let y = 0; y < world.height; y++) {
for (let x = 0; x < world.width; x++) {
process.stdout.write("" + world.grid[y][x].owner)
}
process.stdout.write("\n")
}
}
function distance(x,y,xx,yy) {
return Math.abs(xx - x) + Math.abs(yy - y)
}
function populateWorld(world, points) {
points.forEach(point => {
for (let y=0; y<world.height; y++) {
for (let x=0; x<world.width; x++) {
var dist = distance(point.x,point.y,x,y)
if (dist < world.grid[y][x].distance) {
world.grid[y][x].distance=dist
world.grid[y][x].owner=point.name
} else if (dist === world.grid[y][x].distance) {
world.grid[y][x].owner='.'
}
}
}
})
}
function computeSurface(world, points) {
let infinite = []
let count = {}
var largest = 0
points.forEach(point => {
count[point.name] = 0
})
for (let y=0; y<world.height; y++) {
for (let x=0; x<world.width; x++) {
var owner = world.grid[y][x].owner
if (x==0 || y==0 || x== world.width-1 || y == world.height-1) {
if (!infinite.includes(owner)) {
infinite.push(owner)
}
}
if (!infinite.includes(owner)) {
count[owner]++
if (largest < count[owner]) {
largest = count[owner]
}
}
}
}
return largest
}
function computeSafeLocation(world, points) {
for (let y=0; y<world.height; y++) {
for (let x=0; x<world.width; x++) {
points.forEach(point => {
if (world.grid[y][x].safeCount == undefined) {
world.grid[y][x].safeCount = 0
}
world.grid[y][x].safeCount += distance(x,y,point.x, point.y)
})
}
}
let safeRegionSurface = 0
for (let y=0; y<world.height; y++) {
for (let x=0; x<world.width; x++) {
if (world.grid[y][x].safeCount < 10000) {
safeRegionSurface++
}
}
}
return safeRegionSurface
}
function runDay06(file, shouldDraw) {
let points = load(file)
let world = initWorld(points)
populateWorld(world, points)
if (shouldDraw) {
draw(world)
}
console.log(file)
console.log("\tstep1",computeSurface(world, points))
console.log("\tstep2",computeSafeLocation(world, points))
}
runDay06('input06-test.txt', false)
runDay06('input06.txt', false)