From 08ff58deaf6a3e127fd8fc5530e2f900192719c5 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 5 Jan 2025 11:21:09 +0200 Subject: [PATCH 1/3] day25 part1 --- examples/aoc2024/day25/part1.jou | 69 ++++++++++++++++++++++++++ examples/aoc2024/day25/sampleinput.txt | 39 +++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 examples/aoc2024/day25/part1.jou create mode 100644 examples/aoc2024/day25/sampleinput.txt diff --git a/examples/aoc2024/day25/part1.jou b/examples/aoc2024/day25/part1.jou new file mode 100644 index 00000000..0cf4800b --- /dev/null +++ b/examples/aoc2024/day25/part1.jou @@ -0,0 +1,69 @@ +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]): + printf( + "key %d,%d,%d,%d,%d fits lock %d,%d,%d,%d,%d\n", + keys[ik][0], + keys[ik][1], + keys[ik][2], + keys[ik][3], + keys[ik][4], + locks[il][0], + locks[il][1], + locks[il][2], + locks[il][3], + locks[il][4], + ) + 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 @@ +##### +.#### +.#### +.#### +.#.#. +.#... +..... + +##### +##.## +.#.## +...## +...#. +...#. +..... + +..... +#.... +#.... +#...# +#.#.# +#.### +##### + +..... +..... +#.#.. +###.. +###.# +###.# +##### + +..... +..... +..... +#.... +#.#.. +#.#.# +##### From 898b2600638b4543f867319ab9e0688efe041b8e Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 5 Jan 2025 11:23:19 +0200 Subject: [PATCH 2/3] tidy --- examples/aoc2024/day25/part1.jou | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/examples/aoc2024/day25/part1.jou b/examples/aoc2024/day25/part1.jou index 0cf4800b..3ffd4cda 100644 --- a/examples/aoc2024/day25/part1.jou +++ b/examples/aoc2024/day25/part1.jou @@ -50,19 +50,6 @@ def main() -> int: for il = 0; il < nlocks; il++: for ik = 0; ik < nkeys; ik++: if fits(keys[ik], locks[il]): - printf( - "key %d,%d,%d,%d,%d fits lock %d,%d,%d,%d,%d\n", - keys[ik][0], - keys[ik][1], - keys[ik][2], - keys[ik][3], - keys[ik][4], - locks[il][0], - locks[il][1], - locks[il][2], - locks[il][3], - locks[il][4], - ) result++ printf("%d\n", result) # Output: 3 From 5475555c77ce46a750940925792f667fe20d540c Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 5 Jan 2025 11:25:14 +0200 Subject: [PATCH 3/3] update readme --- README.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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