diff --git a/src/includes/Aoc2302.tsx b/src/includes/Aoc2302.tsx index 765322f..2dbce90 100644 --- a/src/includes/Aoc2302.tsx +++ b/src/includes/Aoc2302.tsx @@ -12,6 +12,10 @@ var Aoc2302 = () => { const handleData = async () => { try { const raws = await FetchData(URL) + // raws.map((line) => "⭐" + line.substring(4)) // map method does not edit original array + for (let i = 0; i < raws.length; i++) { + raws[i] = "⭐" + raws[i].substring(4) + } setLines(raws) } catch (error: any) { console.error("Error fetching data: ", error) @@ -25,43 +29,44 @@ var Aoc2302 = () => { let r2 = 0 let idx = 1 for (let line of lines) { - line = line.trim() - let s: string[] = line.split(':')[1].split(' ') - s.shift() - let ok = true - let i = 0 - while (i < s.length - 1) { - let w = s[i + 1] - if (w[w.length-1] === ',' || w[w.length-1] === ';') - w = w.slice(0, -1) - let v = parseInt(s[i]) - if (D[w] < v) - ok = false - i += 2 - } - if (ok) - r1 += idx - // part 2 - let [R, B, G] = [0, 0, 0] - let rr: string[] = line.split(':')[1].split(';') - for (let el of rr) { - let e: string[] = el.split(' ') - e.shift() - i = 0 - while (i < e.length - 1) { - let v = parseInt(e[i]) - let k = e[i + 1] - if (k.includes('red')) - R = Math.max(R, v) - if (k.includes('blue')) - B = Math.max(B, v) - if (k.includes('green')) - G = Math.max(G, v) - i += 2 - } - } - idx++ - r2 += R * B * G + // console.log(line) + line = line.trim() + let s: string[] = line.split(':')[1].split(' ') + s.shift() + let ok = true + let i = 0 + while (i < s.length - 1) { + let w = s[i + 1] + if (w[w.length-1] === ',' || w[w.length-1] === ';') + w = w.slice(0, -1) + let v = parseInt(s[i]) + if (D[w] < v) + ok = false + i += 2 + } + if (ok) + r1 += idx + // part 2 + let [R, B, G] = [0, 0, 0] + let rr: string[] = line.split(':')[1].split(';') + for (let el of rr) { + let e: string[] = el.split(' ') + e.shift() + i = 0 + while (i < e.length - 1) { + let v = parseInt(e[i]) + let k = e[i + 1] + if (k.includes('red')) + R = Math.max(R, v) + if (k.includes('blue')) + B = Math.max(B, v) + if (k.includes('green')) + G = Math.max(G, v) + i += 2 + } + } + idx++ + r2 += R * B * G } setPart1(r1) setPart2(r2)