## DEV Community π©βπ»π¨βπ» is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

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
}
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";
msg=S"%this.vals(), %word"]
\#vals.set(that,val=word)
)
mut method Void processR1(S word) = {
(size) = 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 void
}
mut method Void processR4(S word) = {
if done return void
open = I.List()(for i in Range(10I), v in this.vals() if v==S"" \add(i))
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()"
}
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
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 = (
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)
)
``````

If you liked this post, consider giving a look to the '1 week report' on advent of code