Skip to content

Commit

Permalink
Merge pull request #50 from nuoxoxo/campus
Browse files Browse the repository at this point in the history
Campus
  • Loading branch information
nuoxoxo authored Dec 11, 2023
2 parents 3dfe2ae + 0ffc7d5 commit 3ddd198
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/gig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
# Runs on pushes targeting the default branch
push:
# branches: ['main']
branches-ignore: ['23']
branches-ignore: ['campus','23']
pull_request:
branches: ['main']
# Allows you to run this workflow manually from the Actions tab
Expand Down
119 changes: 115 additions & 4 deletions src/includes/Aoc2311.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
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/2311." + choice

var Aoc2311 = () => {

const [lines, setLines] = useState<string[]>([])
const [p1/*, setPart1*/] = useState<number>(0)
const [p2] = useState<number>(0)
const [null_lines, setNullLines] = useState<string[]>([])
const [p1, setPart1] = useState<number>(0)
const [p2, setPart2] = useState<number>(0)

const handleData = async () => {

try {
const raws = await FetchDataWithoutTrim(URL)
if ( ! raws[raws.length - 1].length)
raws.pop()
setLines(raws)
} catch (error: any) {
console.error("Error fetching data: ", error)
Expand All @@ -25,17 +28,125 @@ var Aoc2311 = () => {
handleData()
}, [])

useEffect(() => {
Solver()
}, [lines])

const Solver = () => {

const denseChars:string[] = [ '⬤', '◯', '◌', '◉', '✳', '✴', '✵', '✶', '✷', '✸', '✹', '✺']
const denseChar:string = denseChars[Math.floor(Math.random() * denseChars.length)]
if (lines === undefined || lines[0] === undefined) return

let coor: number[][] = []
let R = lines.length, C = lines[0].length, r = -1, c
while (++r < R) {
c = -1
while (++c < C) {
let node = lines[r][c]
if (node == '#')
coor.push([r, c])
}
}
let EC:number[] = []
let ER:number[] = []
let IA:string[][] = []
c = -1
while (++c < C) {
r = -1
let temp: string[] = []
while (++r < R) {
temp.push(lines[r][c])
}
IA.push(temp)
}
r = -1
while (++r < lines.length) {
let ok = true
for (let node of lines[r])
if (node != '.')
ok = false
if (ok)
ER.push(r)
}
c = -1
while (++c < IA.length) {
let ok = true
for (let node of IA[c])
if (node != '.')
ok = false
if (ok)
EC.push(c)
}
setPart1(calc(coor, ER, EC))
setPart2(calc(coor, ER, EC, 1e6))
let temp_null_lines: string[] = [...lines]
for (let ec of EC) {
let i = -1
while (++i < R) {
temp_null_lines[i] =
temp_null_lines[i].substring(0, ec) + '|' + temp_null_lines[i].substring(ec + 1)
temp_null_lines[i] =
temp_null_lines[i]
.replace(/\./g, ' ')
.replace(/\#/g, denseChar)
}
}
for (let er of ER) {
temp_null_lines[er] =
temp_null_lines[er]
.replace(/\ /g, '─')
.replace(/\|/g, '┼')
.replace(/\#/g, denseChar)
}
setNullLines(temp_null_lines)
}

const calc = (coor: number[][], ER:number[], EC:number[], xp: number = 2): number => {
// if (coor.length == 0) return 0
let res = 0
let i = -1
while (++i < coor.length) {

let [r, c] = coor[i]
for (let node_next of coor.slice(0, i)) {

let [next_r, next_c] = node_next
let [s, e] = [ Math.min(r, next_r), Math.max(r, next_r) ]
let node = s - 1
while (++node < e) {
if (ER.includes(node))
res += xp - 1
}
res += e - s
s = Math.min(c, next_c), e = Math.max(c, next_c)
node = s - 1
while (++node < e) {
if (EC.includes(node))
res += xp - 1
}
res += e - s
}
}
return res
}

return (
<>
{lines ? (
<>
<div className="playground">
<div className="field res-field res-field-1813">
<span>--- 2023 Day 10: Pipe Maze ---</span>
<span>--- 2023 Day 11: Cosmic Expansion ---</span>
<span>Part 1: {p1 ? p1 : "(empty)"} </span>
<span>Part 2: {p2 ? p2 : "(empty)"} </span>
</div>
</div>
<div className="field data-field data-field-2310">
{ null_lines ? null_lines.join('\n') : "No data available." }
</div>
<br/>⬆️
<br/><br/>
<div className="field data-field data-field-2310">
{ lines ? lines.join('\n') : "No data available." }
</div>
Expand Down
1 change: 1 addition & 0 deletions src/styles/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ body {

.data-field-2310 {
font-size: 10px;
text-align: center;
}

.data-field-1818 {
Expand Down

0 comments on commit 3ddd198

Please sign in to comment.