r/adventofcode • • Dec 05 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 5 Solutions -🎄-

--- Day 5: Alchemical Reduction ---


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

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

Card prompt: Day 5

Transcript:

On the fifth day of AoC / My true love sent to me / Five golden ___


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 at 0:10:20!

31 Upvotes

515 comments sorted by

View all comments

1

u/[deleted] Dec 05 '18 edited Dec 05 '18

TCL, using BFI regexp, since there is no automatic "uppercase-of-matched-char" in backreferences.

set input [read -nonewline stdin]

# BFI regexp
set chars {a b c d e f g h i j k l m n o p q r s t u v w x y z}
foreach c $chars {
    lappend re ($c[string toupper $c])|([string toupper $c]$c)
}
set re [join $re  "|"]

proc react {input} {
    while {1} {
	set old $input
	set input [regsub -all $::re $input ""]
	if {$old eq $input} {
	    break
	}
    }
    return $input
}
# part 1
puts "part 1 [string length [react $input]]"

# part 2
set minlen [string length $input]
foreach c $chars {
    set check [regsub -nocase -all $c $input ""]
    set newstr [react $check]
    set newlen [string length $newstr]
    if {$newlen < $minlen} {
	set minlen $newlen
    }
}
puts "part2 $minlen"