I'm not super thrilled with my Day 2 code, but I haven't really had the time to tweak it with everything going on at work currently.
Swift Solutions
Part 1
This one was fairly simple. Just count how many times each letter appears in each String and act appropriately. I do like the fact that a Swift String type is really an Array of Characters.
// Part 1: Find the checksumsfunccalculateChecksum(_idCodes:[String])->Int{vardoubles=0vartriples=0idCodes.map{(boxID)invardoubleTrue=falsevartripleTrue=falseforcharinboxID{letcount=boxID.filter{$0==char}.countifcount==2{doubleTrue=true}ifcount==3{tripleTrue=true}}doubles+=doubleTrue?1:0triples+=tripleTrue?1:0}returndoubles*triples}letchecksum=calculateChecksum(boxIDs)print("Boxes checksum is: \(checksum)")
Part 2
This one feels clunky, if I get a chance I'll revisit it.
I break on the first hit for a solution to short circuit everything and return the answer, this can help a lot with so many Characters to test.
I use zip(_:_:) with a filter and count to quickly test how many differences there are in the same positions. When I see two strings that differ by one character in the same position I move to the next step.
In the second part I cast the Arrays into Set types so that I can use the Collection Algebra features to quickly find the actual character to remove by subtracting one collection from the other. With that done I can just remove it from the source Array and return what's left.
// Part 2: Box finderfuncfindTheBoxes(_idCodes:[String])->String{varresult=""vardifferceCount=0forboxIDinidCodes{ifdifferceCount==1{break}forcodeinidCodes{differceCount=zip(boxID,code).filter{$0!=$1}.countifdifferceCount==1{letdiff=Set(boxID).subtracting(code)ifletcharToRemove=diff.first{result=boxIDifletfoo=result.index(of:charToRemove){result.remove(at:foo)break}}}}}returnresult}lettheBoxes=findTheBoxes(boxIDs)print("Matching box ID is: \(theBoxes)")
Normally I would import Foundation here so that I could just use NSOrderedSet and skip a few steps. I wanted to try and keep it all in the Swift Standard Library though, so I didn't!
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.
I'm not super thrilled with my Day 2 code, but I haven't really had the time to tweak it with everything going on at work currently.
Swift Solutions
Part 1
This one was fairly simple. Just count how many times each letter appears in each
String
and act appropriately. I do like the fact that a SwiftString
type is really anArray
ofCharacters
.Part 2
This one feels clunky, if I get a chance I'll revisit it.
I break on the first hit for a solution to short circuit everything and return the answer, this can help a lot with so many
Characters
to test.I use
zip(_:_:)
with a filter and count to quickly test how many differences there are in the same positions. When I see two strings that differ by one character in the same position I move to the next step.In the second part I cast the Arrays into
Set
types so that I can use the Collection Algebra features to quickly find the actual character to remove by subtracting one collection from the other. With that done I can just remove it from the sourceArray
and return what's left.Normally I would
import Foundation
here so that I could just useNSOrderedSet
and skip a few steps. I wanted to try and keep it all in the Swift Standard Library though, so I didn't!