-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path12.js
138 lines (117 loc) · 3.93 KB
/
12.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
const fs = require('fs')
const args = process.argv.slice(2)
const data = fs.readFileSync(args[0], 'utf8')
const lines = data.split('\n')
let map = []
lines.forEach(line => {
map.push(line.trim().split(''))
})
const regions = findAllRegions()
console.log('Del 1:', regions.map(region => regionFacts(region)).reduce((a, b) => a + b, 0))
function findAllRegions() {
let regions = []
for (let y = 0; y < map.length; y++) {
for (let x = 0; x < map[0].length; x++) {
let found = false
regions.forEach(region => {
if (region.has(`${x},${y}`)) {
found = true
return
}
})
if (!found) regions.push(findRegion([x, y]))
}
}
console.log('Regions:', regions.length)
return regions
}
function regionFacts(region) {
const area = region.size
const positions = [...region].map(p => p.split(',').map(x => parseInt(x)))
const type = map[positions[0][1]][positions[0][0]]
const perimeter = positions.reduce((acc, pos) => {
const nb = neighbours(pos, type)
const border = nb.filter(n => !region.has(n.join(',')))
let sumOutside = 0
const d = [[0, 1], [0, -1], [1, 0], [-1, 0]]
d.forEach(dir => {
const x = pos[0] + dir[0]
const y = pos[1] + dir[1]
if (map[y] == undefined || map[y][x] == undefined) sumOutside++
})
return acc + border.length + sumOutside
}, 0)
let sides = new Set()
let test = positions.sort((a, b) => a[1] - b[1])
for (const pos of test) {
const type = getType(pos[0], pos[1])
// Uppåt
let other = getType(pos[0], pos[1] - 1, 'UP')
let p = ['UP', pos[1] - 1]
if (other != type) {
console.log('UP', pos, other)
sides.add(p.join(','))
}
}
for (const pos of test) {
const type = getType(pos[0], pos[1])
// Nedåt
other = getType(pos[0], pos[1] + 1, 'DOWN')
p = ['DOWN', pos[1] + 1]
if (other != type) {
console.log('DOWN', pos, other)
sides.add(p.join(','))
}
}
for (const pos of test) {
const type = getType(pos[0], pos[1])
// Vänster
other = getType(pos[0] - 1, pos[1], 'LEFT')
p = [pos[0] - 1, 'LEFT']
if (other != type) {
console.log('LEFT', pos, other)
sides.add(p.join(','))
}
}
for (const pos of test) {
const type = getType(pos[0], pos[1])
// Höger
other = getType(pos[0] + 1, pos[1], 'RIGHT')
p = [pos[0] + 1, 'RIGHT']
if (other != type) {
console.log('RIGHT', pos, other)
sides.add(p.join(','))
}
}
console.log(type, 'Price:', area * perimeter, 'Area:', area, 'Perimeter:', perimeter, 'Sides:', sides, sides.size)
return area * perimeter
}
function getType(x, y, dir = 0) {
return map[y] == undefined ? dir : map[y][x] == undefined ? dir : map[y][x]
}
function findRegion(start) {
const queue = [start]
const found = new Set()
const type = map[start[1]][start[0]]
const visited = new Set()
found.add(start.join(','))
while (queue.length > 0) {
const current = queue.shift()
for (const nextpos of neighbours(current, type)) {
const next = nextpos.join(',')
found.add(current.join(','))
if (!visited.has(next)) queue.push(nextpos)
visited.add(next)
if (found.has(next)) continue
}
}
//console.log('Found region:', type, found.size)
return found
}
function neighbours(pos, type) {
const nb = [[0, 1], [0, -1], [1, 0], [-1, 0]]
const all = nb.map(n => [pos[0] + n[0], pos[1] + n[1]])
const active = all.filter(n => map[n[1]] != undefined && map[n[1]][n[0]] != undefined)
const sametype = active.filter(n => map[pos[1]][pos[0]] == type)
return sametype
}