diff options
author | kartofen <mladenovnasko0@gmail.com> | 2023-02-10 23:53:15 +0200 |
---|---|---|
committer | kartofen <mladenovnasko0@gmail.com> | 2023-02-10 23:53:15 +0200 |
commit | 555091332de32b1d3b36c888ed21898aae540750 (patch) | |
tree | 6dd5dc02ecb4d7678d13e5f1265e61e155f33c43 /Advent-of-Code-2022/aoc-15 | |
parent | 9f57e15a054b16d335e9d5c49ddd7be829b4e272 (diff) |
add a couple of days
Diffstat (limited to 'Advent-of-Code-2022/aoc-15')
-rwxr-xr-x | Advent-of-Code-2022/aoc-15/build.sh | 5 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/input.txt | 22 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/main.c | 135 | ||||
-rw-r--r-- | Advent-of-Code-2022/aoc-15/sample.txt | 14 |
4 files changed, 176 insertions, 0 deletions
diff --git a/Advent-of-Code-2022/aoc-15/build.sh b/Advent-of-Code-2022/aoc-15/build.sh new file mode 100755 index 0000000..9e59ef5 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +set -xe + +gcc -o main main.c -Wall -Wextra -pedantic -D_POSIX_C_SOURCE diff --git a/Advent-of-Code-2022/aoc-15/input.txt b/Advent-of-Code-2022/aoc-15/input.txt new file mode 100644 index 0000000..49e9e5e --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/input.txt @@ -0,0 +1,22 @@ +Sensor at x=1384790, y=3850432: closest beacon is at x=2674241, y=4192888 +Sensor at x=2825953, y=288046: closest beacon is at x=2154954, y=-342775 +Sensor at x=3553843, y=2822363: closest beacon is at x=3444765, y=2347460 +Sensor at x=2495377, y=3130491: closest beacon is at x=2761496, y=2831113 +Sensor at x=1329263, y=1778185: closest beacon is at x=2729595, y=2000000 +Sensor at x=2882039, y=2206085: closest beacon is at x=2729595, y=2000000 +Sensor at x=3903141, y=2510440: closest beacon is at x=4006219, y=3011198 +Sensor at x=3403454, y=3996578: closest beacon is at x=3754119, y=4475047 +Sensor at x=3630476, y=1048796: closest beacon is at x=3444765, y=2347460 +Sensor at x=16252, y=2089672: closest beacon is at x=-276514, y=2995794 +Sensor at x=428672, y=1150723: closest beacon is at x=-281319, y=668868 +Sensor at x=2939101, y=3624676: closest beacon is at x=2674241, y=4192888 +Sensor at x=3166958, y=2890076: closest beacon is at x=2761496, y=2831113 +Sensor at x=3758241, y=3546895: closest beacon is at x=4006219, y=3011198 +Sensor at x=218942, y=3011070: closest beacon is at x=-276514, y=2995794 +Sensor at x=52656, y=3484635: closest beacon is at x=-276514, y=2995794 +Sensor at x=2057106, y=405314: closest beacon is at x=2154954, y=-342775 +Sensor at x=1966905, y=2495701: closest beacon is at x=2761496, y=2831113 +Sensor at x=511976, y=2696731: closest beacon is at x=-276514, y=2995794 +Sensor at x=3094465, y=2478570: closest beacon is at x=3444765, y=2347460 +Sensor at x=806671, y=228252: closest beacon is at x=-281319, y=668868 +Sensor at x=3011731, y=1976307: closest beacon is at x=2729595, y=2000000 diff --git a/Advent-of-Code-2022/aoc-15/main.c b/Advent-of-Code-2022/aoc-15/main.c new file mode 100644 index 0000000..242b150 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/main.c @@ -0,0 +1,135 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> + +#if 0 + #define PART part1 +#else + #define PART part2 +#endif + +#if 0 + #define FILENAME "sample.txt" + #define TARGET_Y 10 + #define UPPER 20 +#else + #define FILENAME "input.txt" + #define TARGET_Y 2000000 + #define UPPER 2000000 +#endif + +#define LOWER 1000000 + +#define ENTRIES_CAP 30 +#define CAP 10000000 +#define OFFSET (CAP/2) + +int sensors[CAP][2]; +int beacons[CAP][2]; +size_t entries = 0; + +int obj[CAP]; +int row[CAP]; + +size_t sum = 0; + +#define MAX(x, y) (((x) > (y)) ? (x) : (y)) + +int taxi_distance(int x1, int y1, int x2, int y2) +{ + return abs(x1 - x2) + abs(y1 - y2); +} + +void analyze(int target_y) +{ + memset(obj, 0, sizeof(obj)); + memset(row, 0, sizeof(row)); + + for(size_t i = 0; i < entries; i++) + { + int x1 = sensors[i][0]; int x2 = beacons[i][0]; + int y1 = sensors[i][1]; int y2 = beacons[i][1]; + + int r = taxi_distance(x1, y1, x2, y2); + int diff = abs(target_y - y1); + for(int i = x1 - (r - diff); i <= x1 + (r - diff); i++) { + row[i + OFFSET] = 1; + } + + if(y2 == target_y) obj[x2 + OFFSET] = 1; + } +} + +void part1() +{ + analyze(TARGET_Y); + + for(size_t i = 0; i < CAP; i++) + if(row[i] - obj[i] > 0) sum++; +} + +void part2() +{ + for(int y = LOWER; y <= UPPER; y++) + { + printf("%d\n", y); + analyze(y); + + for(int x = LOWER; x <= UPPER; x++) + if(row[x + OFFSET] == 0 && obj[x + OFFSET] == 0) { + sum = (x * 4000000) + y; + return; + } + } + + exit(1); +} + +void parse_xy(char *str, int *x, int *y, int nth) +{ + char *s, *sx; + char *y_str, *x_str; + + x_str = strtok_r(strtok_r(str, ",", &s), " ", &sx); + y_str = strtok_r(NULL, ",", &s); + + for(int i = 1; i < nth; i++) + x_str = strtok_r(NULL, " ", &sx); + + assert(x_str != NULL); + assert(y_str != NULL); + + *x = atoi(&(x_str[2])); + *y = atoi(&(y_str[3])); +} + +void parse() +{ + FILE *fp = fopen(FILENAME, "r"); + if(!fp) { + fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME); + exit(1); + } + + char line[512]; + while(fgets(line, sizeof(line), fp)) + { + size_t len = strlen(line); + if(line[len-1] == '\n') line[len-1] = 0; + + parse_xy(strtok(line, ":"), &sensors[entries][0], &sensors[entries][1], 3); + parse_xy(strtok(NULL, ":"), &beacons[entries][0], &beacons[entries][1], 5); + entries++; + } + + fclose(fp); +} + +int main(void) +{ + parse(); + PART(); + printf("%ld\n", sum); + return 0; +} diff --git a/Advent-of-Code-2022/aoc-15/sample.txt b/Advent-of-Code-2022/aoc-15/sample.txt new file mode 100644 index 0000000..a612424 --- /dev/null +++ b/Advent-of-Code-2022/aoc-15/sample.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3 |