-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
125 17 |