Skip to content

Commit

Permalink
day11 part1
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 11, 2024
1 parent 72d4e16 commit b9a8886
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
73 changes: 73 additions & 0 deletions examples/aoc2024/day11/part1.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import "stdlib/ascii.jou"
import "stdlib/io.jou"
import "stdlib/mem.jou"
import "stdlib/str.jou"


# count_digits(999) = 3
# count_digits(1000) = 4
def count_digits(n: long) -> int:
result = 0
while n > 0:
n /= 10
result++
return result


# split_digits(1234) == [12, 34]
def split_digits(n: long) -> long[2]:
length = count_digits(n)
assert length % 2 == 0

power_of_10 = 1
for i = 0; i < length/2; i++:
power_of_10 *= 10

return [n / power_of_10, n % power_of_10]


# Returns new number of stones
def blink(stones: long*, nstones: int) -> int:
old_nstones = nstones

for i = 0; i < old_nstones; i++:
if stones[i] == 0:
stones[i] = 1
elif count_digits(stones[i]) % 2 == 0:
pair = split_digits(stones[i])
stones[i] = pair[0]
stones[nstones++] = pair[1]
else:
stones[i] *= 2024

return nstones


def main() -> int:
f = fopen("sampleinput.txt", "r")
assert f != NULL
line: byte[100] = ""
fgets(line, sizeof(line) as int, f)
fclose(f)

words = split_by_ascii_whitespace(line)

nstones = 0
while words[nstones] != NULL:
nstones++

stones: long* = malloc(sizeof(stones[0]) * nstones)
assert stones != NULL
for i = 0; i < nstones; i++:
stones[i] = atoll(words[i])

free(words)

for i = 0; i < 25; i++:
# Make sure there's enough room even if every stone splits.
stones = realloc(stones, sizeof(stones[0]) * (2 * nstones))
assert stones != NULL
nstones = blink(stones, nstones)
printf("%d\n", nstones) # Output: 55312

return 0
1 change: 1 addition & 0 deletions examples/aoc2024/day11/sampleinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
125 17

0 comments on commit b9a8886

Please sign in to comment.