I am stuck in debug hell. Code works for testdata, not for realdata
I am not loosing any datasets, as far as i can tell
of course i can provide a dataset, if necessary
console.log('day5a')
import { testData, realData} from "./day5data.js";
let data = testData.split('n')
let grid = createGrid(1000,1000)
let allReadings = []
getReadings()
drawLines()
let crossings = countCrossings()
console.log('crossings: ', crossings)
// console.log('hvReading: ', hvReadings)
function drawLines(){
let x = allReadings.length
console.log('allReadings.length ', allReadings.length)
let noneForgotten = true
allReadings.forEach(reading => {
let x1= reading[0][0]*1, y1=reading[0][1]*1, x2= reading[1][0]*1, y2=reading[1][1]*1
if (x1 > x2 && y1 > y2) drawX1andY1bigger(x1, y1, x2, y2)
else if(x1 > x2 && y1 < y2) drawX1andY2areBigger(x1, y1, x2, y2)
else if(x1 < x2 && y1 < y2) drawX2andY2areBigger(x1, y1, x2, y2)
else if(x1 < x2 && y1 > y2) drawX2andY1areBigger(x1, y1, x2, y2)
else if(y1===y2){
if(x1 > x2) drawHorizontal(y2,x2,x1)
else drawHorizontal(y2,x1,x2)
} else if(x1===x2) {
if(y1>y2) drawVertical(x1, y2, y1)
else drawVertical(x1, y1, y2)
}
else {
console.log(reading, ' DONT FORGET ME')
noneForgotten = false
}
})
console.log(noneForgotten, ' noone')
}
// diagonal drawings
function drawX2andY1areBigger(x1, y1, x2, y2){
for(;x1<=x2;x1++,y1--){
grid[x1][y1]++
}
}
function drawX2andY2areBigger(x1, y1, x2, y2){
for(;x1<=x2;x1++,y1++) {
grid[x1][y1]++
}
}
function drawX1andY2areBigger(x1, y1, x2, y2){
for(;x1>=x2;x1--,y1++) {
grid[x1][y1]++
}
}
function drawX1andY1bigger(x1, y1, x2, y2){
for(;x1>=x2;x1--, y1--) {
grid[x1][y1]++
}
}
// horizontal drawings
function drawHorizontal(row, startCol, endCol){
for (let i = startCol; i <= endCol ; i++) {
grid[row][i]++
}
}
function drawVertical(col, startRow, endRow){
for (let i = startRow; i <= endRow; i++) {
grid[i][col]++
}
}
function getReadings(){
for (let i = 0; i < data.length; i++) {
data[i] = data[i].split(' -> ')
for (let j = 0; j < data[i].length; j++) {
data[i][j] = data[i][j].split(',').map(Number)
}
allReadings.push(data[i])
}
}
function createGrid (cols, rows) {
let grid = []
for (let i = 0; i < cols; i++) {
grid[i] = []
for (let j = 0; j < rows; j++) {
grid[i][j] = 0
}
}
return grid
}
function countCrossings(){
let crossings = 0
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[i].length; j++) {
if(grid[i][j] >=2) {
crossings++
}
}
}
console.log('crossings', crossings)
return crossings
}
wont let me post without more explanation, so: i am creating a two dimensional array, filled with zeros in createGrid()
i import the dataset, split it at linebrack, split every array in that at the ‘ -> ‘ arrow, split every array in that at the semicolon. i’m mapping it to number, just to be sure
after that, i draw “lines” at drawLines. depending on the coordinates, i raise the grid[i][j]++ in the functions described from line 44 to line 80
will provide further informationbs if nessecary