23

Day 3: Foggy Forest

Megathread guidelines

  • 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

FAQ

you are viewing a single comment's thread
view the rest of the comments
[-] Camille@lemmy.ml 6 points 1 day ago

Hare

Not too complicated but my solution is clearly inefficient. For part 1 I built a horizontal segments for each pair of trees on each line and when the length of a segment fit in the height of the currently read map, I check if the same segment exists $length rows higher.

part 1

type hsegment = struct {
	column: uint,
	length: uint,
};

fn hseg(col: uint, length: uint) hsegment = hsegment {
	column = col,
	length = length,
};

fn seg_eq(lhs: hsegment, rhs: hsegment) bool = {
	return lhs.column == rhs.column && lhs.length == rhs.length;
};

fn get_all_segments(line: []u8) []hsegment = {
	let segs: []hsegment = alloc([], 0)!;
	let found: []uint = alloc([], 0)!;
	defer free(found);

	for (let col = len(line) - 1; col < len(line); col -= 1) {
		const c = line[col];
		if (c == '#') {
			for (let fnd .. found) {
				append(segs, hseg(col: uint, fnd-col: uint))!;
			};
			append(found, col: uint)!;
		};
	};

	return segs;
};

fn find_segment(line: []hsegment, seg: hsegment) bool = {
	for (let s .. line) {
		if (seg_eq(s, seg))
			return true;
	};
	return false;
};

fn part1(scan: *bufio::scanner) (int | aoc::error) = {
	let segs: [][]hsegment = alloc([], 0)!;
	defer free(segs);

	let squares = 0;
	let linenb = 0;

	for (true) {
		match (bufio::read_line(scan)!) {
		case io::EOF => break;
		case let line: []u8 => {
			const cursegs = get_all_segments(line);
			append(segs, cursegs)!;

			const height = len(segs);
			for (let seg .. cursegs) {
				if (seg.length >= height)
					continue;

				const otherline = segs[height - seg.length - 1];
				if (find_segment(otherline, seg)) {
					squares += 1;
				};
			};
		};
		};

		linenb += 1;
	};

	for (let s .. segs) {
		free(s);
	};
	return squares;
};

For part 2 I created a real list of points, wrote functions to do all the math around points and defining squares. I build a list of all the known squares and finally compute its length. At some point it didn't work because I only checked the length of the sides of my squares, not the diagonals, so I had too many squares but couldn't tell what was wrong. I doubted the sort algorithm so hard I rewrote a bubble sort, to be sure lmao

part 2

type point = struct {
	x: int,
	y: int,
};

fn p(x: int, y: int) point = point { x = x, y = y };
fn p_eq(lhs: point, rhs: point) bool = {
	return lhs.x == rhs.x && lhs.y == rhs.y;
};

fn p_cmp(a: point, b: point) int = {
	const dx = b.x - a.x;
	if (dx != 0)
		return dx;

	return b.y - a.y;
};

// Good old squared hypothenuse, no need to sqrt it.
fn distance(lhs: point, rhs: point) int = {
	const dx = rhs.x - lhs.x;
	const dy = rhs.y - lhs.y;
	return dx*dx + dy*dy;
};

// Given an original point orig and two points A and B, find the fourth point
// that make it a square and return true if any.
//
// If distance(A, orig) != distance(B, orig), the function returns false.
fn find_fourth(
	orig: point,
	a: point,
	b: point,
	points: []point,
) nullable * point = {
	if (p_eq(orig, a) || p_eq(orig, b) || p_eq(a, b))
		return null;

	const dist = distance(orig, a);
	const dist2 = distance(a, b);

	if (dist != distance(orig, b))
		return null;

	for (let p .. points) {
		if (p_eq(p, orig) || p_eq(p, a) || p_eq(p, b))
			continue;

		if (
			distance(a, p) == dist
			&& distance(b, p) == dist
			&& dist2 == distance(orig, p)
		) {
			return &p;
		};
	};

	return null;
};

fn add_all_points(points: *[]point, line: []u8, linenb: uint) []point = {
	let curpoints: []point = alloc([], 0)!;

	for (let col = 0u; col < len(line): uint; col += 1) {
		if (line[col] == '#') {
			const pnt = p(linenb: int, col: int);
			append(points, pnt)!;
			append(curpoints, pnt)!;
		};
	};

	return curpoints;
};

type square = [4]point;

fn make_square(p1: point, p2: point, p3: point, p4: point) square = {
	let sq = [p1, p2, p3, p4]: square;

	for (let i = 0z; i < 3; i += 1) {
		for (let j = i + 1; j < 4; j += 1) {
			if (p_cmp(sq[i], sq[j]) > 0) {
				let tmp = sq[i];
				sq[i] = sq[j];
				sq[j] = tmp;
			};
		};
	};

	return sq;
};

fn add_square(squares: *[]square, sq: square) void = {
	for (let s .. squares) {
		if (
			p_eq(s[0], sq[0])
			&& p_eq(s[3], sq[3])
		) {
			return;
		};
	};
	append(squares, sq)!;
};

fn search_them_all(
	squares: *[]square,
	curpoints: []point,
	points: []point,
) void = {
	const pnb = len(points);
	for (let cur .. curpoints) {
		for (let i = 0z; i < pnb - 1; i += 1) {
			for (let j = i; j < pnb; j += 1) {
				const a = points[i];
				const b = points[j];
				match (find_fourth(cur, a, b, points)) {
				case null => continue;
				case let p: *point =>
					const sq = make_square(cur, a, b, *p);
					add_square(squares, sq);
				};
			};
		};
	};
};

fn part2(scan: *bufio::scanner) (int | aoc::error) = {
	let points: []point = alloc([], 0)!;
	let linenb = 0u;
	let squares: []square = alloc([], 0)!;
	defer free(squares);

	for (true) {
		match (bufio::read_line(scan)!) {
		case io::EOF => break;
		case let line: []u8 => {
			const curpoints = add_all_points(&points, line, linenb);
			defer free(curpoints);

			search_them_all(&squares, curpoints, points);
		};
		};

		linenb += 1;
	};

	return len(squares): int;
};

this post was submitted on 03 Dec 2026
23 points (100.0% liked)

Advent Of Code

1278 readers
56 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 2026

Solution Threads

M T W T F S S
1 2 3 4 5 6
7 8 9 10 11 12

Visualisations Megathread

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 3 years ago
MODERATORS