From b697264521edf0c89f3803ab761619c6d8e6b007 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sat, 9 Dec 2023 21:37:05 +0200 Subject: [PATCH 1/9] aoc day 9, surprisingly easy --- examples/aoc2023/day09/part1.jou | 48 ++++++++++++++++++++ examples/aoc2023/day09/part2.jou | 62 ++++++++++++++++++++++++++ examples/aoc2023/day09/sampleinput.txt | 3 ++ 3 files changed, 113 insertions(+) create mode 100644 examples/aoc2023/day09/part1.jou create mode 100644 examples/aoc2023/day09/part2.jou create mode 100644 examples/aoc2023/day09/sampleinput.txt diff --git a/examples/aoc2023/day09/part1.jou b/examples/aoc2023/day09/part1.jou new file mode 100644 index 00000000..93f72e54 --- /dev/null +++ b/examples/aoc2023/day09/part1.jou @@ -0,0 +1,48 @@ +import "stdlib/io.jou" +import "stdlib/ascii.jou" +import "stdlib/str.jou" +import "stdlib/mem.jou" + + +def predict_next(nums: long*, len: int) -> long: + all_zero = True + for i = 0; i < len; i++: + if nums[i] != 0: + all_zero = False + break + + if all_zero: + return 0 + + diffs: long* = malloc(sizeof(diffs[0]) * len) + for i = 1; i < len; i++: + diffs[i-1] = nums[i]-nums[i-1] + + result = nums[len-1] + predict_next(diffs, len-1) + free(diffs) + return result + + +# return value is an array terminated by nums_len=-1 +def main() -> int: + f = fopen("input.txt", "r") + assert f != NULL + + line: byte[1000] + result: long = 0 + + while fgets(line, sizeof(line) as int, f) != NULL: + nums: long[100] + nnums = 0 + parts = split_by_ascii_whitespace(line) + for p = parts; *p != NULL; p++: + assert nnums < sizeof(nums)/sizeof(nums[0]) + nums[nnums++] = atoll(*p) + free(parts) + + result += predict_next(nums, nnums) + + fclose(f) + + printf("%lld\n", result) + return 0 diff --git a/examples/aoc2023/day09/part2.jou b/examples/aoc2023/day09/part2.jou new file mode 100644 index 00000000..7e513bc9 --- /dev/null +++ b/examples/aoc2023/day09/part2.jou @@ -0,0 +1,62 @@ +import "stdlib/io.jou" +import "stdlib/ascii.jou" +import "stdlib/str.jou" +import "stdlib/mem.jou" + + +def predict_next(nums: long*, len: int) -> long: + all_zero = True + for i = 0; i < len; i++: + if nums[i] != 0: + all_zero = False + break + + if all_zero: + return 0 + + diffs: long* = malloc(sizeof(diffs[0]) * len) + for i = 1; i < len; i++: + diffs[i-1] = nums[i]-nums[i-1] + + result = nums[len-1] + predict_next(diffs, len-1) + free(diffs) + return result + + +def swap(a: long*, b: long*) -> None: + tmp = *a + *a = *b + *b = tmp + + +def reverse(nums: long*, len: int) -> None: + p = nums + q = &nums[len-1] + while p < q: + swap(p++, q--) + + +# return value is an array terminated by nums_len=-1 +def main() -> int: + f = fopen("input.txt", "r") + assert f != NULL + + line: byte[1000] + result: long = 0 + + while fgets(line, sizeof(line) as int, f) != NULL: + nums: long[100] + nnums = 0 + parts = split_by_ascii_whitespace(line) + for p = parts; *p != NULL; p++: + assert nnums < sizeof(nums)/sizeof(nums[0]) + nums[nnums++] = atoll(*p) + free(parts) + + reverse(nums, nnums) + result += predict_next(nums, nnums) + + fclose(f) + + printf("%lld\n", result) + return 0 diff --git a/examples/aoc2023/day09/sampleinput.txt b/examples/aoc2023/day09/sampleinput.txt new file mode 100644 index 00000000..539a763f --- /dev/null +++ b/examples/aoc2023/day09/sampleinput.txt @@ -0,0 +1,3 @@ +0 3 6 9 12 15 +1 3 6 10 15 21 +10 13 16 21 30 45 From 3d1603d18046c4cd3094c32ebc1669b4f6ea180d Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 10 Dec 2023 17:35:01 +0200 Subject: [PATCH 2/9] aoc day 10 --- .gitignore | 13 +- examples/aoc2023/day10/part1.jou | 106 +++++++++++++++ examples/aoc2023/day10/part2.jou | 171 ++++++++++++++++++++++++ examples/aoc2023/day10/sampleinput.txt | 5 + examples/aoc2023/day10/sampleinput2.txt | 9 ++ 5 files changed, 298 insertions(+), 6 deletions(-) create mode 100644 examples/aoc2023/day10/part1.jou create mode 100644 examples/aoc2023/day10/part2.jou create mode 100644 examples/aoc2023/day10/sampleinput.txt create mode 100644 examples/aoc2023/day10/sampleinput2.txt diff --git a/.gitignore b/.gitignore index 3454a4b0..81177664 100644 --- a/.gitignore +++ b/.gitignore @@ -25,11 +25,12 @@ # ide stuff /.vscode/ -# Advent of Code input files https://adventofcode.com/ -# These are large text files, and each AoC user gets different input files. -# Tests use sampleinput.txt files, copied from problem descriptions +# Advent of Code files https://adventofcode.com/ +# Inputs are large text files, and each AoC user gets different input files. +# Tests use sampleinput.txt files, copied from problem descriptions. +# +# Creator of AoC tells people not to commit inputs to git. I also cannot +# commit problem descriptions to cit because of copyright. +/examples/aoc2023/day*/input /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 diff --git a/examples/aoc2023/day10/part1.jou b/examples/aoc2023/day10/part1.jou new file mode 100644 index 00000000..11b9d979 --- /dev/null +++ b/examples/aoc2023/day10/part1.jou @@ -0,0 +1,106 @@ +import "stdlib/io.jou" +import "stdlib/mem.jou" +import "stdlib/str.jou" + + +class Input: + width: int + height: int + data: byte* + + def is_in_bounds(self, point: int[2]) -> bool: + x = point[0] + y = point[1] + return 0 <= x and x < self->width and 0 <= y and y < self->height + + def get(self, point: int[2]) -> byte: + assert self->is_in_bounds(point) + x = point[0] + y = point[1] + return self->data[(self->width + 1)*y + x] + + +def find_S(input: Input*) -> int[2]: + for x = 0; x < input->width; x++: + for y = 0; y < input->height; y++: + if input->get([x, y]) == 'S': + return [x, y] + + assert False + + +def get_dirs(c: byte) -> int[2][2]: + if c == '7': + return [[-1, 0], [0, 1]] + if c == 'J': + return [[-1, 0], [0, -1]] + if c == 'L': + return [[1, 0], [0, -1]] + if c == 'F': + return [[1, 0], [0, 1]] + if c == '-': + return [[-1, 0], [1, 0]] + if c == '|': + return [[0, -1], [0, 1]] + + printf("c = '%c'\n", c) + fflush(stdout) + assert False + + +def eq(a: int[2], b: int[2]) -> bool: + return a[0] == b[0] and a[1] == b[1] + + +def find_initial_direction(input: Input*, S: int[2]) -> int[2]: + directions = [[0,1], [0,-1], [1,0], [-1,0]] + for d = &directions[0]; d < &directions[4]; d++: + S_to_neighbor = *d + neighbor_to_S = [-S_to_neighbor[0], -S_to_neighbor[1]] + neighbor = [S[0] + S_to_neighbor[0], S[1] + S_to_neighbor[1]] + + if input->is_in_bounds(neighbor) and input->get(neighbor) != '.': + dirs = get_dirs(input->get(neighbor)) + if eq(dirs[0], S_to_neighbor) or eq(dirs[1], S_to_neighbor): + return S_to_neighbor + + assert False + + +def main() -> int: + max_len = 100000 + input = Input{data = calloc(1, max_len+1)} + + f = fopen("sampleinput.txt", "r") + assert f != NULL + fread(input.data, 1, max_len, f) + fclose(f) + + input.width = strcspn(input.data, "\n") as int + input.height = (strlen(input.data) as int) / (input.width + 1) + + point = find_S(&input) + + # take first step away from S + dir = find_initial_direction(&input, point) + point[0] += dir[0] + point[1] += dir[1] + loop_length = 1 + + while input.get(point) != 'S': + came_from = [-dir[0], -dir[1]] + dirs = get_dirs(input.get(point)) + assert eq(came_from, dirs[0]) or eq(came_from, dirs[1]) + if eq(came_from, dirs[0]): + dir = dirs[1] + else: + dir = dirs[0] + point[0] += dir[0] + point[1] += dir[1] + loop_length++ + + assert loop_length % 2 == 0 + printf("%d\n", loop_length / 2) # Output: 4 + + free(input.data) + return 0 diff --git a/examples/aoc2023/day10/part2.jou b/examples/aoc2023/day10/part2.jou new file mode 100644 index 00000000..2a365d18 --- /dev/null +++ b/examples/aoc2023/day10/part2.jou @@ -0,0 +1,171 @@ +import "stdlib/io.jou" +import "stdlib/mem.jou" +import "stdlib/str.jou" +import "stdlib/math.jou" + + +class Input: + width: int + height: int + data: byte* + + def is_in_bounds(self, point: int[2]) -> bool: + x = point[0] + y = point[1] + return 0 <= x and x < self->width and 0 <= y and y < self->height + + def get(self, point: int[2]) -> byte: + assert self->is_in_bounds(point) + x = point[0] + y = point[1] + return self->data[(self->width + 1)*y + x] + + +def find_S(input: Input*) -> int[2]: + for x = 0; x < input->width; x++: + for y = 0; y < input->height; y++: + if input->get([x, y]) == 'S': + return [x, y] + + assert False + + +def get_dirs(c: byte) -> int[2][2]: + if c == '7': + return [[-1, 0], [0, 1]] + if c == 'J': + return [[-1, 0], [0, -1]] + if c == 'L': + return [[1, 0], [0, -1]] + if c == 'F': + return [[1, 0], [0, 1]] + if c == '-': + return [[-1, 0], [1, 0]] + if c == '|': + return [[0, -1], [0, 1]] + + printf("c = '%c'\n", c) + fflush(stdout) + assert False + + +def eq(a: int[2], b: int[2]) -> bool: + return a[0] == b[0] and a[1] == b[1] + + +def find_initial_direction(input: Input*, S: int[2]) -> int[2]: + directions = [[0,1], [0,-1], [1,0], [-1,0]] + for d = &directions[0]; d < &directions[4]; d++: + S_to_neighbor = *d + neighbor_to_S = [-S_to_neighbor[0], -S_to_neighbor[1]] + neighbor = [S[0] + S_to_neighbor[0], S[1] + S_to_neighbor[1]] + + if input->is_in_bounds(neighbor) and input->get(neighbor) != '.': + dirs = get_dirs(input->get(neighbor)) + if eq(dirs[0], S_to_neighbor) or eq(dirs[1], S_to_neighbor): + return S_to_neighbor + + assert False + + +def polygon_area(corners: int[2]*, ncorners: int) -> int: + # Any polygon area can be thought of as consisting of triangle areas. + # + # Consider a triangle with two adjacent sides as corners: + # + # O corners[i] + # |\ + # | \ + # | \ + # | \ + # | A O corners[i+1] + # | / + # | / + # | / + # |/ + # O + # + # (0,0) + # + # Its area is 1/2 |det(corners[i], corners[i+1])|, where det(a,b) + # denotes a 2x2 determinant with a and b as rows (or columns), and + # | ... | denotes the absolute value. Without the absolute value, + # the sign of a determinant is basically the orientation of the + # triangle. + # + # We can view a polygon's area as consisting of triangles like this. + # As we go around the polygon, the differently oriented triangles + # overlap with opposite orientations outside the polygon, but not + # inside the polygon. So + # + # polygon area = 1/2 |sum(det(corners[i], corners[i+1]))| + double_area = 0 + for i = 0; i < ncorners; i++: + a = corners[i][0] + b = corners[i][1] + c = corners[(i+1) % ncorners][0] + d = corners[(i+1) % ncorners][1] + double_area += a*d - b*c + return abs(double_area)/2 + + +def main() -> int: + max_len = 100000 + input = Input{data = calloc(1, max_len+1)} + + f = fopen("sampleinput2.txt", "r") + assert f != NULL + fread(input.data, 1, max_len, f) + fclose(f) + + input.width = strcspn(input.data, "\n") as int + input.height = (strlen(input.data) as int) / (input.width + 1) + + point = find_S(&input) + + loop: int[2]* = malloc(input.width * input.height * sizeof(loop[0])) + loop[0] = point + + # take first step away from S + dir = find_initial_direction(&input, point) + point[0] += dir[0] + point[1] += dir[1] + loop_length = 1 + + while input.get(point) != 'S': + came_from = [-dir[0], -dir[1]] + dirs = get_dirs(input.get(point)) + assert eq(came_from, dirs[0]) or eq(came_from, dirs[1]) + if eq(came_from, dirs[0]): + dir = dirs[1] + else: + dir = dirs[0] + loop[loop_length++] = point + point[0] += dir[0] + point[1] += dir[1] + + area_along_middle_of_path = polygon_area(loop, loop_length) + + # Suppose the path has: + # n 90 degree turns towards inside of loop [1/4] + # m 90 degree turns towards outside of loop [3/4] + # k straight lines [+1/2] + # + # In square brackets is how much bigger area_along_middle_of_path + # becomes than area outside loop. For example, each straight line + # contributes 1/2 extra. So: + # + # area_along_middle_of_path + # = area_inside_loop + 1/4 n + 3/4 m + 1/2 k + # = area_inside_loop + (n+m+k)/2 - (n-m)/4 + # = area_inside_loop + loop_length/2 - 1 + # + # Here n-m = 4 because the loop goes around a total of 360 degrees + # towards the inside: + # + # 90*n - 90*m = 360 + printf("%d\n", area_along_middle_of_path + 1 - loop_length/2) # Output: 4 + + free(input.data) + free(loop) + return 0 diff --git a/examples/aoc2023/day10/sampleinput.txt b/examples/aoc2023/day10/sampleinput.txt new file mode 100644 index 00000000..7650925b --- /dev/null +++ b/examples/aoc2023/day10/sampleinput.txt @@ -0,0 +1,5 @@ +..... +.S-7. +.|.|. +.L-J. +..... diff --git a/examples/aoc2023/day10/sampleinput2.txt b/examples/aoc2023/day10/sampleinput2.txt new file mode 100644 index 00000000..bd9cdf53 --- /dev/null +++ b/examples/aoc2023/day10/sampleinput2.txt @@ -0,0 +1,9 @@ +........... +.S-------7. +.|F-----7|. +.||.....||. +.||.....||. +.|L-7.F-J|. +.|..|.|..|. +.L--J.L--J. +........... From 3bd6f534193b081d20cf784396677d8eefdd0c03 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 10 Dec 2023 17:37:37 +0200 Subject: [PATCH 3/9] clean up debug print --- examples/aoc2023/day10/part1.jou | 2 -- examples/aoc2023/day10/part2.jou | 2 -- 2 files changed, 4 deletions(-) diff --git a/examples/aoc2023/day10/part1.jou b/examples/aoc2023/day10/part1.jou index 11b9d979..1519355d 100644 --- a/examples/aoc2023/day10/part1.jou +++ b/examples/aoc2023/day10/part1.jou @@ -43,8 +43,6 @@ def get_dirs(c: byte) -> int[2][2]: if c == '|': return [[0, -1], [0, 1]] - printf("c = '%c'\n", c) - fflush(stdout) assert False diff --git a/examples/aoc2023/day10/part2.jou b/examples/aoc2023/day10/part2.jou index 2a365d18..4511e2f9 100644 --- a/examples/aoc2023/day10/part2.jou +++ b/examples/aoc2023/day10/part2.jou @@ -44,8 +44,6 @@ def get_dirs(c: byte) -> int[2][2]: if c == '|': return [[0, -1], [0, 1]] - printf("c = '%c'\n", c) - fflush(stdout) assert False From 951ef6eae61ee9b2a5b628b9cd8ec5be207d54f4 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 10 Dec 2023 17:38:24 +0200 Subject: [PATCH 4/9] fix comment typo --- examples/aoc2023/day10/part2.jou | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/aoc2023/day10/part2.jou b/examples/aoc2023/day10/part2.jou index 4511e2f9..cdae7181 100644 --- a/examples/aoc2023/day10/part2.jou +++ b/examples/aoc2023/day10/part2.jou @@ -150,7 +150,7 @@ def main() -> int: # k straight lines [+1/2] # # In square brackets is how much bigger area_along_middle_of_path - # becomes than area outside loop. For example, each straight line + # becomes than area inside loop. For example, each straight line # contributes 1/2 extra. So: # # area_along_middle_of_path From cb251ff8922816dacee9eb689beb89f67e464be6 Mon Sep 17 00:00:00 2001 From: Akuli Date: Sun, 10 Dec 2023 17:40:57 +0200 Subject: [PATCH 5/9] delete confusing detail --- examples/aoc2023/day10/part2.jou | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/examples/aoc2023/day10/part2.jou b/examples/aoc2023/day10/part2.jou index cdae7181..6346464a 100644 --- a/examples/aoc2023/day10/part2.jou +++ b/examples/aoc2023/day10/part2.jou @@ -159,9 +159,7 @@ def main() -> int: # = area_inside_loop + loop_length/2 - 1 # # Here n-m = 4 because the loop goes around a total of 360 degrees - # towards the inside: - # - # 90*n - 90*m = 360 + # towards the inside. printf("%d\n", area_along_middle_of_path + 1 - loop_length/2) # Output: 4 free(input.data) From 5039bc2123563d42c666d94fc3019d3e6aa49fe1 Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 11 Dec 2023 17:07:04 +0200 Subject: [PATCH 6/9] fix tests --- examples/aoc2023/day09/part1.jou | 4 ++-- examples/aoc2023/day09/part2.jou | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/aoc2023/day09/part1.jou b/examples/aoc2023/day09/part1.jou index 93f72e54..d201dd41 100644 --- a/examples/aoc2023/day09/part1.jou +++ b/examples/aoc2023/day09/part1.jou @@ -25,7 +25,7 @@ def predict_next(nums: long*, len: int) -> long: # return value is an array terminated by nums_len=-1 def main() -> int: - f = fopen("input.txt", "r") + f = fopen("sampleinput.txt", "r") assert f != NULL line: byte[1000] @@ -44,5 +44,5 @@ def main() -> int: fclose(f) - printf("%lld\n", result) + printf("%lld\n", result) # Output: 114 return 0 diff --git a/examples/aoc2023/day09/part2.jou b/examples/aoc2023/day09/part2.jou index 7e513bc9..f3521c2c 100644 --- a/examples/aoc2023/day09/part2.jou +++ b/examples/aoc2023/day09/part2.jou @@ -38,7 +38,7 @@ def reverse(nums: long*, len: int) -> None: # return value is an array terminated by nums_len=-1 def main() -> int: - f = fopen("input.txt", "r") + f = fopen("sampleinput.txt", "r") assert f != NULL line: byte[1000] @@ -58,5 +58,5 @@ def main() -> int: fclose(f) - printf("%lld\n", result) + printf("%lld\n", result) # Output: 2 return 0 From 0d10e42415d7f3f9bdc219cc0bab59508d64e64f Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 11 Dec 2023 18:22:38 +0200 Subject: [PATCH 7/9] day 11 --- examples/aoc2023/day11/part1.jou | 86 ++++++++++++++++++++++++ examples/aoc2023/day11/part2.jou | 92 ++++++++++++++++++++++++++ examples/aoc2023/day11/sampleinput.txt | 10 +++ 3 files changed, 188 insertions(+) create mode 100644 examples/aoc2023/day11/part1.jou create mode 100644 examples/aoc2023/day11/part2.jou create mode 100644 examples/aoc2023/day11/sampleinput.txt diff --git a/examples/aoc2023/day11/part1.jou b/examples/aoc2023/day11/part1.jou new file mode 100644 index 00000000..ea39cd6f --- /dev/null +++ b/examples/aoc2023/day11/part1.jou @@ -0,0 +1,86 @@ +import "stdlib/io.jou" +import "stdlib/mem.jou" +import "stdlib/str.jou" +import "stdlib/math.jou" + + +class Input: + width: int + height: int + data: byte* + + def duplicate_blank_lines(self) -> None: + self->data = realloc(self->data, 2*strlen(self->data) + 1) + assert self->data != NULL + + y = 0 + while y < self->height: + if strspn(&self->data[(self->width + 1)*y], ".") == self->width: + # duplicate row + blank_start = &self->data[(self->width + 1)*y] + next_start = &self->data[(self->width + 1)*(y+1)] + memmove(next_start, blank_start, strlen(blank_start) + 1) + y += 2 + self->height++ + else: + y++ + + def transpose(self) -> None: + old_content = strdup(self->data) + old_width = self->width + old_height = self->height + self->width = old_height + self->height = old_width + + memset(self->data, '\n', strlen(self->data)) + for x = 0; x < self->width; x++: + for y = 0; y < self->height; y++: + self->data[(self->width + 1)*y + x] = old_content[(old_width + 1)*x + y] + + free(old_content) + + # array is terminated by [-1, -1] + def get_hashtag_coords(self) -> int[2]*: + result: int[2]* = malloc(sizeof(result[0]) * (self->width * self->height + 1)) + result_len = 0 + for y = 0; y < self->height; y++: + for x = 0; x < self->width; x++: + if self->data[(self->width + 1)*y + x] == '#': + result[result_len++] = [x, y] + + result[result_len] = [-1, -1] + return result + + +def manhattan_distance(a: int[2], b: int[2]) -> int: + return abs(a[0]-b[0]) + abs(a[1]-b[1]) + + +def main() -> int: + max_len = 100000 + input = Input{data = calloc(1, max_len+1)} + + f = fopen("sampleinput.txt", "r") + assert f != NULL + fread(input.data, 1, max_len, f) + fclose(f) + + input.width = strcspn(input.data, "\n") as int + input.height = (strlen(input.data) as int) / (input.width + 1) + + input.duplicate_blank_lines() + input.transpose() + input.duplicate_blank_lines() + input.transpose() + + coords = input.get_hashtag_coords() + + sum = 0 + for i = 0; coords[i][0] != -1; i++: + for k = 0; k < i; k++: + sum += manhattan_distance(coords[i], coords[k]) + printf("%d\n", sum) # Output: 374 + + free(input.data) + free(coords) + return 0 diff --git a/examples/aoc2023/day11/part2.jou b/examples/aoc2023/day11/part2.jou new file mode 100644 index 00000000..b05ace4b --- /dev/null +++ b/examples/aoc2023/day11/part2.jou @@ -0,0 +1,92 @@ +import "stdlib/io.jou" +import "stdlib/mem.jou" +import "stdlib/str.jou" + + +class Input: + width: int + height: int + data: byte* + + # returned array is terminated by -1 + def find_blank_lines(self) -> int*: + result: int* = malloc(sizeof(result[0]) * (self->height + 1)) + result_len = 0 + + for y = 0; y < self->height; y++: + if strspn(&self->data[(self->width + 1)*y], ".") == self->width: + result[result_len++] = y + result[result_len] = -1 + return result + + def transpose(self) -> None: + old_content = strdup(self->data) + old_width = self->width + old_height = self->height + self->width = old_height + self->height = old_width + + memset(self->data, '\n', strlen(self->data)) + for x = 0; x < self->width; x++: + for y = 0; y < self->height; y++: + self->data[(self->width + 1)*y + x] = old_content[(old_width + 1)*x + y] + + free(old_content) + + # returned array is terminated by -1 + def get_hashtag_y_coords(self) -> int*: + result: int* = malloc(sizeof(result[0]) * (self->width * self->height + 1)) + result_len = 0 + for y = 0; y < self->height; y++: + for x = 0; x < self->width; x++: + if self->data[(self->width + 1)*y + x] == '#': + result[result_len++] = y + + result[result_len] = -1 + return result + + +def sum_vertical_distances(input: Input*) -> long: + hashtag_y_coords = input->get_hashtag_y_coords() + blank_lines = input->find_blank_lines() + + result = 0L + for end = hashtag_y_coords; *end != -1; end++: + for start = hashtag_y_coords; start < end; start++: + for y = *start; y < *end; y++: + y_is_blank = False + for p = blank_lines; *p != -1; p++: + if *p == y: + y_is_blank = True + break + if y_is_blank: + # Change to 1000000 for actual input + result += 100 + #result += 1000000 + else: + result += 1 + + free(hashtag_y_coords) + free(blank_lines) + return result + + +def main() -> int: + max_len = 100000 + input = Input{data = calloc(1, max_len+1)} + + f = fopen("sampleinput.txt", "r") + assert f != NULL + fread(input.data, 1, max_len, f) + fclose(f) + + input.width = strcspn(input.data, "\n") as int + input.height = (strlen(input.data) as int) / (input.width + 1) + + result = sum_vertical_distances(&input) + input.transpose() + result += sum_vertical_distances(&input) + printf("%lld\n", result) # Output: 8410 + + free(input.data) + return 0 diff --git a/examples/aoc2023/day11/sampleinput.txt b/examples/aoc2023/day11/sampleinput.txt new file mode 100644 index 00000000..986aad4a --- /dev/null +++ b/examples/aoc2023/day11/sampleinput.txt @@ -0,0 +1,10 @@ +...#...... +.......#.. +#......... +.......... +......#... +.#........ +.........# +.......... +.......#.. +#...#..... From 424e4453e5f96d8e14b08c19fbb6f5907eef6008 Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 11 Dec 2023 19:50:29 +0200 Subject: [PATCH 8/9] Update .gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 81177664..61a48c3d 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,7 @@ # Tests use sampleinput.txt files, copied from problem descriptions. # # Creator of AoC tells people not to commit inputs to git. I also cannot -# commit problem descriptions to cit because of copyright. +# commit problem descriptions to git because of copyright. /examples/aoc2023/day*/input /examples/aoc2023/day*/input.txt /examples/aoc2023/day*/puzzle.md From 91907866ece9b098a09b6ac3de499a79168797aa Mon Sep 17 00:00:00 2001 From: Akuli Date: Mon, 11 Dec 2023 19:52:08 +0200 Subject: [PATCH 9/9] remove some variables --- examples/aoc2023/day10/part1.jou | 1 - examples/aoc2023/day10/part2.jou | 1 - 2 files changed, 2 deletions(-) diff --git a/examples/aoc2023/day10/part1.jou b/examples/aoc2023/day10/part1.jou index 1519355d..5cf3106c 100644 --- a/examples/aoc2023/day10/part1.jou +++ b/examples/aoc2023/day10/part1.jou @@ -54,7 +54,6 @@ def find_initial_direction(input: Input*, S: int[2]) -> int[2]: directions = [[0,1], [0,-1], [1,0], [-1,0]] for d = &directions[0]; d < &directions[4]; d++: S_to_neighbor = *d - neighbor_to_S = [-S_to_neighbor[0], -S_to_neighbor[1]] neighbor = [S[0] + S_to_neighbor[0], S[1] + S_to_neighbor[1]] if input->is_in_bounds(neighbor) and input->get(neighbor) != '.': diff --git a/examples/aoc2023/day10/part2.jou b/examples/aoc2023/day10/part2.jou index 6346464a..1319c025 100644 --- a/examples/aoc2023/day10/part2.jou +++ b/examples/aoc2023/day10/part2.jou @@ -55,7 +55,6 @@ def find_initial_direction(input: Input*, S: int[2]) -> int[2]: directions = [[0,1], [0,-1], [1,0], [-1,0]] for d = &directions[0]; d < &directions[4]; d++: S_to_neighbor = *d - neighbor_to_S = [-S_to_neighbor[0], -S_to_neighbor[1]] neighbor = [S[0] + S_to_neighbor[0], S[1] + S_to_neighbor[1]] if input->is_in_bounds(neighbor) and input->get(neighbor) != '.':