I'm pretty sure part 2 has to be O(n2) but you can cut down on the total number of iterations by only looking forward...here's my solution (implemented in Julia)
function inventory_management_diff(input)word_idx=0whileword_idx<length(input)word_idx+=1word1=input[word_idx]forword2ininput[word_idx+1:end]ch_diff=one_char_diff(word1,word2)ifch_diff!=nothingreturnstring(word1[1:ch_diff-1],word1[ch_diff+1:end])endendendendfunction one_char_diff(word1,word2)found_diff=nothingch_idx=0whilech_idx<length(word1)ch_idx+=1ifword1[ch_idx]!=word2[ch_idx]iffound_diff==nothingfound_diff=ch_idxelsereturnnothingendendendreturnfound_diffendfunction main()filename=ARGS[1]# julia arrays are 1-indexed!input=split(read(filename,String))test_input=["abcde","fghij","klmno","pqrst","fguij","axcye","wvxyz"]println(inventory_management_diff(input))endmain()
Julia's a weird language...it's so close to python that I forget it does weird things like "Arrays are 1-indexed", and it spells None/null as nothing
Ryan is an engineer in the Sacramento Area with a focus in Python, Ruby, and Rust. Bash/Python Exercism mentor. Coding, physics, calculus, music, woodworking. Looking for work!
Hosting my solutions on my github...
MustafaHaddara / advent-of-code-2018
Code for https://adventofcode.com/2018/
advent-of-code-2018
Code for adventofcode.com/2018/
I'm trying to learn Julia so I'm using AoC to force myself to learn.
I'm pretty sure part 2 has to be O(n2) but you can cut down on the total number of iterations by only looking forward...here's my solution (implemented in Julia)
Julia's a weird language...it's so close to python that I forget it does weird things like "Arrays are 1-indexed", and it spells
None
/null
asnothing
I agree that Julia is weird, but I actually love it because it adds some functional stuff that I miss in python like the pipe operator!