diff --git a/.gitignore b/.gitignore index 61a48c3d..e3e0dfb9 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,6 @@ # # Creator of AoC tells people not to commit inputs to git. I also cannot # commit problem descriptions to git because of copyright. -/examples/aoc2023/day*/input -/examples/aoc2023/day*/input.txt -/examples/aoc2023/day*/puzzle.md +/examples/aoc*/day*/input +/examples/aoc*/day*/input.txt +/examples/aoc*/day*/puzzle.md diff --git a/compare_compilers.sh b/compare_compilers.sh index ccdd5675..7fe920af 100755 --- a/compare_compilers.sh +++ b/compare_compilers.sh @@ -31,7 +31,7 @@ done if [ ${#files[@]} = 0 ]; then # skip compiler_cli, because it has a race condition when two compilers simultaneously run it # TODO: do not skip Advent Of Code files - files=( $(find stdlib examples tests -name '*.jou' | grep -v aoc2023 | grep -v tests/should_succeed/compiler_cli | grep -v tests/crash | sort) ) + files=( $(find stdlib examples tests -name '*.jou' | grep -v aoc202. | grep -v tests/should_succeed/compiler_cli | grep -v tests/crash | sort) ) fi if [ ${#actions[@]} = 0 ]; then actions=(tokenize parse run) diff --git a/examples/aoc2024/day01/part1.jou b/examples/aoc2024/day01/part1.jou new file mode 100644 index 00000000..3cd813c1 --- /dev/null +++ b/examples/aoc2024/day01/part1.jou @@ -0,0 +1,44 @@ +import "stdlib/io.jou" +import "stdlib/math.jou" + + +def swap(a: int*, b: int*) -> None: + temp = *a + *a = *b + *b = temp + + +def sort(array: int*, length: int) -> None: + # inefficient and dumb sorting algorithm + while length > 0: + smallest = 0 + for i = 1; i < length; i++: + if array[i] < array[smallest]: + smallest = i + swap(&array[0], &array[smallest]) + array++ + length-- + + +def main() -> int: + f = fopen("sampleinput.txt", "r") + assert f != NULL + + left_column: int[2000] + right_column: int[2000] + n = 0 + while fscanf(f, "%d %d", &left_column[n], &right_column[n]) == 2: + n++ + assert n < 2000 + + fclose(f) + + sort(left_column, n) + sort(right_column, n) + + result: long = 0 + for i = 0; i < n; i++: + result += abs(left_column[i] - right_column[i]) + printf("%lld\n", result) # Output: 11 + + return 0 diff --git a/examples/aoc2024/day01/part2.jou b/examples/aoc2024/day01/part2.jou new file mode 100644 index 00000000..2ad991f7 --- /dev/null +++ b/examples/aoc2024/day01/part2.jou @@ -0,0 +1,28 @@ +import "stdlib/io.jou" +import "stdlib/math.jou" + + +def main() -> int: + f = fopen("input", "r") + assert f != NULL + + left_column: int[2000] + right_column: int[2000] + n = 0 + while fscanf(f, "%d %d", &left_column[n], &right_column[n]) == 2: + n++ + assert n < 2000 + + fclose(f) + + result: long = 0 + for i = 0; i < n; i++: + count = 0 + for k = 0; k < n; k++: + if left_column[i] == right_column[k]: + count++ + result += count * left_column[i] + + printf("%lld\n", result) # Output: 31 + + return 0 diff --git a/examples/aoc2024/day01/sampleinput.txt b/examples/aoc2024/day01/sampleinput.txt new file mode 100644 index 00000000..b8af9ad2 --- /dev/null +++ b/examples/aoc2024/day01/sampleinput.txt @@ -0,0 +1,6 @@ +3 4 +4 3 +2 5 +1 3 +3 9 +3 3 diff --git a/runtests.sh b/runtests.sh index e711ba48..fa76b3c1 100755 --- a/runtests.sh +++ b/runtests.sh @@ -236,7 +236,7 @@ function run_test() counter=0 skipped=0 -for joufile in examples/*.jou examples/aoc2023/day*/part*.jou tests/*/*.jou; do +for joufile in examples/*.jou examples/aoc*/day*/part*.jou tests/*/*.jou; do if ! [[ $joufile == *"$file_filter"* ]]; then # Skip silently, without showing that this is skipped. # This produces less noisy output when you select only a few tests.