DEV Community

Marco Servetto
Marco Servetto

Posted on

Advent of code, day 8

Ok, this was engaging but also very, very challenging.
The only reason I managed to finish is that I made a good 'add' method checking for two important properties before adding.
In this way, I was notified of my broken logic over and over again until I could fix it.
Doing this exercise It was also evident that I need to add to adamsTowel at least the following: split a string on all characters and sorting of lists.

Split = {class method S.List (S that)=//hack to split for now \()(for c in that.replace(S"" with=S",").split(S",")\add(c)) } Values = Data:{ mut S.List vals = \[S"";S"";S"";S"";S"";S"";S"";S"";S"";S""] mut method Void add(I that S word)=( v = \vals.val(that) //here my life saver checks!! X[v==S"" || v==word msg=S"%v, %word"; Match.None()(for w in this.vals() \add(w==word)) msg=S"%this.vals(), %word"] \#vals.set(that,val=word) ) mut method Void processR1(S word) = { (size) = word if size==2I return \add(1I, word=word) if size==3I return \add(7I, word=word) if size==4I return \add(4I, word=word) if size==7I return \add(8I, word=word) return void } read method I op(S that, S minus) = (//this method is the key var res = that //to a shortish solution for c in Split(minus) ( res:=res.replace(c with=S"") ) res.size() ) mut method Void processR2(S word) = { (size) = word if size!=6I return void one=\vals.val(1I) four=\vals.val(4I) if \op(word minus=one)==5I return \add(6I, word=word) if \op(word minus=four)==3I return \add(0I, word=word) return void } mut method Void processR3(S word) = { (size) = word if size!=5I return void one = \vals.val(1I) six = \vals.val(6I) if \op(word minus=one)==3I return \add(3I, word=word) if \op(six minus=word)==1I return \add(5I, word=word) if \op(six minus=word)==2I && \vals.val(3I)!=word return \add(2I, word=word) return void } mut method Void processR4(S word) = { done=Match.Some()(for w in this.vals() \add(w==word)) if done return void open = I.List()(for i in Range(10I), v in this.vals() if v==S"" \add(i)) if open.size()==1I return \add(open.left(), word=word) return void } mut method Void process(S.List words) = ( for w in words ( \processR1(word=w) ) for w in words ( \processR2(word=w) ) for w in words ( \processR3(word=w) ) for w in words ( \processR4(word=w) ) ) read method I decode(S word) = { for res in Range(10I) e in \vals if e==word return res error X"%word %this.vals()" } read method I decode4Digits(S.List words)=( var res = 0I for w in words ( res:=(res*10I)+\decode(word=w) ) res ) } Sort = {class method S (S that)=(//should I sort all and var res = S"" //recompose the string all = Split(that) //instead? for l in S.List[S"a";S"b";S"c";S"d";S"e";S"f";S"g"] ( if l in all res++=l ) res )} Main8Part2 = ( input = Fs.Real.#$of().read(\"input") var I tot=0I for line in input.split(S.nl()) ( vs=Values() bar=line.split(S" | ") imm words=S.List()(for s in bar().split(S" ") \add(Sort(s))) imm wordsB=S.List()(for s in bar().split(S" ") \add(Sort(s))) vs.process(words=words) res=vs.decode4Digits(words=wordsB) tot+=res ) Debug(tot) ) 
Enter fullscreen mode Exit fullscreen mode

If you liked this post, consider giving a look to the '1 week report' on advent of code
(https://www.youtube.com/watch?v=hBuFvq7v6v8)

Top comments (0)