Skip to content

Commit

Permalink
finish 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Nov 28, 2024
1 parent 09a79e6 commit bf3948c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 45 deletions.
91 changes: 59 additions & 32 deletions 2021/22/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,71 @@ export async function taskOne(input: string[]): Promise<void> {
console.log(sum)
}

export async function taskTwo(_input: string[]): Promise<void> {
export async function taskTwo(input: string[]): Promise<void> {

interface Range<T> {
min: number // inc
max: number // inc
val: T
}

class RangeManager<T> {
public ranges: Range<T>[] = []
const allCubes: Entry[] = []

constructor(min: number, max: number, def: T) {
this.ranges.push({min,max, val:def})
for (const i of input) {
const r = /((?:on)|(?:off)) x=(-?[0-9]+)\.\.(-?[0-9]+),y=(-?[0-9]+)\.\.(-?[0-9]+),z=(-?[0-9]+)\.\.(-?[0-9]+)/.exec(i)
if (r == null) throw i
const b = r[1] == 'on'

const nums = r.slice(2).map(j => parseInt(j.trim()))
const newCube: Cube = {
x1: nums[0],
x2: nums[1]+1,
y1: nums[2],
y2: nums[3]+1,
z1: nums[4],
z2: nums[5]+1
}

public set(min: number, max: number, val: T) {
let i = 0;
while(this.ranges[i].min < min) {i++}
if (this.ranges[i])
const newCubes: Entry[] = []
if (b) {
newCubes.push({cube:newCube, factor: 1})
}

public get(min: number, max: number): Range<T>[] {
const r: Range<T>[] = []

let i = 0;
while(this.ranges[i].min < min) {i++}
if (this.ranges[i].max > max) {
return [{min, max, val: this.ranges[i].val}]
}
r.push({min, max: this.ranges[i].max, val: this.ranges[i].val})
i++
while(this.ranges[i].max < max) {
r.push(this.ranges[i])
i++
for (const oldCube of allCubes) {
const int = getIntersection(oldCube.cube, newCube)
if (int !== null) {
newCubes.push({cube:int, factor:-oldCube.factor})
}
r.push({min: this.ranges[i].min, max, val: this.ranges[i].val})
}
newCubes.forEach(c => allCubes.push(c))
}

const r = allCubes.map((e) => {
const c = e.cube
const v = (c.x2 - c.x1) * (c.y2 - c.y1) * (c.z2 - c.z1)
return v * e.factor
}).reduce((a,b)=>a+b,0)
console.log(r)

return r
function getIntersection(a: Cube, b: Cube): Cube|null {
const intersection = {
x1: Math.max(a.x1,b.x1),
x2: Math.min(a.x2,b.x2),
y1: Math.max(a.y1,b.y1),
y2: Math.min(a.y2,b.y2),
z1: Math.max(a.z1,b.z1),
z2: Math.min(a.z2,b.z2),
}
if (intersection.x1 > intersection.x2 || intersection.y1 > intersection.y2 || intersection.z1 > intersection.z2) {
return null
}
return intersection
}
}

interface Cube {
x1: number
x2: number
y1: number
y2: number
z1: number
z2: number
}

interface Entry {
cube: Cube
factor: number
}
}
30 changes: 17 additions & 13 deletions 2021/25/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,34 @@ export async function taskOne(input: string[]): Promise<void> {
let i = 0
do {
moved = false
const newField = field.map(i => i.map(j => '.'))
const newField = field.map(i => i.map(j => j))
for (let x = 0; x < xLen; x++) {
for (let y = 0; y < yLen; y++) {
if (field[y][x] == '>') {
if(field[y][(x+1)%xLen] == '.') {
newField[y][x] = '.'
newField[y][(x+1)%xLen] = '>'
moved = true
} else {
newField[y][x] = '>'
}
}
if (field[y][x] == 'v') {
if(field[(y+1)%yLen][x] == '.') {
newField[(y+1)%yLen][x] = 'v'
}
}
}
const newerField = newField.map(i => i.map(j => j))
for (let x = 0; x < xLen; x++) {
for (let y = 0; y < yLen; y++) {
if (newField[y][x] == 'v') {
if(newField[(y+1)%yLen][x] == '.') {
newerField[y][x] = '.'
newerField[(y+1)%yLen][x] = 'v'
moved = true
} else {
newField[y][x] = 'v'
}
}
}
}
}
}
field = newField


field = newerField
i++
console.log(i)
} while(moved)
console.log(i)

Expand Down

0 comments on commit bf3948c

Please sign in to comment.