Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli authored Dec 26, 2023
1 parent b861399 commit b390718
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def main() -> int:
See the [examples](./examples/) and [tests](./tests/) directories for more example programs
or read [the Jou tutorial](./doc/tutorial.md).

So far, Jou is usable enough for writing small programs.
So far, Jou is usable for writing small programs that don't have a lot of dependencies.
For example, I solved all problems of [Advent of Code 2023](https://adventofcode.com/2023/) in Jou.
See [examples/aoc2023](./examples/aoc2023/) for the code.

Expand Down
2 changes: 1 addition & 1 deletion examples/aoc2023/day24/bigint.jou
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def bigdivmod(x: BigInt, y: BigInt) -> BigInt[2]:
if bigsign(x) < 0:
remainder = bigneg(remainder)

# When nonzero remainder, force its sign to be same as sign of y, similar to jou %
# When nonzero remainder, force its sign to be same sign as y, similar to jou %
if bigsign(remainder) != 0 and bigsign(remainder) != bigsign(y):
remainder = bigadd(remainder, y)
quotient = bigsub(quotient, bigint(1))
Expand Down
9 changes: 4 additions & 5 deletions examples/aoc2023/day24/part2.jou
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class EquationSolver:
return True


# Equations list should be terminated by an equation with 0 variables.
def solve_linear_system_of_equations(eqs: Equation*, neqs: int) -> BigInt*:
nvariables = 0
for i = 0; i < neqs; i++:
Expand All @@ -102,7 +101,7 @@ def solve_linear_system_of_equations(eqs: Equation*, neqs: int) -> BigInt*:
memcpy(eq->coeffs, eqs[i].coeffs, sizeof(eqs[i].coeffs[0]) * eqs[i].nvariables)
eq->rhs = eqs[i].rhs

# Each equation only has one unknown variable + variables from equations after it
# Each final equation only has one unknown variable + variables from equations after it
final_equations: Equation* = malloc(sizeof(final_equations[0]) * nvariables)

for var = 0; var < nvariables; var++:
Expand Down Expand Up @@ -206,13 +205,13 @@ def get_paired_index(N: int, small: int, big: int) -> int:
# p1 = project location of point i at time t_i to the plane
# p2 = project location of point j at time t_j to the plane
# p3 = project location of point k at time t_k to the plane
# the equation says that p1,p2,p3 go along the same line.
# there is an equation which says that p1,p2,p3 go along the same line.
#
# To test if 2D points go along the same line, we check if their 2x2 determinant is zero.
# Unfortunately the 2x2 determinant creates products like t_i*t_j into the equations.
# We just replace each of these products with a new dummy variable:
# t_1*t_2 becomes t_(N+1), t_1*t_3 becomes t_(N+2) and so on.
# Even with that, the resulting equations contain enough info to solve this.
# Even with that, the resulting equations contain enough info to be solved.
def create_equations(moving_points: MovingPoint*, N: int, neqs: int*) -> Equation*:
nvariables = N + N*(N-1)/2 # N normal variables, (n choose 2) paired variables

Expand Down Expand Up @@ -261,7 +260,7 @@ def create_equations(moving_points: MovingPoint*, N: int, neqs: int*) -> Equatio
#
# With sympy (Python library), I expanded this to:
#
# -six*sjy + six*sky - six*tj*vjy + six*tk*vky + siy*sjx - siy*skx + siy*tj*vjx - siy*tk*vkx - sjx*sky + sjx*ti*viy - sjx*tk*vky + sjy*skx - sjy*ti*vix + sjy*tk*vkx - skx*ti*viy + skx*tj*vjy + sky*ti*vix - sky*tj*vjx - ti*tj*vix*vjy + ti*tj*viy*vjx + ti*tk*vix*vky - ti*tk*viy*vkx - tj*tk*vjx*vky + tj*tk*vjy*vkx
# -six*sjy + six*sky - six*t_j*vjy + six*t_k*vky + siy*sjx - siy*skx + siy*t_j*vjx - siy*t_k*vkx - sjx*sky + sjx*t_i*viy - sjx*t_k*vky + sjy*skx - sjy*t_i*vix + sjy*t_k*vkx - skx*t_i*viy + skx*t_j*vjy + sky*t_i*vix - sky*t_j*vjx - t_i*t_j*vix*vjy + t_i*t_j*viy*vjx + t_i*t_k*vix*vky - t_i*t_k*viy*vkx - t_j*t_k*vjx*vky + t_j*t_k*vjy*vkx
eq->coeffs[i] = bigadd4(bigmul(sjx, viy), bigneg(bigmul(sjy, vix)), bigneg(bigmul(skx, viy)), bigmul(sky, vix))
eq->coeffs[j] = bigadd4(bigmul(siy, vjx), bigneg(bigmul(six, vjy)), bigmul(skx, vjy), bigneg(bigmul(sky, vjx)))
eq->coeffs[k] = bigadd4(bigmul(six, vky), bigneg(bigmul(siy, vkx)), bigneg(bigmul(sjx, vky)), bigmul(sjy, vkx))
Expand Down
9 changes: 4 additions & 5 deletions examples/aoc2023/day25/part1.jou
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class Graph:

fprintf(f, "}\n")
fclose(f)
# system("dot -T png /tmp/aoc25-graph.txt -o /tmp/aoc25-graph.png && open /tmp/aoc25-graph.png")
system("dot -T png /tmp/aoc25-graph.txt -o /tmp/aoc25-graph.png && eom /tmp/aoc25-graph.png")
system("dot -T png /tmp/aoc25-graph.txt -o /tmp/aoc25-graph.png && open /tmp/aoc25-graph.png")

def connect(self, a: int, b: int) -> None:
assert 0 <= a and a < self->num_nodes
Expand Down Expand Up @@ -100,7 +99,8 @@ class Graph:

return result

# Returns shortest path. Result always starts with a and ends with b. No other termination.
# Returns shortest path, or NULL if there is no path.
# Returned path always starts with a and ends with b. There is no -1 termination or similar.
def dijkstra_algorithm(self, a: int, b: int) -> int*:
assert a != b

Expand Down Expand Up @@ -160,7 +160,7 @@ class Graph:
free(nodes_with_distance_set)
free(known_shortest)

return realloc(result, (p as long) - (result as long))
return result

# If this returns True, the nodes will always be in the same region after breaking 3 connections.
def nodes_are_strongly_connected(self, a: int, b: int) -> bool:
Expand Down Expand Up @@ -273,7 +273,6 @@ def main() -> int:

graph.merge_nodes(nodes[0], nodes[1])
# printf("Merged %d and %d, now there are %d nodes\n", nodes[0], nodes[1], graph.num_nodes)
# fflush(stdout)

#graph.visualize_with_graphviz()

Expand Down

0 comments on commit b390718

Please sign in to comment.