r/adventofcode • • Dec 19 '17

SOLUTION MEGATHREAD -๐ŸŽ„- 2017 Day 19 Solutions -๐ŸŽ„-

--- Day 19: A Series of Tubes ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Need a hint from the Hugely* Handyโ€  Haversackโ€ก of Helpfulยง Hintsยค?

Spoiler


AoC ops @ T-2 minutes to launch:

[23:58] <daggerdragon> ATTENTION MEATBAGS T-2 MINUTES TO LAUNCH

[23:58] <Topaz> aaaaah

[23:58] <Cheezmeister> Looks like I'll be just able to grab my input before my flight boards. Wish me luck being offline in TOPAZ's HOUSE OF PAIN^WFUN AND LEARNING

[23:58] <Topaz> FUN AND LEARNING

[23:58] <Hade> FUN IS MANDATORY

[23:58] <Skie> I'm pretty sure that's not the mandate for today

[Update @ 00:16] 69 gold, silver cap

  • My tree is finally trimmed with just about every ornament I own and it's real purdy. hbu?

[Update @ 00:18] Leaderboard cap!

  • So, was today's mandate Helpful Hint any help at all?

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

11 Upvotes

186 comments sorted by

View all comments

3

u/[deleted] Dec 19 '17

Pretty happy with this one. I nearly didn't bother, until I realized I could ignore almost everything except '+'.

Golang: package main

import (
	"strings"
)

type coord struct {
	Y int
	X int
}

type direction struct {
	Y int
	X int
}

func walk(in string) (string, int) {
	maze := strings.Split(strings.Trim(in, "\n"), "\n")

	pos := coord{Y: 0, X: strings.IndexRune(maze[0], '|')}
	dir := direction{Y: 1, X: 0}
	out := make([]byte, 0)
	count := 0
	for {
		var prev coord
		prev, pos = pos, coord{pos.Y + dir.Y, pos.X + dir.X}
		count++
		if pos.Y < 0 || pos.Y >= len(maze) || pos.X < 0 || pos.X >= len(maze[0]) {
			break
		}

		char := maze[pos.Y][pos.X]
		if char >= 'A' && char <= 'Z' {
			out = append(out, char)
		}

		if char == ' ' {
			break
		}

		if char == '+' {
			// find another way out
			for _, nextDir := range []direction{direction{-1, 0}, direction{0, -1}, direction{0, 1}, direction{1, 0}} {
				next := coord{pos.Y + nextDir.Y, pos.X + nextDir.X}
				if next == prev {
					continue
				}

				if next.Y < 0 || next.Y >= len(maze) || next.X < 0 || next.X >= len(maze[0]) {
					continue
				}

				nextChar := maze[next.Y][next.X]
				if nextChar == ' ' {
					continue
				}
				dir = nextDir
			}
		}
	}
	return string(out), count
}

func puzzle1(in string) string {
	letters, _ := walk(in)
	return letters
}

func puzzle2(in string) int {
	_, count := walk(in)
	return count
}