Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add min/max functions to mathlib #466

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions examples/aoc2023/day02/part2.jou
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import "stdlib/str.jou"
import "stdlib/io.jou"
import "stdlib/ascii.jou"
import "stdlib/math.jou"


class Game:
Expand All @@ -13,12 +14,12 @@ class Game:
color: byte[10]
assert sscanf(text, "%d %9s", &n, color) == 2

if strcmp(color, "red") == 0 and n > self->red:
self->red = n
if strcmp(color, "green") == 0 and n > self->green:
self->green = n
if strcmp(color, "blue") == 0 and n > self->blue:
self->blue = n
if strcmp(color, "red") == 0:
self->red = max(self->red, n)
if strcmp(color, "green") == 0:
self->green = max(self->green, n)
if strcmp(color, "blue") == 0:
self->blue = max(self->blue, n)

def get_power(self) -> int:
return self->red * self->green * self->blue
Expand Down
6 changes: 3 additions & 3 deletions examples/aoc2023/day07/part2.jou
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import "stdlib/io.jou"
import "stdlib/str.jou"
import "stdlib/mem.jou"
import "stdlib/math.jou"


class Hand:
letters: byte[6]
Expand Down Expand Up @@ -114,9 +116,7 @@ class Hand:

best = -1
for i = 0; i < nvariations; i++:
value = variations[i].hand_type_strength_without_J_special_casing()
if value > best:
best = value
best = max(best, variations[i].hand_type_strength_without_J_special_casing())

free(variations)
self->cached_hand_type_strength = best
Expand Down
16 changes: 4 additions & 12 deletions examples/aoc2023/day16/part2.jou
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,12 @@ def main() -> int:
best = 0

for x = 0; x < grid.width; x++:
n = run_beam(&grid, LightBeam{location = [x, 0], direction = [0, 1]})
if n > best:
best = n
n = run_beam(&grid, LightBeam{location = [x, grid.height - 1], direction = [0, -1]})
if n > best:
best = n
best = max(best, run_beam(&grid, LightBeam{location = [x, 0], direction = [0, 1]}))
best = max(best, run_beam(&grid, LightBeam{location = [x, grid.height - 1], direction = [0, -1]}))

for y = 0; y < grid.height; y++:
n = run_beam(&grid, LightBeam{location = [0, y], direction = [1, 0]})
if n > best:
best = n
n = run_beam(&grid, LightBeam{location = [grid.width - 1, y], direction = [-1, 0]})
if n > best:
best = n
best = max(best, run_beam(&grid, LightBeam{location = [0, y], direction = [1, 0]}))
best = max(best, run_beam(&grid, LightBeam{location = [grid.width - 1, y], direction = [-1, 0]}))

printf("%d\n", best) # Output: 51
free(grid.data)
Expand Down
7 changes: 1 addition & 6 deletions examples/aoc2023/day17/part1.jou
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "stdlib/io.jou"
import "stdlib/mem.jou"
import "stdlib/math.jou"
import "../grid.jou"


Expand Down Expand Up @@ -60,12 +61,6 @@ class Node:
++*nneighbors


def min(x: int, y: int) -> int:
if x < y:
return x
return y


def dijkstra_algorithm(grid: Grid*, start1: Node, start2: Node, goal1: Node, goal2: Node) -> int:
assert grid->is_in_bounds(goal1.location)
assert grid->is_in_bounds(goal2.location)
Expand Down
7 changes: 1 addition & 6 deletions examples/aoc2023/day17/part2.jou
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "stdlib/io.jou"
import "stdlib/mem.jou"
import "stdlib/math.jou"
import "../grid.jou"


Expand Down Expand Up @@ -61,12 +62,6 @@ class Node:
++*nneighbors


def min(x: int, y: int) -> int:
if x < y:
return x
return y


def dijkstra_algorithm(grid: Grid*, start1: Node, start2: Node, goal1: Node, goal2: Node) -> int:
assert grid->is_in_bounds(goal1.location)
assert grid->is_in_bounds(goal2.location)
Expand Down
27 changes: 26 additions & 1 deletion stdlib/math.jou
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
# Math lib
# Some math functions

# Other functions
# Choose bigger/smaller
def min(a: int, b: int) -> int:
if a < b:
return a
else:
return b
def max(a: int, b: int) -> int:
if a > b:
return a
else:
return b

declare fmin(a: double, b: double) -> double
declare fmax(a: double, b: double) -> double

def llmin(a: long, b: long) -> long:
if a < b:
return a
else:
return b
def llmax(a: long, b: long) -> long:
if a > b:
return a
else:
return b

# Returns the absolute value of x: |x|.
declare abs(x: int) -> int
declare fabs(x: double) -> double
Expand Down
8 changes: 8 additions & 0 deletions tests/should_succeed/mathlibtest.jou
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import "stdlib/math.jou"
import "stdlib/io.jou"

def main() -> int:
printf("%d\n", min(12, 34)) # Output: 12
printf("%lld\n", llmin(1000000000000L, 2000000000000L)) # Output: 1000000000000
printf("%f\n", fmin(1.2, 3.4)) # Output: 1.200000

printf("%d\n", max(12, 34)) # Output: 34
printf("%lld\n", llmax(1000000000000L, 2000000000000L)) # Output: 2000000000000
printf("%f\n", fmax(1.2, 3.4)) # Output: 3.400000

printf("%d\n", abs(-1)) # Output: 1
printf("%lld\n", llabs(-1145141919180L)) # Output: 1145141919180
printf("%f\n", fabs(-114.514)) # Output: 114.514000
Expand Down