Skip to content

Commit

Permalink
bug fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
nuoxoxo committed Dec 13, 2023
1 parent 3484568 commit 467061a
Showing 1 changed file with 86 additions and 65 deletions.
151 changes: 86 additions & 65 deletions src/includes/Aoc2313.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { useState, useEffect } from "react"
import { FetchDataWithoutTrim, Deepcopy2DArray } from "../helpers/Helpers"

const suffixes = ['in', 'alt',]// 'test']
const suffixes = ["in", "alt"] // 'test']
const choice = suffixes[Math.floor(Math.random() * suffixes.length)]
const URL:string = "https://raw.githubusercontent.com/nuoxoxo/in/main/aoc/2313." + choice
const URL: string =
"https://raw.githubusercontent.com/nuoxoxo/in/main/aoc/2313." + choice

var Aoc2313 = () => {

const [A, setA] = useState<string[][][]>([])
const [B, setB] = useState<string[][][]>([])
const [C, setC] = useState<string[][][]>([])
const [Coors, setCoors] = useState<number[][]>([]) // [ p1/p2 , nth_bloc, h/v, idx ]
const [p1, setPart1] = useState<number>(0)
const [p2, setPart2] = useState<number>(0)
const [FinalB, setFinalB] = useState<string[][][]>([])
const [FinalC, setFinalC] = useState<string[][][]>([])

const handleData = async () => {

try {
const raws = await FetchDataWithoutTrim(URL)
if ( ! raws[raws.length - 1].length)
raws.pop()
const lines:string[][][] = []
const BB:string[][][] = []
const CC:string[][][] = []
let temp:string[][] = []
if (!raws[raws.length - 1].length) raws.pop()
const lines: string[][][] = []
const BB: string[][][] = []
const CC: string[][][] = []
let temp: string[][] = []
for (const line of raws) {
if (line.length == 0) {
lines.push(temp)
Expand Down Expand Up @@ -53,95 +53,110 @@ var Aoc2313 = () => {

useEffect(() => {
Solver()
}, [A,B,C])
}, [A])

useEffect(() => {
Planner()
}, [Coors, B, C])

useEffect(() => {
Renderer()
}, [Coors,B,C])
}, [FinalB, FinalC])

const Planner = () => {

for (const coor of Coors) {
let PlanB: string[][][] = B.map((level_2) =>
level_2.map((level_3) => level_3.map((char) => char))
)
let PlanC: string[][][] = C.map((level_2) =>
level_2.map((level_3) => level_3.map((char) => char))
)

// console.log('out:',coor)

for (const coor of Coors) {
let [part, nth_bloc, axis, index] = coor
// console.log(coor)

if ( part === 1 ) {

if (axis == 1) {// horizontal
B[nth_bloc].splice(index, 0, Array(B[nth_bloc][0].length).fill(['━']))
} else if (axis == 2) {//vertical
for (let line of B[nth_bloc]) {
line.splice(index, 0, '┃')
if (part === 1) {
if (axis == 1) {
// horizontal
PlanB[nth_bloc].splice(
index,
0,
Array(PlanB[nth_bloc][0].length).fill(["━"])
)
} else if (axis == 2) {
//vertical
for (let line of PlanB[nth_bloc]) {
line.splice(index, 0, "┃")
}
let top = Array(B[nth_bloc][0].length).fill([' '])
top[index] = '┃'
B[nth_bloc].unshift(top)
B[nth_bloc].push(top)
let top = Array(PlanB[nth_bloc][0].length).fill([" "])
top[index] = "┃"
PlanB[nth_bloc].unshift(top)
PlanB[nth_bloc].push(top)
}
} else if (part == 2){

if (axis == 1) {// horizontal
C[nth_bloc].splice(index, 0, Array(C[nth_bloc][0].length).fill(['━']))
} else if (axis == 2) {//vertical
for (let line of C[nth_bloc]) {
line.splice(index, 0, '┃')
} else if (part == 2) {
if (axis == 1) {
// horizontal
PlanC[nth_bloc].splice(
index,
0,
Array(PlanC[nth_bloc][0].length).fill(["━"])
)
} else if (axis == 2) {
//vertical
for (let line of PlanC[nth_bloc]) {
line.splice(index, 0, "┃")
}
let top = Array(C[nth_bloc][0].length).fill([' '])
top[index] = '┃'
C[nth_bloc].unshift(top)
C[nth_bloc].push(top)
let top = Array(PlanC[nth_bloc][0].length).fill([" "])
top[index] = "┃"
PlanC[nth_bloc].unshift(top)
PlanC[nth_bloc].push(top)
}
}
}
setB(B)
setC(C)
setFinalB(PlanB)
setFinalC(PlanC)
}

const Solver = () => {

let [res1, res2] = [0, 0]
let coordinates: number[][] = []
let p2 = true
let lhs, rhs, axis
let i = -1
while (++i < A.length) {
// for (const bloc of A) {
// for (const bloc of A) {

// transpose it yea !
// ++nth_bloc
let bloc:string[][] = A[i]
let tp:string[][] = bloc[0].map((_, col) => bloc.map(row => row[col]))
let bloc: string[][] = A[i]
let tp: string[][] = bloc[0].map((_, col) => bloc.map((row) => row[col]))

lhs = horver_calculator(bloc)
rhs = horver_calculator(tp)
res1 += lhs * 100 + rhs

axis = lhs !== 0 ? 1 : 2
coordinates.push([1, i, axis, lhs+rhs])
coordinates.push([1, i, axis, lhs + rhs])

lhs = horver_calculator(bloc, p2)
rhs = horver_calculator(tp, p2)
res2 += lhs * 100 + rhs

axis = lhs !== 0 ? 1 : 2
coordinates.push([2, i, axis, lhs+rhs])
coordinates.push([2, i, axis, lhs + rhs])
}

setCoors(coordinates)
setPart1(res1)
setPart2(res2)
}

const horver_calculator = (bloc: string[][], part2=false): number => {

const horver_calculator = (bloc: string[][], part2 = false): number => {
let index = 0
let i = 0
while (++i < bloc.length) {
let diff = 0, u = i - 1, d = i
let diff = 0,
u = i - 1,
d = i
let busted = false
while (u > -1 && d < bloc.length) {
let j = -1
Expand All @@ -160,7 +175,7 @@ var Aoc2313 = () => {
u -= 1
d += 1
}
if ( ! part2 && diff == 0 || part2 && diff == 1) {
if ((!part2 && diff == 0) || (part2 && diff == 1)) {
index = i
break
}
Expand All @@ -170,23 +185,30 @@ var Aoc2313 = () => {

const Renderer = () => {
return A.map((elemA, i) => {
elemA = [Array(elemA[0].length).fill([' ']), Array(elemA[0].length).fill([' ']), ...elemA]
elemA = [
Array(elemA[0].length).fill([" "]),
Array(elemA[0].length).fill([" "]),
...(elemA || []),
]
// 👆 unshift 2 blank lines now the left block (which is the orignal block) looks better
const elemB = B[i]
const elemC = C[i]
const elemB = FinalB[i] || [];
const elemC = FinalC[i] || [];

// Return JSX or components if you want to render them
return (
<div key={i} style={{
display:'flex',
flexDirection:'row',
justifyContent:'center',
alignContent:'center',
paddingBottom:'21px'
}}>
<div>{elemA.map((row)=>row.join('')).join('\n')}</div>
<div>{elemB.map((row)=>row.join('')).join('\n')}</div>
<div>{elemC.map((row)=>row.join('')).join('\n')}</div>
<div
key={i}
style={{
display: "flex",
flexDirection: "row",
justifyContent: "center",
alignContent: "center",
paddingBottom: "21px",
}}
>
<div>{elemA.map((row) => row.join("")).join("\n")}</div>
<div>{elemB.map((row) => row.join("")).join("\n")}</div>
<div>{elemC.map((row) => row.join("")).join("\n")}</div>
</div>
)
})
Expand Down Expand Up @@ -246,7 +268,6 @@ var Aoc2313 = () => {
.join('\n\n')
: "No data available."}
</div> */}

</div>
</>
) : (
Expand Down

0 comments on commit 467061a

Please sign in to comment.