Quest 2: From Complex to Clarity

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

Link to participate: https://everybody.codes/

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 2 points 11 months ago

Sadly Uiua doesn't handle the large numbers here well, so here's a Dart solution instead.

import 'dart:math';
import 'package:more/more.dart';

typedef P = Point;
void main(List<String> args) {
  P add(P a, P b) => P(a.x + b.x, a.y + b.y);
  P mul(P a, P b) => P(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x);
  P div(P a, P b) => P(a.x ~/ b.x, a.y ~/ b.y);
  P part1(P a) {
    P r = P(0, 0);
    for (var _ in 0.to(3)) {
      r = add(div(mul(r, r), P(10, 10)), a);
    }
    return r;
  }

  bool test2(P a) {
    P r = P(0, 0);
    for (var _ in 0.to(100)) {
      r = add(div(mul(r, r), P(100000, 100000)), a);
      if (r.x.abs() > 1000000 || r.y.abs() > 1000000) return false;
    }
    return true;
  }

  part2(P a, {step = 1}) => [
        for (var x in 0.to(1001, step: step))
          for (var y in 0.to(1001, step: step)) test2(P(a.x + x, a.y + y))
      ].count((e) => e);

  print(part1(P(25, 9)));
  print(part2(P(35300, -64910), step: 10));
  print(part2(P(35300, -64910)));
}
  • source