13

Day 2: Fulminating Farmland

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 4 points 8 hours ago

Hare

The first part wasn't hard, I just had to check the maths twice. But I've spent way too much time on part 2 realizing that my function to find the optimal instruction iterated in reverse order, leading me to choose either a +6 move or the worst available one hehehe.

part 1

type waypoint = enum uint {
	red = 0,
	orange = 1,
	yellow = 2,
	green = 3,
	blue = 4,
	violet = 5,
	__modulo = 6,
};

type state = struct {
	current: waypoint,
	distance: uint,
};

fn u8_to_waypoint(c: u8) (waypoint | aoc::error) = {
	switch (c) {
	case 'r' => return waypoint::red;
	case 'o' => return waypoint::orange;
	case 'y' => return waypoint::yellow;
	case 'g' => return waypoint::green;
	case 'b' => return waypoint::blue;
	case 'v' => return waypoint::violet;
	case =>
	     return fmt::asprintf("Unknown input '{}'", c)!: aoc::error;
	};
};

fn goto_next(st: *state, w: waypoint) void = {
	if (st.current < w) {
		st.distance += w - st.current;
	} else {
		st.distance += waypoint::__modulo - st.current + w;
	};

	st.current = w;
};

fn part1(scan: *bufio::scanner) (int | aoc::error) = {
	let st = state { current = waypoint::red, distance = 0 };
	const prgm = bufio::read_line(scan)!: []u8;

	for (let insn .. prgm) {
		goto_next(&st, u8_to_waypoint(insn)?);
	};

	return st.distance: int;
};

part 2

fn waypoint_to_u8(w: waypoint) u8 = {
	switch (w) {
	case waypoint::red => return 'r';
	case waypoint::orange => return 'o';
	case waypoint::yellow => return 'y';
	case waypoint::green => return 'g';
	case waypoint::blue => return 'b';
	case waypoint::violet => return 'v';
	case =>
	     return 'a'; // unreachable
	};
};

fn next_wp(w: waypoint) waypoint = switch (w) {
	case waypoint::red => return waypoint::violet;
	case waypoint::orange => return waypoint::red;
	case waypoint::yellow => return waypoint::orange;
	case waypoint::green => return waypoint::yellow;
	case waypoint::blue => return waypoint::green;
	case waypoint::violet => return waypoint::blue;
	case waypoint::__modulo => return waypoint::__modulo;
};

fn find_insn(w: waypoint, prgm: *[]u8) bool = {
	let insn = waypoint_to_u8(w);
	match (bytes::index(*prgm, insn)) {
	case void =>
		return false;
	case let idx: size =>
		const left = prgm[0..idx];
		const right = prgm[idx+1..];
		append(left, right...)!;
		*prgm = left;
		return true;
	};
};

fn goto_farthest(st: *state, prgm: *[]u8) void = {
	let best_insn = st.current;

	for (true) {
		if (find_insn(best_insn, prgm))
		   break;

		best_insn = next_wp(best_insn);
	};

	goto_next(st, best_insn);
};

fn part2(scan: *bufio::scanner) (int | aoc::error) = {
	let st = state { current = waypoint::red, distance = 0 };
	let prgm = bufio::read_line(scan)!: []u8;

	for (len(prgm) > 0) {
		goto_farthest(&st, &prgm);
	};

	return st.distance: int;
};

this post was submitted on 02 Dec 2026
13 points (100.0% liked)

Advent Of Code

1262 readers
48 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