this post was submitted on 05 Nov 2025
10 points (100.0% liked)

Advent Of Code

1118 readers
5 users here now

An unofficial home for the advent of code community on programming.dev! Other challenges are also welcome!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

Everybody Codes is another collection of programming puzzles with seasonal events.

EC 2025

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 2 years ago
MODERATORS
 

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
[โ€“] janAkali@lemmy.sdf.org 3 points 5 days ago

For quest 2, I decided to implement my own Complex type and operators, because I expected parts 2 and 3 to have something unconventional, but alas it's just a regular Mandelbrot fractal.

Nim again, Nim forever:

type Complex = tuple[x,y: int]
proc `$`(c: Complex): string = &"[{c.x},{c.y}]"
proc `+`(a,b: Complex): Complex = (a.x+b.x, a.y+b.y)
proc `-`(a,b: Complex): Complex = (a.x-b.x, a.y-b.y)
proc `*`(a,b: Complex): Complex = (a.x*b.x - a.y*b.y, a.x*b.y + a.y*b.x)
proc `/`(a,b: Complex): Complex = (a.x div b.x, a.y div b.y)
proc `/`(a: Complex, b: int): Complex = (a.x div b, a.y div b)

proc parseInput(input: string): Complex =
  let parts = input.split({'=','[',',',']'})
  (parseInt(parts[2]), parseInt(parts[3]))

proc isStable(point: Complex, iter: int): bool =
  var num: Complex
  for _ in 1..iter:
    num = (num * num) / 100_000 + point
    if num.x notin -1_000_000 .. 1_000_000 or
       num.y notin -1_000_000 .. 1_000_000:
      return false
  true

proc solve_part1*(input: string): Solution =
  let start = parseInput(input)
  var point: Complex
  for _ in 1..3:
    point = (point * point) / 10 + start
  result := $point

proc solve_part2*(input: string): Solution =
  let start = parseInput(input)
  for y in 0..100:
    for x in 0..100:
      let point: Complex = (start.x + 10 * x, start.y + 10 * y)
      if point.isStable(iter=100): inc result.intVal

proc solve_part3*(input: string): Solution =
  let start = parseInput(input)
  for y in 0..1000:
    for x in 0..1000:
      let point: Complex = (start.x + x, start.y + y)
      if point.isStable(iter=100): inc result.intVal

Full solution at Codeberg: solution.nim