Skip to content

Commit

Permalink
AOC 2024 day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 1, 2024
1 parent 30131bc commit c243f66
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion compare_compilers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions examples/aoc2024/day01/part1.jou
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions examples/aoc2024/day01/part2.jou
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions examples/aoc2024/day01/sampleinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3
2 changes: 1 addition & 1 deletion runtests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit c243f66

Please sign in to comment.