From 06c1cf2d6330bb36d0c1ceb3fb937bf565177d43 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 19 Dec 2023 12:55:28 +0200 Subject: [PATCH 1/2] Add min/max functions to mathlib --- examples/aoc2023/day02/part2.jou | 13 ++++++----- examples/aoc2023/day07/part2.jou | 6 ++--- examples/aoc2023/day16/part2.jou | 16 ++++--------- examples/aoc2023/day17/part1.jou | 7 +----- examples/aoc2023/day17/part2.jou | 7 +----- stdlib/math.jou | 35 +++++++++++++++++++++++++++- tests/should_succeed/mathlibtest.jou | 8 +++++++ 7 files changed, 58 insertions(+), 34 deletions(-) diff --git a/examples/aoc2023/day02/part2.jou b/examples/aoc2023/day02/part2.jou index b8a209fa..e8f46e75 100644 --- a/examples/aoc2023/day02/part2.jou +++ b/examples/aoc2023/day02/part2.jou @@ -1,6 +1,7 @@ import "stdlib/str.jou" import "stdlib/io.jou" import "stdlib/ascii.jou" +import "stdlib/math.jou" class Game: @@ -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 diff --git a/examples/aoc2023/day07/part2.jou b/examples/aoc2023/day07/part2.jou index 68986e68..205edb6c 100644 --- a/examples/aoc2023/day07/part2.jou +++ b/examples/aoc2023/day07/part2.jou @@ -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] @@ -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 diff --git a/examples/aoc2023/day16/part2.jou b/examples/aoc2023/day16/part2.jou index 847fcb29..62d9be95 100644 --- a/examples/aoc2023/day16/part2.jou +++ b/examples/aoc2023/day16/part2.jou @@ -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) diff --git a/examples/aoc2023/day17/part1.jou b/examples/aoc2023/day17/part1.jou index 613d824b..06b8cba1 100644 --- a/examples/aoc2023/day17/part1.jou +++ b/examples/aoc2023/day17/part1.jou @@ -1,5 +1,6 @@ import "stdlib/io.jou" import "stdlib/mem.jou" +import "stdlib/math.jou" import "../grid.jou" @@ -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) diff --git a/examples/aoc2023/day17/part2.jou b/examples/aoc2023/day17/part2.jou index ae537329..3ccd2056 100644 --- a/examples/aoc2023/day17/part2.jou +++ b/examples/aoc2023/day17/part2.jou @@ -1,5 +1,6 @@ import "stdlib/io.jou" import "stdlib/mem.jou" +import "stdlib/math.jou" import "../grid.jou" @@ -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) diff --git a/stdlib/math.jou b/stdlib/math.jou index fdc2a4b1..999e96c3 100644 --- a/stdlib/math.jou +++ b/stdlib/math.jou @@ -1,7 +1,40 @@ # 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 + +def fmin(a: double, b: double) -> double: + if a < b: + return a + else: + return b +def fmax(a: double, b: double) -> double: + if a > b: + return a + else: + return b + +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 diff --git a/tests/should_succeed/mathlibtest.jou b/tests/should_succeed/mathlibtest.jou index b28f6a2a..3c95d158 100644 --- a/tests/should_succeed/mathlibtest.jou +++ b/tests/should_succeed/mathlibtest.jou @@ -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 From 6dfc5fedfe70f0fa090d0634efa86979094f0ea5 Mon Sep 17 00:00:00 2001 From: Akuli Date: Tue, 19 Dec 2023 12:59:08 +0200 Subject: [PATCH 2/2] Use "declare" for fmin/fmax --- stdlib/math.jou | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/stdlib/math.jou b/stdlib/math.jou index 999e96c3..097860c8 100644 --- a/stdlib/math.jou +++ b/stdlib/math.jou @@ -13,16 +13,8 @@ def max(a: int, b: int) -> int: else: return b -def fmin(a: double, b: double) -> double: - if a < b: - return a - else: - return b -def fmax(a: double, b: double) -> double: - 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: