Spyke
advent_of_code·Advent Of Codebyhades

🦆 Everybody.Codes 2025 Quest 8 Solutions 🦆

Quest 8: The Art of Connection

  • 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/

View original on programming.dev

Rust

pub fn solve_part_1(input: &str) -> String {
    let numbers: Vec<i32> = input.split(",").map(|x| x.parse().unwrap()).collect();
    let mut count = 0;
    for i in 1..numbers.len() {
        if numbers[i].abs_diff(numbers[i - 1]) == 16 {
            count += 1;
        }
    }
    count.to_string()
}

pub fn solve_part_2(input: &str) -> String {
    let numbers: Vec<i32> = input.split(",").map(|x| x.parse().unwrap()).collect();
    let mut lines: Vec<(i32, i32)> = vec![];
    for i in 1..numbers.len() {
        let (a, b) = (numbers[i - 1], numbers[i]);
        if a > b {
            lines.push((b, a));
        } else {
            lines.push((a, b));
        }
    }
    let mut knots = 0;
    for i in 0..lines.len() {
        for j in 0..i {
            let (a, b) = lines[i];
            let (c, d) = lines[j];
            if a == c || a == d || b == c || b == d {
                continue;
            }
            let c_inside = c > a && c < b;
            let d_inside = d > a && d < b;
            if c_inside != d_inside {
                knots += 1;
            }
        }
    }
    knots.to_string()
}

pub fn solve_part_3(input: &str) -> String {
    let numbers: Vec<i32> = input.split(",").map(|x| x.parse().unwrap()).collect();
    let mut lines: Vec<(i32, i32)> = vec![];
    for i in 1..numbers.len() {
        let (a, b) = (numbers[i - 1], numbers[i]);
        if a > b {
            lines.push((b, a));
        } else {
            lines.push((a, b));
        }
    }
    let mut best_cut_threads = i64::MIN;
    for d in 1..=256 {
        for c in 1..d {
            let mut cut_threads = 0;
            for (a, b) in lines.iter().copied() {
                if a == c || a == d || b == c || b == d {
                    if a == c && b == d {
                        cut_threads += 1;
                    }
                    continue;
                }
                let c_inside = c > a && c < b;
                let d_inside = d > a && d < b;
                if c_inside != d_inside {
                    cut_threads += 1;
                }
            }
            if cut_threads > best_cut_threads {
                best_cut_threads = cut_threads;
            }
        }
    }
    best_cut_threads.to_string()
}
2

Haskell

Woo! I got on the leaderboard at last. I don't think I've seen a problem like this one before, but fortunately it wasn't as tricky as it seemed at first glance.

import Control.Monad  
import Data.List  
import Data.List.Split  
import Data.Tuple  

readInput :: String -> [(Int, Int)]  
readInput = map fixOrder . (zip <*> tail) . map read . splitOn ","  
  where  
    fixOrder (x, y)  
      | x > y = (y, x)  
      | otherwise = (x, y)  

crosses (a, b) (c, d) =  
  not (a == c || a == d || b == c || b == d)  
    && ((a < c && c < b) /= (a < d && d < b))  

part1 n = length . filter ((== n `quot` 2) . uncurry (-) . swap)  

part2 n = sum . (zipWith countKnots <*> inits)  
  where  
    countKnots x strings = length $ filter (crosses x) strings  

part3 n strings =  
  maximum [countCuts (a, b) | a <- [1 .. n - 1], b <- [a + 1 .. n]]  
  where  
    countCuts x = length $ filter (\s -> x == s || x `crosses` s) strings  

main =  
  forM_  
    [ ("everybody_codes_e2025_q08_p1.txt", part1 32),  
      ("everybody_codes_e2025_q08_p2.txt", part2 256),  
      ("everybody_codes_e2025_q08_p3.txt", part3 256)  
    ]  
    $ \(input, solve) -> readFile input >>= print . solve . readInput  
2

Nim

Part 2 - I just really didn't want to think that day. So when puzzle asked me to check if lines intersect - I wrote the intersection checking solution with 2D points.

Part 3 is geometry + bruteforce.

proc solve_part1*(input: string): Solution =
  let pins = input.split(',').mapIt(parseInt(it))
  for i in 0 ..< pins.high:
    let d = abs(pins[i] - pins[i+1])
    if d == 16: inc result.intVal

proc ccw(A,B,C: Vec2): bool = (C.y-A.y) * (B.x-A.x) > (B.y-A.y) * (C.x-A.x)
proc isIntersection(A,B,C,D: Vec2): bool = ccw(A,C,D) != ccw(B,C,D) and ccw(A,B,C) != ccw(A,B,D)

proc solve_part2*(input: string): Solution =
  const two_pi = PI * 2
  const pin_count = 256

  var pins: array[pin_count, Vec2]
  for i in 0 ..< pin_count:
    let angle = two_pi * (i / pin_count)
    let point: Vec2 = (cos(angle), sin(angle))
    pins[i] = point

  let inst = input.split(',').mapIt(parseInt(it))
  var lines: seq[(Vec2, Vec2)]

  for i in 0 ..< inst.high:
    let A = pins[inst[i]-1]
    let B = pins[inst[i+1]-1]

    for (C, D) in lines:
      if isIntersection(A,B,C,D):
        inc result.intVal
    lines.add shortenSegment(A, B, 0.0001)

proc solve_part3*(input: string): Solution =
  const two_pi = PI * 2
  const pin_count = 256

  var pins: array[pin_count, Vec2]
  for i in 0 ..< pin_count:
    let angle = two_pi * (i / pin_count)
    let point: Vec2 = (cos(angle), sin(angle))
    pins[i] = point

  let inst = input.split(',').mapIt(parseInt(it))
  var lines: seq[(Vec2, Vec2)]

  for i in 0 ..< inst.high:
    let A = pins[inst[i]-1]
    let B = pins[inst[i+1]-1]
    lines.add shortenSegment(A, B, 0.0001)

  var bestSum = 0
  for i in 0 ..< pin_count:
    for j in i+1 ..< pin_count:
      let A = pins[i]
      let B = pins[j]
      var sum = 0
      for (C, D) in lines:
        if isIntersection(A,B,C,D): inc sum
      if sum > bestSum: bestSum = sum
  result := bestSum

Full solution at Codeberg: solution.nim

2

Uiua

Just a dirty great hack and a few minutes of toasty CPU for part3 with live data.

"1,5,2,6,8,4,1,7,3"
⊜⋕⊸≠@,
&p /+=⊃(⧈₂(⌵/-)|÷2/↥) # Part1 --> 4

"1,5,2,6,8,4,1,7,3,5,7,8,2"
⊜⋕⊸≠@,
Knot  ← (⊃(=¯|=)∩⌞(±-)⊙°⊟)
Knots ← /↧/↥↯∞_2[∩⌟Knot]°⊟
⧈₂⍆
&p /+≡(/+≡Knots¤°⊂↙¯)⊙¤+1↘2⇡⊸⧻ # Part 2 --> 21

"1,5,2,6,8,4,1,7,3,6"
⊜⋕⊸≠@,
⊃(⧅<2+1⇡⧻◴|⧈₂⍆)            # Possible cuts, existing strings.
/↥+⊃(≡˜∊⊙¤|≡(/+≡Knots¤)⊙¤) # Part3 --> 7
2

Scheme/Guile

Takes about 5 seconds.

(import (rnrs io ports (6))
        (srfi srfi-1))
#!curly-infix

(define (parse-file file-name)
  (let ((sequence (map string->number (string-split (string-trim-both (call-with-input-file file-name get-string-all)) #\,))))
    (zip sequence (cdr sequence))))

(let loop ((sequence (parse-file "notes/everybody_codes_e2025_q08_p1.txt")) (count 0))
  (if (null? sequence)
      (format #t "P1 Answer: ~a\n\n" count)
      (loop (cdr sequence) (+ count (if (and last (eq? (modulo (- (cadar sequence) (caar sequence)) 32) 16)) 1 0)))))

(define (crosses-over? a b)
  (let ((a1 (car a))
        (a2 (cadr a))
        (b1 (car b))
        (b2 (cadr b)))
    (let ((a2 (modulo {a2 - a1} 256))
          (b1 (modulo {b1 - a1} 256))
          (b2 (modulo {b2 - a1} 256)))
      (and (not (eq? b1 0)) (not (eq? b2 0))
      (or
        (and {b1 < a2} {b2 > a2})
        (and {b1 > a2} {b2 < a2}))))))
(define (count-cross-overs sequence a)
  (let loop ((sequence sequence) (count 0))
    (if (null? sequence)
        count
        (loop (cdr sequence) (+ count (if (crosses-over? (car sequence) a) 1 0))))))
(let loop ((sequence (parse-file "notes/everybody_codes_e2025_q08_p2.txt")) (passed '()) (count 0))
  (if (null? sequence)
      (format #t "P2 Answer: ~a\n\n" count)
      (loop (cdr sequence) (cons (car sequence) passed) (+ count (count-cross-overs passed (car sequence))))))


(let ((sequence (parse-file "notes/everybody_codes_e2025_q08_p3.txt")))
  (let loop ((i 1) (greatest 0))
    (if {i > 256}
        (format #t "P3 Answer: ~a\n\n" greatest)
        (loop (1+ i) (max greatest (let loop ((j i) (greatest 0))
              (if {j > 256}
                  greatest
                  (loop (1+ j) (max greatest (count-cross-overs sequence (list i j)))))))))))
2
programming.dev

Python

# pairwise helps picking consecutive values easier
#   [A,B,C,D] -> [AB, BC, CD]
# combinations will give me combinations of elements in a sequence
#   [A,B,C,D] -> [AB, AC, AD, BC, BD, CD]
from itertools import pairwise, combinations


# line will pass thorough the center if the points are on opposite ends,
#   i.e., total points / 2 distance away
def part1(data: str, nail_count: int = 32):
    opposite_dist = nail_count // 2
    nodes = [int(n) for n in data.split(",")]

    center_passes = 0
    for a, b in pairwise(nodes):
        if abs(a - b) == opposite_dist:
            center_passes += 1
    return center_passes


assert part1("1,5,2,6,8,4,1,7,3", 8) == 4


# BRUTEFORCE: take every two points and check if they intersect
def part2(data: str, nail_count: int = 32):
    def intersects(s1, s2):
        # expand the points
        (x1, x2), (y1, y2) = s1, s2
        # make sure x1 is the smaller nail and x2 is the bigger one
        if x1 > x2:
            x1, x2 = x2, x1
        # there is an intersection if one point lies bwtween x1 and x2
        #  and the other lies outside it
        if x1 < y1 < x2 and (y2 > x2 or y2 < x1):
            return True
        if x1 < y2 < x2 and (y1 > x2 or y1 < x1):
            return True
        return False

    nodes = [int(n) for n in data.split(",")]
    knots = 0
    for s1, s2 in combinations(pairwise(nodes), 2):
        if intersects(s1, s2):
            knots += 1

    return knots


assert part2("1,5,2,6,8,4,1,7,3,5,7,8,2", 8) == 21

from collections import defaultdict


# This is better than bruteforce
def part3(data: str, nail_count: int = 256):
    connections = defaultdict(list)
    nodes = [int(n) for n in data.split(",")]

    # record all connections, both ways
    for a, b in pairwise(nodes):
        connections[a].append(b)
        connections[b].append(a)

    max_cuts = 0
    for node_a in range(1, nail_count + 1):
        cuts = 0
        for node_b in range(node_a + 2, nail_count + 1):
            # add new cuts for the node we just added between a and b
            for other_node in connections[node_b - 1]:
                if other_node > node_b or other_node < node_a:
                    cuts += 1
            # also add any cuts for threads that go between node a and b
            cuts += connections[node_a].count(node_b)

            # remove cuts that were going from BETWEEN node_a and node_b-1 (prev node_b) to node_b
            for other_node in connections[node_b]:
                if node_a < other_node < node_b:
                    cuts -= 1
            # also remove any cuts for threads that go between node a and b-1
            cuts -= connections[node_a].count(node_b - 1)

            max_cuts = max(max_cuts, cuts)

    return max_cuts


assert part3("1,5,2,6,8,4,1,7,3,6", 8) == 7
2

Thank you! I love seeing well-documented solutions for problems that I'm struggling with, so I try to add comments for non-trivial parts of any code I post. :)

2

You reached the end

🦆 Everybody.Codes 2025 Quest 8 Solutions 🦆 | Spyke