Skip to content

Commit

Permalink
some 2021
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 25, 2023
1 parent 6a95fc7 commit 3e75003
Show file tree
Hide file tree
Showing 9 changed files with 363 additions and 4 deletions.
29 changes: 29 additions & 0 deletions 2021/19/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
188 changes: 188 additions & 0 deletions 2021/19/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import {parseNumberList} from '../../base'

export async function taskOne(input: string[]): Promise<void> {
let i = 1
const s: Scanner[] = []
while(i < input.length) {
const scanner: Scanner = {b:[],mid:[],max:[],min:[]}
while(i < input.length && input[i] != "") {
const coord = (parseNumberList(input[i]) as Coord)
scanner.b.push(coord)
i++
}
s.push(scanner)
i+=2
}
s.forEach(i => {
for (let j = 0; j < i.b.length; j++) {
for (let k = 0; k < i.b.length;k++) {
const rel = i.b[j].map((a,ind) => Math.abs(a-i.b[k][ind]))
const ma = Math.max(...rel)
const mi = Math.min(...rel)
i.max.push(ma)
i.min.push(mi)
i.mid.push(rel.filter(i=>i!=ma && i!=mi)[0])
}
}
})
let pot = findMatches(s)
const scanner: Record<number, {sc: Coord}&Scanner> = {}
scanner[0]= {
sc: [0,0,0],
b: s[0].b,
...buildRels(s[0].b)
}
i = -1
while(pot.length > 0) {
i++
if (i >= pot.length) i = 0
const p = pot[i]
if (scanner[p[0]] != undefined && scanner[p[1]] != undefined) {
pot = pot.slice(0,i).concat(pot.slice(i+1))
i--
continue
}
if (scanner[p[0]] == undefined && scanner[p[1]] == undefined) continue

let known = -1
let unknown = -1
if (scanner[p[0]] != undefined) {
known = p[0]
unknown = p[1]
} else {
known = p[1]
unknown = p[0]
}

for (let or = 0; or < 48; or++) {
const perm = getPermutation(s[unknown].b, or)
const rels = buildRels(perm)
if (isMatch(scanner[known], rels)) {
console.log("match")
let knownPoints: Coord[] = []
for (let j = 0; j < scanner[known].b.length; j++) {
const knownRels = scanner[known].b.map(f => singleRel(scanner[known].b[j], f))
for (let k = 0; k < perm.length; k++) {
const unknownRels = perm.map(f => singleRel(perm[k],f))
let count = 0
for (let l = 0; l < knownRels.length; l++) {
for (let m = 0; m < unknownRels.length; m++) {
if (knownRels[l].max == unknownRels[m].max && knownRels[l].min == unknownRels[m].min && knownRels[l].mid == unknownRels[m].mid) count++
//if (knownRels[l].max == unknownRels[m].max && knownRels[l].min == unknownRels[m].min && knownRels[l].mid == unknownRels[m].mid) count++
}
}
//console.log(count)
if (count >= 12) {
knownPoints = [scanner[known].b[j], perm[k]]
break;
}
}
if (knownPoints.length == 2) {
break;
}
}
if (knownPoints.length < 2) continue
scanner[unknown] = {
sc: scanner[known].sc.map((a, ind) => a+knownPoints[0][ind]-knownPoints[1][ind]) as Coord,
b: perm,
...rels
}
break;
}
}
}

const pointSet: Set<string> = new Set()
for (let i = 0; i < s.length; i++) {
scanner[i].b.forEach(j => {
pointSet.add(j.map((a, ind)=>scanner[i].sc[ind]+a).join(","))
})
}

console.log(scanner)
for (let i = 0; i < s.length; i++) {
console.log(scanner[i].sc)
}
console.log("done")
console.log(pointSet.size)
}

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
}

type Coord = [number, number, number]
interface Scanner {
b: Coord[]
mid: number[]
max: number[]
min: number[]
}

function findMatches(s: Scanner[]): [number,number][] {
const pot: [number,number][] = []

for (let i = 0; i < s.length; i++) {
for (let j = i+1; j < s.length; j++) {
if (isMatch(s[i], s[j])) pot.push([i, j])
}
}

return pot
}

function isMatch(a: {mid: number[], min: number[], max: number[]}, b: {mid: number[], min: number[], max: number[]}) {
let count = 0
for (let k = 0; k < a.mid.length; k++) {
for (let l = 0; l < b.mid.length; l++) {
if (a.mid[k] == b.mid[l] && a.min[k] == b.min[l] && a.max[k] == b.max[l]) count++
//if (a.mid[k] == -b.mid[l] && a.min[k] == -b.min[l] && a.max[k] == -b.max[l]) count++
}
}
return count >= 12*12
}

const coordSwitch = [
[0,1,2],[0,2,1],[1,0,2],[1,2,0],[2,0,1],[2,1,0]
]

const sign = [
[1,1,1],[-1,1,1],[1,-1,1],[1,1,-1],[-1,-1,1],[-1,1,-1],[1,-1,-1],[-1,-1,-1]
]

function getPermutation(c: Coord[], num: number): Coord[] {
const s = sign[num % sign.length]
const m = coordSwitch[Math.floor(num/sign.length)]
return c.map(i => {
const temp = i.map((j,ind) => j*s[ind])
return [temp[m[0]], temp[m[1]], temp[m[2]]]
})
}

function buildRels(b: Coord[]) {
const temp = {
max: [] as number[],
min: [] as number[],
mid: [] as number[]
}
for (let j = 0; j < b.length; j++) {
for (let k = 0; k < b.length;k++) {
const t = singleRel(b[j],b[k])
temp.max.push(t.max)
temp.min.push(t.min)
temp.mid.push(t.mid)
}
}
return temp
}

function singleRel(a: Coord, b: Coord) {
const rel = b.map((v,ind) => v-a[ind])
const ma = Math.max(...rel)
const mi = Math.min(...rel)
return {
max: ma,
min: mi,
mid: rel.reduce((a,b)=>a+b,0)-ma-mi
}
}
29 changes: 29 additions & 0 deletions 2021/20/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
22 changes: 22 additions & 0 deletions 2021/20/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export async function taskOne(input: string[]): Promise<void> {
const map = input[0]
let image: Image = {}

function setPixel(x:number,y:number,v:boolean, i: Image) {
if (i[x] == undefined) i[x] = {}
i[x][y] = v
}
function getPixel(x:number,y:number,i: Image) {
if (i[x] == undefined) return false
if (i[x][y] == undefined) return false
return i[x][y]
}

for (let i = 2; i<input.length; i++)
}

type Image: Record<number, Record<number, boolean>>

export async function taskTwo(input: string[]): Promise<void> {
console.log("Unimplemented");
}
29 changes: 29 additions & 0 deletions 2021/21/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { readFile } from "fs/promises";
import { taskOne, taskTwo } from "./task";

async function main() {
let lastArgument = process.argv.pop() as string;
let taskNumber = 1;
let isTest = false;

if (lastArgument === "test") {
isTest = true;
taskNumber = parseInt(process.argv.pop() as string);
} else {
taskNumber = parseInt(lastArgument)
}

const fileToLoad = isTest ? "test.in" : "solve.in";
const fileContents = await readFile(fileToLoad, "utf-8")

const lines = fileContents.split("\n");

if (taskNumber === 1) {
await taskOne(lines);
}
if (taskNumber === 2) {
await taskTwo(lines);
}
}

void main();
64 changes: 64 additions & 0 deletions 2021/21/task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export async function taskOne(input: string[]): Promise<void> {
const pos = input.map(i=>parseInt(i.replace(" ", "").split(":")[1].trim()) - 1)
const score = input.map(i=>0)
let die = 0
let rollCount = 0
function roll() {
die++
rollCount++
const temp = die
die %= 100
return temp
}
let player = 0
while(!score.some(i=>i>=1000)) {
const r = roll()+ roll()+ roll()
pos[player] = (pos[player] + r) % 10
score[player] += pos[player] + 1
player = (player + 1) % pos.length
}
console.log(rollCount*Math.min(...score))
}

export async function taskTwo(input: string[]): Promise<void> {
const pos = input.map(i=>parseInt(i.replace(" ", "").split(":")[1].trim()) - 1)
const dieOccurences = [0, 0, 0, 1, 3, 6, 7, 6, 3, 1]
interface PlayerState {
p: number
s: number
}
type Game = { p:[PlayerState, PlayerState], c:0|1, o: number}
const queue: Game[] = [
{p: [{p:pos[0], s:0}, {p:pos[1], s:0}], c:0, o:1}
]
while(queue.length > 0) {
const q = queue.pop() as Game
if (q.o == 0) continue
if (q.p[0].s >= 21) {
continue
}
if (q.p[1].s >= 21) {
continue
}
for(let [roll,occ] of dieOccurences.entries()) {
const newPos = (q.p[q.c].p + roll) % 10
const newScore = q.p[q.c].s+newPos+1
const newP: PlayerState[] = []
if (q.o == 0) {
newP.push({p:newPos, s:newScore})
newP.push(q.p[1])
} else {
newP.push(q.p[0])
newP.push({p:newPos, s:newScore})
}
queue.push({
p: newP as [PlayerState, PlayerState],
c: (q.c+1)%2 as 0|1,
o: q.o*occ
})
}

}

console.log("done")
}
2 changes: 1 addition & 1 deletion 2021/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 2021
![](https://img.shields.io/badge/stars%20⭐-36-yellow)
![](https://img.shields.io/badge/stars%20⭐-37-yellow)

Puzzles not solved during event.
2 changes: 0 additions & 2 deletions 2023/25/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ export async function taskOne(input: string[]): Promise<void> {
let potential: [string, string][][] = [];
const keys = Object.keys(graph);
const keys2 = keys.reverse();
let i = 0
let maxPos = 200
for (const k1 of keys) {
for (const k2 of keys2) {
if (k1 == k2) continue;
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3><a href="2021/README.md">2021</a></h3>

![](https://img.shields.io/badge/stars%20⭐-36-yellow)
![](https://img.shields.io/badge/stars%20⭐-37-yellow)

## Try it yourself
https://adventofcode.com
Expand Down

0 comments on commit 3e75003

Please sign in to comment.