Yeah, a parser generator was definitely the right way to do it. I was stubborn and stuck to the naive way of doing it, which only worked because I noticed the only variable part was the special weaknesses/immunities bit. So I yanked that out:
and then processed the simple tokens the simple way:
units=parse(Int,tokens[1])# this is the easy onehealth=parse(Int,tokens[5])damage=parse(Int,tokens[13])attack_type=tokens[14]initiative=parse(Int,tokens[end])
Assigning teams was done in a parent method that knew about the entire input:
function parse_input(input)groups::Array{Group}=[]team="Immune System"number=1forlineininputifline==""continueendifline=="Immune System:"continueendifline=="Infection:"team="Infection"number=1continueendg=parse_group(line)g.number=numberg.team=teampush!(groups,g)number+=1endreturngroupsend
I also assigned a group number so I could get similar output to the sample output.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yeah, a parser generator was definitely the right way to do it. I was stubborn and stuck to the naive way of doing it, which only worked because I noticed the only variable part was the special weaknesses/immunities bit. So I yanked that out:
and then processed the simple tokens the simple way:
and then processed the special tokens separately:
by the end I kinda ran out of good variable names was just using
bits
as a name, heh.And then at the end, I could just return a new
Group
object...the first arg is the team, and the second arg is the number, more on that belowAssigning teams was done in a parent method that knew about the entire input:
I also assigned a group number so I could get similar output to the sample output.