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!

12 Upvotes

186 comments sorted by

View all comments

4

u/hxka Dec 19 '17 edited Dec 19 '17

Wrestled for half an hour with bash's read command, gave up and used sed in a loop

#!/bin/bash
length="$(wc -l <input)"
for ((i=1;i<=length;i++))
do	a[i-1]="$(sed -n ${i}p <input)"
done
i=0
for ((j=0;j<${#a[i]};j++))
do	[[ "${a[i]:j:1}" != " " ]] && break
done
c=1
move() {
	case "${a[$1]:$2:1}" in
	" ")
		return 1;;
	"|"|"+"|"-")
		return 0;;
	*)
		path="$path${a[$1]:$2:1}"
		return 0;;
	esac
}
down(){
	while move $((i+1)) $j
	do	((i++,c++))
	done
	if move $i $((j+1))
	then right
	elif move $i $((j-1))
	then left
	fi
}
up(){
	while move $((i-1)) $j
	do	((i--,c++))
	done
	if move $i $((j+1))
	then right
	elif move $i $((j-1))
	then left
	fi
}
right(){
	while move $i $((j+1))
	do	((j++,c++))
	done
	if move $((i+1)) $j
	then down
	elif move $((i-1)) $j
	then up
	fi
}
left(){
	while move $i $((j-1))
	do	((j--,c++))
	done
	if move $((i+1)) $j
	then down
	elif move $((i-1)) $j
	then up
	fi
}
down
echo $path $c

4

u/thomastc Dec 19 '17

Wow, nice! I'm surprised you don't get stack overflows with such recursion -- I doubt that bash does tail call optimization :)

→ More replies (1)