Skip to content

Commit

Permalink
finished 2023
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr0nox committed Dec 26, 2023
1 parent 3e75003 commit 351fef5
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 106 deletions.
27 changes: 27 additions & 0 deletions 2023/24/runner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
from task import task1, task2
import sys

def parse_input(input_file_path: str) -> list[str]:
file = open(input_file_path)
raw_lines = file.readlines()
file.close()
lines = []
for line in raw_lines:
lines.append(line.rstrip("\n"))
return lines

if __name__ == "__main__":
if len(sys.argv) <= 2 or sys.argv[2] == "main":
input_file = os.path.dirname(__file__) + "/solve.in"
elif sys.argv[2] == "test":
input_file = os.path.dirname(__file__) + "/test.in"
if not os.path.exists(input_file):
print("Test file doesn't exist!")
exit(1)
lines = parse_input(input_file)

if len(sys.argv) <= 1 or sys.argv[1] == "1":
task1(lines)
if len(sys.argv) <= 1 or sys.argv[1] == "2":
task2(lines)
26 changes: 26 additions & 0 deletions 2023/24/task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import re
from sympy import Symbol
from sympy import solve_poly_system


def task1(input_lines: list[str]):
print("Unimplemented")

def task2(input_lines: list[str]):
lines = list(map(lambda i: list(map(int, i)), list(map(lambda i: re.match("(-?[0-9]+), (-?[0-9]+), (-?[0-9]+) @ (-?[0-9]+), (-?[0-9]+), (-?[0-9]+)", i).groups(), input_lines[:3]))))
px = Symbol('px')
py = Symbol('py')
pz = Symbol('pz')
vx = Symbol('vx')
vy = Symbol('vy')
vz = Symbol('vz')
ts = [Symbol('t'+str(i)) for i in range(3)]
equations = []

for i, l in enumerate(lines):
equations.append(px + vx * ts[i] - l[0] - l[3] * ts[i])
equations.append(py + vy * ts[i] - l[1] - l[4] * ts[i])
equations.append(pz + vz * ts[i] - l[2] - l[5] * ts[i])

result = solve_poly_system(equations, px, py, pz, vx, vy, vz, *ts)
print(result[0][0] + result[0][1] + result[0][2])
105 changes: 1 addition & 104 deletions 2023/24/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,12 @@ export async function taskOne(input: string[]): Promise<void> {
}

export async function taskTwo(input: string[]): Promise<void> {
const lines = input.map(i => i.replace(" ", "").split("@").map(i => parseNumberList(i) as ThreeVector) as ThreeLine)

console.log(getPossible(lines, 0, 1))
//console.log(getPossible(lines, 1, 2))

console.log("Implemented in python")
}

type TwoVector = [number, number]
type TwoLine = [TwoVector, TwoVector]

type ThreeVector = [number, number, number]
type ThreeLine = [ThreeVector, ThreeVector]

function intersect2(_a: TwoLine, _b: TwoLine): TwoVector|undefined {
const [a, b] = _a
Expand All @@ -58,101 +52,4 @@ function twoIntersect(_a: TwoLine, _b: TwoLine): [number, number]|undefined {
if (!isNaN(t) && isFinite(t) && !isNaN(r) && isFinite(r))
return [t, r]
return undefined
}

function getPossible(lines: ThreeLine[], ind1: number, ind2: number) {
const possible: TwoVector[] = []
/*for (let vr = -1000; vr < 1000; vr++) {
let foundPossible = false
for (let p0 = 0; p0 < lines.length; p0++) {
let possible = true;
//console.log(" ")
for (let i = 0; i < 10; i++) {
if (i == p0) continue
const t = (lines[p0][0][ind] + 962702686958 * lines[p0][1][ind] - lines[i][0][ind]-962702686958*vr)/(lines[i][1][ind]-vr)
if (isNaN(t) || !isFinite(t)) continue
//console.log(t)
const diff = Math.abs(t - Math.round(t))
if (diff > 0.001) {
possible = false
break
}
}
if (possible) {
foundPossible = true
break
}
}
if (foundPossible) {
possible.push(vr)
}
}*/
const p0 = lines[0]
const p1 = lines[1]

const limit = 350

for (let vrX = -limit; vrX < limit; vrX++) {
for (let vrY = -limit; vrY < limit; vrY++) {
//let vrX = -330
//let vrY = 63
const _a = [[p0[0][ind1], p0[0][ind2]], [p0[1][ind1]-p1[1][ind1], p0[1][ind2]-p1[1][ind2]]] as TwoLine
const _b = [[p1[0][ind1], p1[0][ind2]], [p1[1][ind1]-vrX, p1[1][ind2]-vrY]] as TwoLine
const int = twoIntersect(_a, _b)
if (!int) {
continue
}
let [r,t] = int
const diffT = Math.abs(t-Math.round(t))
const diffR = Math.abs(r-Math.round(r))
if (diffT > 0.01 || diffR > 0.01) {
continue
}
r = Math.round(r)
t = Math.round(t)
//console.log(t, r,vrX, vrY)
let isPossible = true
const origin = p0[0].map((i, ind)=> i + r*p0[1][ind])
for (let i = 2; i < lines.length; i++) {
//const pI = lines[i][0].map((i, ind)=> i + r*lines[i][1][ind])
if (lines[i][1][0] - vrX < 0.01 || lines[i][1][1] - vrY < 0.01) {
isPossible = false
break
}
}
if (isPossible) {
console.log(vrX, vrY)
}
//for (let j = 0; j < lines.length; j++) {
// let j = 20
//const p0 = lines[j]
/*for (let i = 0; i < lines.length; i++) {
if (i == j) continue
const _a = [[p0[0][ind1], p0[0][ind2]], [p0[1][ind1]-lines[i][1][ind1], p0[1][ind2]-lines[i][1][ind2]]] as TwoLine
const _b = [[lines[i][0][ind1], lines[i][0][ind2]], [lines[i][1][ind1]-vrX, lines[i][1][ind2]-vrY]] as TwoLine
const int = twoIntersect(_a, _b)
if (!int) {
isPossible = false
console.log(_a, _b)
break
}
const [t,r] = int
const diffT = Math.abs(t-Math.floor(t))
const diffR = Math.abs(r-Math.floor(r))
if (diffT > 0.001 || diffR > 0.001) {
isPossible = false
break
}
}
//if(isPossible) break
//}
if (isPossible) {
possible.push([vrX,vrY])
}*/
}
}
return possible
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,

<h3>2023</h3>

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

|Day|Language|
|--|--|
Expand Down Expand Up @@ -37,7 +37,7 @@ Whatever I feel like today. But it will most likely either be Java, Python, C++,
|21|TypeScript|
|22|TypeScript|
|23|TypeScript|
|24|TypeScript|
|24|Part 1: TypeScript, Part 2: Python|
|25|TypeScript|

<h3><a href="2022/README.md">2022</a></h3>
Expand Down

0 comments on commit 351fef5

Please sign in to comment.