Skip to content

Commit

Permalink
Merge pull request #41 from nuoxoxo/23
Browse files Browse the repository at this point in the history
+ 2302 add stars
  • Loading branch information
nuoxoxo authored Dec 2, 2023
2 parents cd35d48 + 6ed5adb commit 4867d01
Showing 1 changed file with 42 additions and 37 deletions.
79 changes: 42 additions & 37 deletions src/includes/Aoc2302.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4867d01

Please sign in to comment.