day 13
This commit is contained in:
parent
e897435921
commit
89cefb4591
|
|
@ -28,7 +28,7 @@ Tokens str_tokenize(Arena *arena, str s, str delim) {
|
|||
|
||||
str stuff = s;
|
||||
isize token_count = 0;
|
||||
while (str_next_token(&stuff, delim).len > 0) {
|
||||
for (; stuff.len > 0; str_next_token(&stuff, delim)) {
|
||||
token_count++;
|
||||
}
|
||||
|
||||
|
|
|
|||
73
aoc/common.h
73
aoc/common.h
|
|
@ -98,76 +98,3 @@ typedef double f64;
|
|||
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
||||
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
#define CLAMP(x, lo, hi) ((x) < (lo) ? (lo) : (x) > (hi) ? (hi) : (x))
|
||||
|
||||
typedef union Vec2 {
|
||||
struct { f32 x, y; };
|
||||
struct { f32 u, v; };
|
||||
} Vec2;
|
||||
|
||||
typedef union Vec2i {
|
||||
struct { i32 x, y; };
|
||||
struct { i32 u, v; };
|
||||
} Vec2i ;
|
||||
|
||||
typedef struct Mat4 {
|
||||
float e[4][4];
|
||||
} Mat4;
|
||||
|
||||
|
||||
static inline Vec2
|
||||
vec2_add(Vec2 a, Vec2 b) {
|
||||
Vec2 result = {
|
||||
.x = a.x + b.x,
|
||||
.y = a.y + b.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline Vec2
|
||||
vec2_sub(Vec2 a, Vec2 b) {
|
||||
Vec2 result = {
|
||||
.x = a.x - b.x,
|
||||
.y = a.y - b.y,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline Vec2
|
||||
vec2(f32 x, f32 y) {
|
||||
Vec2 result = {{ x, y }};
|
||||
return result;
|
||||
}
|
||||
|
||||
static inline f32
|
||||
vec2_dot(Vec2 a, Vec2 b) {
|
||||
return a.x * b.x + a.y * b.y;
|
||||
}
|
||||
|
||||
static inline f32
|
||||
vec2_norm2(Vec2 v) {
|
||||
return vec2_dot(v, v);
|
||||
}
|
||||
|
||||
static inline f32
|
||||
vec2_norm(Vec2 v) {
|
||||
return sqrtf(vec2_norm2(v));
|
||||
}
|
||||
|
||||
static inline f32
|
||||
vec2_dist(Vec2 a, Vec2 b) {
|
||||
return vec2_norm(vec2_sub(a, b));
|
||||
}
|
||||
|
||||
typedef struct Rect2i {
|
||||
int x, y, w, h;
|
||||
} Rect2i;
|
||||
|
||||
typedef struct Rect2f {
|
||||
f32 x, y, w, h;
|
||||
} Rect2f;
|
||||
|
||||
static inline Rect2f
|
||||
rect2f(f32 x, f32 y, f32 w, f32 h) {
|
||||
Rect2f result = { x, y, w, h };
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,94 @@
|
|||
#include "aoc.h"
|
||||
#include <limits.h>
|
||||
#include <ctype.h>
|
||||
|
||||
typedef struct Vec2i {
|
||||
i64 x;
|
||||
i64 y;
|
||||
} Vec2i;
|
||||
|
||||
static Vec2i
|
||||
parse_numbers(str line, Arena *arena) {
|
||||
bool x_set = false;
|
||||
Vec2i result = {0};
|
||||
for (isize i = 0; i < line.len; i++) {
|
||||
if (isdigit(line.data[i])) {
|
||||
isize end = i + 1;
|
||||
while (end < line.len && isdigit(line.data[end])) {
|
||||
end++;
|
||||
}
|
||||
str num_str = str_sub(line, i, end);
|
||||
if (!x_set) {
|
||||
result.x = (i32) parse_i64(num_str, *arena);
|
||||
x_set = true;
|
||||
}
|
||||
else {
|
||||
result.y = (i32) parse_i64(num_str, *arena);
|
||||
return result;
|
||||
}
|
||||
i = end;
|
||||
}
|
||||
}
|
||||
NOT_REACHABLE();
|
||||
}
|
||||
|
||||
static bool
|
||||
solve(Vec2i a, Vec2i b, Vec2i p, Vec2i *solution) {
|
||||
i64 det = a.x * b.y - a.y * b.x;
|
||||
ASSERT(det != 0);
|
||||
|
||||
i64 numerator_a = p.x * b.y - p.y * b.x;
|
||||
i64 numerator_b = p.y * a.x - p.x * a.y;
|
||||
bool solved = false;
|
||||
if ((numerator_a % det == 0) && (numerator_b % det == 0)) {
|
||||
i64 n_a = numerator_a / det;
|
||||
i64 n_b = numerator_b / det;
|
||||
if (n_a >= 0 && n_b >= 0) {
|
||||
solution->x = n_a;
|
||||
solution->y = n_b;
|
||||
solved = true;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// no integer solution
|
||||
}
|
||||
return solved;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Arena *arena = make_arena(Megabytes(1));
|
||||
str input = read_file(arena, argv[1]);
|
||||
|
||||
str line;
|
||||
i64 part_1 = 0;
|
||||
i64 part_2 = 0;
|
||||
while (input.len > 0) {
|
||||
str a_line = str_next_token(&input, STR("\n"));
|
||||
str b_line = str_next_token(&input, STR("\n"));
|
||||
str prize_line = str_next_token(&input, STR("\n"));
|
||||
str empty_line = str_next_token(&input, STR("\n"));
|
||||
ASSERT(str_trim(empty_line).len == 0);
|
||||
|
||||
Vec2i a = parse_numbers(a_line, arena);
|
||||
Vec2i b = parse_numbers(b_line, arena);
|
||||
Vec2i p = parse_numbers(prize_line, arena);
|
||||
Vec2i p_ = { p.x + 10000000000000ull, p.y + 10000000000000ull };
|
||||
|
||||
#if 0
|
||||
printf("a: %ld %ld\n", a.x, a.y);
|
||||
printf("b: %ld %ld\n", b.x, b.y);
|
||||
printf("p: %ld %ld\n", p.x, p.y);
|
||||
printf("\n");
|
||||
#endif
|
||||
Vec2i n;
|
||||
if (solve(a, b, p, &n)) {
|
||||
part_1 += n.x * 3 + n.y;
|
||||
}
|
||||
|
||||
if (solve(a, b, p_, &n)) {
|
||||
part_2 += n.x * 3 + n.y;
|
||||
}
|
||||
}
|
||||
printf("%ld\n", part_1);
|
||||
printf("%ld\n", part_2);
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
Button A: X+94, Y+34
|
||||
Button B: X+22, Y+67
|
||||
Prize: X=8400, Y=5400
|
||||
|
||||
Button A: X+26, Y+66
|
||||
Button B: X+67, Y+21
|
||||
Prize: X=12748, Y=12176
|
||||
|
||||
Button A: X+17, Y+86
|
||||
Button B: X+84, Y+37
|
||||
Prize: X=7870, Y=6450
|
||||
|
||||
Button A: X+69, Y+23
|
||||
Button B: X+27, Y+71
|
||||
Prize: X=18641, Y=10279
|
||||
Loading…
Reference in New Issue