Skip to content

Commit

Permalink
aoc day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Dec 6, 2023
1 parent 36041c2 commit 54540fb
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ jou_compiled
# These are large text files, and each AoC user gets different input files.
# Tests use sampleinput.txt files, copied from problem descriptions
/examples/aoc2023/day*/input.txt

# files created by https://github.com/scarvalhojr/aoc-cli
# not sure if I can commit these to git because copyright
/examples/aoc2023/day*/puzzle.md
66 changes: 66 additions & 0 deletions examples/aoc2023/day06/part1.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import "stdlib/str.jou"
import "stdlib/ascii.jou"
import "stdlib/io.jou"
import "stdlib/math.jou"


# Unlike most other AoC problems, this one is mostly math.
#
# Let T = given amount of time (button + move)
# R = record distance
# t = time button was held down = speed of boat
#
# Then getting a record means that
#
# t*(T-t) > R
#
# where t*(T-t) calculates our distance as speed*time.
# This can be rearranged to
#
# t^2 - Tt + R < 0
#
# and then solved with the quadratic equation:
#
#
# -b +- sqrt(b^2-4ac) T +- sqrt(T^2-4R)
# tmin,tmax = --------------------- = -------------------
# 2a 2
#
# Then we calculate how many integers are strictly between tmin and tmax.

def main() -> int:
times = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]
distances = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1]

f = fopen("input.txt", "r")
assert f != NULL

line: byte[100]
while fgets(line, sizeof(line) as int, f) != NULL:
words = split_by_ascii_whitespace(line)

assert words[0] != NULL
if strcmp(words[0], "Time:") == 0:
dest = &times
else:
dest = &distances

for i = 0; words[i+1] != NULL; i++:
assert i < sizeof(*dest)/sizeof((*dest)[0]) - 1
(*dest)[i] = atoi(words[i+1])
fclose(f)

result = 1
for i = 0; times[i] != -1 and distances[i] != -1; i++:
T = times[i]
R = distances[i]

tmin = (T - sqrt(T*T - 4*R))/2
tmax = (T + sqrt(T*T - 4*R))/2
ints_between = (ceil(tmax) as int) - (floor(tmin) as int) - 1

assert ints_between >= 1 # must be possible to win somehow
result *= ints_between

printf("%d\n", result) # Output: 288
return 0
40 changes: 40 additions & 0 deletions examples/aoc2023/day06/part2.jou
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import "stdlib/str.jou"
import "stdlib/mem.jou"
import "stdlib/ascii.jou"
import "stdlib/io.jou"
import "stdlib/math.jou"


def remove_all_whitespace(s: byte*) -> void:
while *s != '\0':
if is_ascii_whitespace(*s):
memmove(s, &s[1], strlen(s))
else:
s++


def main() -> int:
T = -1 as long
R = -1 as long

f = fopen("sampleinput.txt", "r")
assert f != NULL

line: byte[100]
while fgets(line, sizeof(line) as int, f) != NULL:
remove_all_whitespace(line)
if starts_with(line, "Time:"):
T = atoll(&line[5])
elif starts_with(line, "Distance:"):
R = atoll(&line[9])
else:
assert False

fclose(f)

tmin = (T - sqrt(T*T - 4*R))/2
tmax = (T + sqrt(T*T - 4*R))/2
ints_between = (ceil(tmax) as int) - (floor(tmin) as int) - 1

printf("%d\n", ints_between) # Output: 71503
return 0
2 changes: 2 additions & 0 deletions examples/aoc2023/day06/sampleinput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

0 comments on commit 54540fb

Please sign in to comment.