Skip to content

Commit

Permalink
2018 19,20,21
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 6, 2024
1 parent 8271f47 commit f318db9
Show file tree
Hide file tree
Showing 3 changed files with 375 additions and 45 deletions.
106 changes: 91 additions & 15 deletions 2018/19/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,105 @@ export async function taskOne(input: string[]): Promise<void> {
while(reg[iR] < coms.length) {
reg = execute(reg, coms[reg[iR]])
reg[iR]++
if(reg[0] == 1)
console.log(reg)
}
console.log(reg[0])
}

export async function taskTwo(input: string[]): Promise<void> {
const coms: Command[] = []
const reg = [1,0,0,0,0,0]
const iR = Number(input[0].split(' ')[1])
for (let i = 1; i < input.length; i++) {
const s = input[i].split(' ')
coms.push({
ins: s[0],
a: Number(s[1]),
b: Number(s[2]),
c: Number(s[3])
})
}
let reg = [1,0,0,0,0,0]
while(reg[iR] < coms.length) {
console.log(reg[iR], reg)
reg = execute(reg, coms[reg[iR]])
while (reg[iR] < 36) {
switch (reg[iR]) {
case 0:
reg[3] += 16
break;
case 1:
reg[5] = 1
while(true) {
reg[4] = reg[1]+1
const div = reg[1] / reg[5]
if (reg[1] % reg[5] == 0 && div <= reg[1]) {
reg[0] += reg[5]
}
reg[5]++
if (reg[5] > reg[1]) {
reg[2] = 0
break
}
reg[2] = 1
}
reg[3] = 15
break
case 16:
reg[3] *= reg[3]
break;
case 17:
reg[1] += 2
break;
case 18:
reg[1] *= reg[1]
break;
case 19:
reg[1] *= reg[3]
break;
case 20:
reg[1] *= 11
break;
case 21:
reg[2] += 2
break;
case 22:
reg[2] *= reg[3]
break;
case 23:
reg[2] += 20
break;
case 24:
reg[1] += reg[2]
break;
case 25:
reg[3] += reg[0]
break;
case 26:
reg[3] = 0
break;
case 27:
reg[2] = reg[3]
break;
case 28:
reg[2] *= reg[3]
break;
case 29:
reg[2] += reg[3]
break;
case 30:
reg[2] *= reg[3]
break;
case 31:
reg[2] *= 14
break;
case 32:
reg[2] *= reg[3]
break;
case 33:
reg[1] += reg[2]
break;
case 34:
reg[0] = 0
break;
case 35:
reg[3] = 0
break;
default:
throw reg[iR]
break
}
reg[iR]++
}
console.log(reg)
console.log(reg[0])

}

function execute(input: number[], c: Command) {
Expand Down
204 changes: 176 additions & 28 deletions 2018/20/task.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import {Queue} from '../../base/simpleStructure'

export async function taskOne(input: string[]): Promise<void> {
let edges = new Set<string>()

process(input[0].substring(1, input[0].length-1), [[0,0]])
console.log(edges.size)
//console.log(edges.size, edges)

const minDistances = new Map<string, number>()
const queue = new Queue<Position>()
queue.push({x:0,y:0,d:0})
let max = 0
while(!queue.isEmpty()) {
const q = queue.pop()
const k = q.x + '_' + q.y
if (minDistances.has(k)) continue
minDistances.set(k, q.d)
if (q.d > max) max = q.d
for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
const a = s([q.x+dir[0],q.y+dir[1]],[q.x,q.y])
if (edges.has(a)) {
queue.push({x:q.x+dir[0],y:q.y+dir[1],d:q.d+1})
}
}
}
console.log(max)

function process(re: string, pos: number[][]): number[][] {
console.log(re.substring(0,100))
let ors: string[] = []
let openBrackets = 0
let cur = ''
Expand Down Expand Up @@ -39,26 +59,27 @@ export async function taskOne(input: string[]): Promise<void> {
lastBrack++
} while(openBrackets > 0);
const result = process(r.substring(l+1, lastBrack-1),newPos)
newPos.forEach(element => {
result.push(element)
});
l += lastBrack-1
newPos = result
l = lastBrack - 1
continue
}
const d = [0,0]
if (r[l] == 'N') {
d[1] = -1
} else if (r[l] == 'S') {
d[1] = 1
} else if (r[l] == 'W') {
d[0] = -1
} else if (r[l] == 'E') {
d[0] = 1
}
for (let p = 0; p < newPos.length; p++) {
addEdge(newPos[p], [newPos[p][0]+d[0],newPos[p][1]+d[1]])
newPos[p][0] = d[0]
newPos[p][1] = d[1]
} else {
const d = [0,0]
if (r[l] == 'N') {
d[1] = -1
} else if (r[l] == 'S') {
d[1] = 1
} else if (r[l] == 'W') {
d[0] = -1
} else if (r[l] == 'E') {
d[0] = 1
} else {
//console.log(r[l])
}
for (let p = 0; p < newPos.length; p++) {
addEdge(newPos[p], [newPos[p][0]+d[0],newPos[p][1]+d[1]])
newPos[p][0] += d[0]
newPos[p][1] += d[1]
}
}
}
newPos.forEach(element => finalPos.push(element))
Expand All @@ -70,28 +91,155 @@ export async function taskOne(input: string[]): Promise<void> {
}

function addEdge(e1: number[], e2: number[]) {

edges.add(s(e1,e2))

}
function s(e1: number[],e2:number[]) {

if (e1[0] == e2[0]) {
if (e1[1] < e2[1]) {
edges.add(s(e1,e2))
return si(e1,e2)
} else {
edges.add(s(e2,e1))
return si(e2,e1)
}
} else {
if (e1[0] < e2[0]) {
edges.add(s(e1,e2))
return si(e1,e2)
} else {
edges.add(s(e2,e1))
return si(e2,e1)
}
}
function si(c1: number[],c2:number[]) {

function s(c1: number[],c2:number[]) {
return `${c1[0]}_${c2[0]}|${c1[1]}_${c2[1]}`
return `${c1[0]}_${c1[1]}|${c2[0]}_${c2[1]}`
}

}
interface Position {
x: number,y:number,d:number
}
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
let edges = new Set<string>()

process(input[0].substring(1, input[0].length-1), [[0,0]])
//console.log(edges.size, edges)

const minDistances = new Map<string, number>()
const queue = new Queue<Position>()
queue.push({x:0,y:0,d:0})
let count = 0
while(!queue.isEmpty()) {
const q = queue.pop()
const k = q.x + '_' + q.y
if (minDistances.has(k)) continue
minDistances.set(k, q.d)
if (q.d >= 1000) count++
for (const dir of [[1,0],[-1,0],[0,1],[0,-1]]) {
const a = s([q.x+dir[0],q.y+dir[1]],[q.x,q.y])
if (edges.has(a)) {
queue.push({x:q.x+dir[0],y:q.y+dir[1],d:q.d+1})
}
}
}
console.log(count)

function process(re: string, pos: number[][]): number[][] {
let ors: string[] = []
let openBrackets = 0
let cur = ''
let i = 0
while(i < re.length) {
if (re[i] == '(') {
openBrackets++
cur += re[i]
} else if (re[i] == ')') {
openBrackets--
cur += re[i]
} else if (re[i] == '|' && openBrackets == 0) {
ors.push(cur)
cur = ''
} else {
cur += re[i]
}
i++
}
ors.push(cur)
const finalPos: number[][] = []
for (const r of ors) {
let newPos = copy(pos)
for (let l = 0; l < r.length; l++) {
if (r[l] == '(') {
let lastBrack = l
let openBrackets = 0
do {
if (r[lastBrack] == '(') openBrackets++
if (r[lastBrack] == ')') openBrackets--
lastBrack++
} while(openBrackets > 0);
const result = process(r.substring(l+1, lastBrack-1),newPos)
newPos = result
l = lastBrack - 1
continue
} else {
const d = [0,0]
if (r[l] == 'N') {
d[1] = -1
} else if (r[l] == 'S') {
d[1] = 1
} else if (r[l] == 'W') {
d[0] = -1
} else if (r[l] == 'E') {
d[0] = 1
} else {
//console.log(r[l])
}
for (let p = 0; p < newPos.length; p++) {
addEdge(newPos[p], [newPos[p][0]+d[0],newPos[p][1]+d[1]])
newPos[p][0] += d[0]
newPos[p][1] += d[1]
}
}
}
newPos.forEach(element => finalPos.push(element))
}


// deduplicate
return finalPos.filter((p,idx) => finalPos.findIndex(j => p[0]==j[0]&&p[1]==j[1]) == idx)
}

function addEdge(e1: number[], e2: number[]) {

edges.add(s(e1,e2))

}
function s(e1: number[],e2:number[]) {

if (e1[0] == e2[0]) {
if (e1[1] < e2[1]) {
return si(e1,e2)
} else {
return si(e2,e1)
}
} else {
if (e1[0] < e2[0]) {
return si(e1,e2)
} else {
return si(e2,e1)
}
}
function si(c1: number[],c2:number[]) {

return `${c1[0]}_${c1[1]}|${c2[0]}_${c2[1]}`
}

}
interface Position {
x: number,y:number,d:number
}
}

function copy<T>(v:T):T {
Expand Down
Loading

0 comments on commit f318db9

Please sign in to comment.