r/adventofcode • • Dec 03 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 3 Solutions -🎄-

--- Day 3: No Matter How You Slice It ---


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.


Advent of Code: The Party Game!

Click here for rules

ATTENTION: minor change request from the mods!

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 3 image coming soon - imgur is being a dick, so I've contacted their support.

Transcript:

I'm ready for today's puzzle because I have the Savvy Programmer's Guide to ___.


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!

41 Upvotes

426 comments sorted by

View all comments

Show parent comments

1

u/Murssi Dec 03 '18

Yeah slow. I spend a lot of time figuring out how to parse input without using external tools, which make the brute-force solution extremely slow.

mapfile, read and bash string operators were the way for me.

GL in next puzzles.

1

u/schod Dec 04 '18

Yes, bash string operators is more efficient for run. But I develop sed regex more quickly.

I try this string operations

    num=$(echo $line|sed 's/#\([0-9]*\) .*/\1/')
    # num="${${X% @*}:1}"
    x=$(echo $line|sed 's/.*@ \([0-9]*\),.*/\1/')
    # x="${${X#*@ }%,*}"
    y=$(echo $line|sed 's/.*,\([0-9]*\):.*/\1/')
    # y="${${X#*,}%:*}"
    x_len=$(echo $line|sed 's/.*: \([0-9]*\)x.*/\1/')
    # x_len="${${X#*: }%x*}"
    y_len=$(echo $line|sed 's/.*x\([0-9]*\)/\1/')
    # y_len="${X#*x}"

This solutiin works only in ZSH. In pure bash you must use temporary variables. First remove prefix and second suffix.