-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday3.html
100 lines (86 loc) · 2.85 KB
/
day3.html
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
<html>
<body>
YO
<table>
<tr>
<td>9</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>8</td>
<td>1</td>
<td>4</td>
</tr>
<tr>
<td>7</td>
<td>6</td>
<td>5</td>
</tr>
</table>
<script>
const input = 289326
// const input =23
let table = [
[9,2,3],
[8,1,4],
[7,6,5]
]
let currentSquareSize = 3
drawTable = (input,table,currentSquareSize) => {
// starts at 9 since we have a table with 9 elements
for(var i=9; i <= input; i++){
if (currentSquareSize * currentSquareSize == i){
previousSquareSize = currentSquareSize // previousSquareSize = 3
currentSquareSize += 2 // currentSquareSize = 5
smallStartSquare = previousSquareSize * previousSquareSize // smallStartSquare = 9
startSquare = currentSquareSize * currentSquareSize // startSquare = 25
topArray = Array()
bottomArray = Array()
verticalIndexRight = 0
verticalIndexLeft = table.length - 1
topArray[0] = startSquare
for(var y=smallStartSquare+1; y <= startSquare - 1; y++){
if( smallStartSquare + 1 <= y && y <= smallStartSquare + previousSquareSize + 1){
// 25,10,11,12,13
topArray.push(y)
}else if ( smallStartSquare + previousSquareSize + 1 < y && y < smallStartSquare + 2 * (previousSquareSize + 1) ){
// 14,15,16
table[verticalIndexRight].push(y)
verticalIndexRight += 1
}else if ( smallStartSquare + 2 * (previousSquareSize + 1) <= y && y <= startSquare - (previousSquareSize + 1) ){
// 21,20,19,1}8,17
// 43,42,41,40,39,38,37
bottomArray.push(y)
// reversed afterwards
}else{
// 23,22,21
table[verticalIndexLeft].splice(0,0,y)
verticalIndexLeft -= 1
}
}
// console.log({topArray, bottomArray})
table.splice(0, 0, topArray)
table.push(bottomArray.reverse())
}
}
// console.table(table)
table
}
getDistance = (input) => {
drawTable(input, table, currentSquareSize)
table.map( (row,index) => {
if ( row.indexOf(input) !== -1)
a = [row.indexOf(input) + 1, index + 1]
})
table.map( (row,index) => {
if (row.indexOf(1) !== -1)
b = [row.indexOf(1) + 1, index + 1]
})
console.log({horizontal: a[0]-b[0],
vertical: a[1]-b[1]})
}
getDistance(input)
</script>
</body>
</html>