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

AoC 2024: day 25 #547

Merged
merged 3 commits into from
Jan 5, 2025
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
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ See the [examples](./examples/) and [tests](./tests/) directories for more examp
or read [the Jou tutorial](./doc/tutorial.md).

For now, Jou is great 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,
and I'm currently working on Advent of Code 2024.
See [examples/aoc2023](./examples/aoc2023/)
and [examples/aoc2024](./examples/aoc2024/) for the code.
For example, I solved all problems of
[Advent of Code 2023](https://adventofcode.com/2023/) and
[Advent of Code 2024](https://adventofcode.com/2024/) with Jou. See
[examples/aoc2023](./examples/aoc2023/) and
[examples/aoc2024](./examples/aoc2024/) for the code.

I think Jou will be useful for two kinds of people:
- People who find C programming fun but like Python's syntax
Expand Down
56 changes: 56 additions & 0 deletions examples/aoc2024/day25/part1.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import "stdlib/io.jou"
import "stdlib/mem.jou"
import "../../aoc2023/grid.jou"


def fits(key: int*, lock: int*) -> bool:
for i = 0; i < 5; i++:
if key[i] + lock[i] > 5:
return False
return True


def main() -> int:
f = fopen("sampleinput.txt", "r")
assert f != NULL

keys: int[5][500]
locks: int[5][500]
nkeys = 0
nlocks = 0

while feof(f) == 0:
grid = read_grid_from_file(f)
assert grid.width == 5
assert grid.height == 7

dest: int*
if grid.get([0, 0]) == '#':
# lock
assert nlocks < sizeof(locks)/sizeof(locks[0])
dest = locks[nlocks++]
else:
# key
assert nkeys < sizeof(keys)/sizeof(keys[0])
dest = keys[nkeys++]

for x = 0; x < 5; x++:
count = -1 # ignore one full row
for y = 0; y < 7; y++:
if grid.get([x, y]) == '#':
count++
assert count >= 0
dest[x] = count

free(grid.data)

fclose(f)

result = 0
for il = 0; il < nlocks; il++:
for ik = 0; ik < nkeys; ik++:
if fits(keys[ik], locks[il]):
result++

printf("%d\n", result) # Output: 3
return 0
39 changes: 39 additions & 0 deletions examples/aoc2024/day25/sampleinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#####
.####
.####
.####
.#.#.
.#...
.....

#####
##.##
.#.##
...##
...#.
...#.
.....

.....
#....
#....
#...#
#.#.#
#.###
#####

.....
.....
#.#..
###..
###.#
###.#
#####

.....
.....
.....
#....
#.#..
#.#.#
#####
Loading