diff --git a/README.md b/README.md index 1e757bbe..9b97068e 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/aoc2024/day25/part1.jou b/examples/aoc2024/day25/part1.jou new file mode 100644 index 00000000..3ffd4cda --- /dev/null +++ b/examples/aoc2024/day25/part1.jou @@ -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 diff --git a/examples/aoc2024/day25/sampleinput.txt b/examples/aoc2024/day25/sampleinput.txt new file mode 100644 index 00000000..8e298551 --- /dev/null +++ b/examples/aoc2024/day25/sampleinput.txt @@ -0,0 +1,39 @@ +##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +#####