I actually managed to complete Part A with an O(n2) algorithm, before realizing that would be too slow for part B...then I realized I could use a stack to implement an O(n) algorithm.
function reduction(chain)chain=reduction_one_pass(chain)returnlength(chain)endfunction reduction_one_pass(chain)result=[chain[1]]i=2whilei<=length(chain)a=result[end]b=chain[i]i+=1whilea!=b&&uppercase(a)==uppercase(b)deleteat!(result,length(result))# remove a# get new a,biflength(result)>0a=result[end]elsea=""endb=chain[i]i+=1end# b doesn't match last char on stackpush!(result,b)endreturnjoin(result,"")endfunction main()filename=ARGS[1]# julia arrays are 1-indexed!input=split(read(filename,String),"\n")[1]test_input="dabAcCaCBAcCcaDA"println(reduction(input))endmain()
Part B is iterating through all letters in the alphabet, doing the string replace and then, doing the same reduction as Part A, while tracking the minimum reduced length. I feel like there's a better way to do that, but...never figured it out.
function find_biggest_reduction(chain)smallest=-1# there's gotta be a pattern here but I can't quite figure it out# so we're brute forcing itchar_A=Int("a"[1])foriinchar_A:char_A+25letter=Char(i)sub=replace(replace(chain,letter=>""),uppercase(letter)=>"")reduced=reduction_one_pass(sub)println("$letter -> length: $(length(reduced))")iflength(reduced)<smallest||smallest==-1smallest=length(reduced)endendreturnsmallestend
I logged my output and saw that, for my input, v reduced far better than anything else.
a -> length: 10432
b -> length: 10448
c -> length: 10510
d -> length: 10484
e -> length: 10450
f -> length: 10528
g -> length: 10424
h -> length: 10490
i -> length: 10480
j -> length: 10480
k -> length: 10444
l -> length: 10386
m -> length: 10426
n -> length: 10454
o -> length: 10476
p -> length: 10412
q -> length: 10476
r -> length: 10420
s -> length: 10426
t -> length: 10452
u -> length: 10456
v -> length: 4684
w -> length: 10468
x -> length: 10366
y -> length: 10476
z -> length: 10486
My gut tells me there's some pattern in the input that would tell you which char to remove, and that way you only have to do the reduction once (as opposed to 26 times).
I'm a Sr. Software Engineer at Flashpoint. I specialize in Python and Go, building functional, practical, and maintainable web systems leveraging Kubernetes and the cloud. Blog opinions are my own.
I actually managed to complete Part A with an O(n2) algorithm, before realizing that would be too slow for part B...then I realized I could use a stack to implement an O(n) algorithm.
Part B is iterating through all letters in the alphabet, doing the string replace and then, doing the same reduction as Part A, while tracking the minimum reduced length. I feel like there's a better way to do that, but...never figured it out.
I logged my output and saw that, for my input,
vreduced far better than anything else.My gut tells me there's some pattern in the input that would tell you which char to remove, and that way you only have to do the reduction once (as opposed to 26 times).
I looked at that, but when it wasn't definitively the letter that showed up the most, I didn't look very much further.
I bet you're right though, there's some "clustering factor" or something that would tip you off for which letter is most productive.