FML
This commit is contained in:
parent
de73079754
commit
bbe7d66027
@ -37,10 +37,12 @@ Ratio intersect_x(Line l1, Line l2) {
|
||||
Ratio left_side = ratio_subtract(l1.slope, l2.slope);
|
||||
Ratio right_side = ratio_subtract(l2.offset, l1.offset);
|
||||
Ratio divided = ratio_divide(right_side, left_side);
|
||||
printf("left: %lld/%lld, right: %lld/%lld, divided: %lld/%lld\n", left_side.num, left_side.denom, right_side.num, right_side.denom, divided.num, divided.denom);
|
||||
return divided;
|
||||
}
|
||||
|
||||
int li_cost(int ax, int ay, int bx, int by, int px, int py) {
|
||||
long long li_cost(int ax, int ay, int bx, int by, long long px, long long py) {
|
||||
printf("prize at %lld, %lld\n", px, py);
|
||||
Line b_line;
|
||||
b_line.slope.num = by;
|
||||
b_line.slope.denom = bx;
|
||||
@ -60,36 +62,26 @@ int li_cost(int ax, int ay, int bx, int by, int px, int py) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
long b_x_movement = (r.num / r.denom);
|
||||
long b_cost = b_x_movement / bx;
|
||||
long a_x_movement = px - b_x_movement;
|
||||
long a_cost = a_x_movement / ax;
|
||||
printf("%d\n", b_cost + a_cost);
|
||||
long long b_x_movement = (r.num / r.denom);
|
||||
if (b_x_movement % bx != 0) return 0; // Fuuuuuuuuuuuuuuck this
|
||||
long long b_cost = b_x_movement / bx;
|
||||
long long a_x_movement = px - b_x_movement;
|
||||
if (a_x_movement % ax != 0) return 0; // Fuuuuuuuuuuuuuuck this
|
||||
long long a_cost = (a_x_movement / ax) * 3;
|
||||
printf("b: %lld, a: %lld\n", b_x_movement, a_x_movement);
|
||||
printf("b presses: %lld, a presses: %lld\n", b_x_movement / bx, a_x_movement / ax);
|
||||
if (b_x_movement < 0 || a_x_movement < 0) return 0;
|
||||
printf("cost: %lld\n", b_cost + a_cost);
|
||||
return b_cost + a_cost;
|
||||
}
|
||||
|
||||
int best_cost(int ax, int ay, int bx, int by, int px, int py) {
|
||||
int best_cost_so_far = 0;
|
||||
|
||||
for (int i = 0; i <= 100; i++) {
|
||||
px -= ax;
|
||||
py -= ay;
|
||||
if (px % bx != 0 || py % by != 0) continue;
|
||||
if (px / bx != py / by) continue;
|
||||
|
||||
int cost = (i + 1) * 3 + px / bx;
|
||||
if (best_cost_so_far == 0 || best_cost_so_far > cost) best_cost_so_far = cost;
|
||||
}
|
||||
|
||||
return best_cost_so_far;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int ax, ay, bx, by, px, py;
|
||||
int ax, ay, bx, by;
|
||||
long long px, py;
|
||||
char *line;
|
||||
|
||||
int cost = 0;
|
||||
int cost_2 = 0;
|
||||
long long cost = 0;
|
||||
long long cost_2 = 0;
|
||||
while((line = aoc_read_line()) != NULL) {
|
||||
sscanf(line, "Button A: X+%d, Y+%d", &ax, &ay);
|
||||
sscanf(aoc_read_line(), "Button B: X+%d, Y+%d", &bx, &by);
|
||||
@ -98,9 +90,10 @@ int main() {
|
||||
|
||||
cost += li_cost(ax, ay, bx, by, px, py);
|
||||
cost_2 += li_cost(ax, ay, bx, by, px + 10000000000000, py + 10000000000000);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("Part 1: %d\n", cost);
|
||||
printf("Part 1: %lld\n", cost);
|
||||
printf("Part 2: %lld\n", cost_2);
|
||||
|
||||
aoc_free();
|
||||
|
Loading…
Reference in New Issue
Block a user