//// main.swift// advent_of_code_7//// Created by skg on 12/7/20.//importFoundation// i love the internet// https://exceptionshub.com/swift-extract-regex-matches.htmlextensionString{funcregex(pattern:String)->[String]{do{letregex=tryNSRegularExpression(pattern:pattern,options:NSRegularExpression.Options(rawValue:0))letnsstr=selfasNSStringletall=NSRange(location:0,length:nsstr.length)varmatches:[String]=[String]()regex.enumerateMatches(in:self,options:NSRegularExpression.MatchingOptions(rawValue:0),range:all){(result:NSTextCheckingResult?,_,_)inifletr=result{letresult=nsstr.substring(with:r.range)asStringmatches.append(result)}}returnmatches}catch{return[String]()}}}finalclassBag{varcolor:Stringvarcontents:Dictionary<String,Int>init(color:String,contents:Dictionary<String,Int>){self.color=colorself.contents=contents}}funcreadFile(path:String)->[String]{errno=0iffreopen(path,"r",stdin)==nil{perror(path)return[]}varinputArray=[String]()whileletline=readLine(){inputArray.append(line)}returninputArray}funcparseInput(puzzleInput:[String])->Dictionary<String,Bag>{// There are two types of strings// light red bags contain 1 bright white bag, 2 muted yellow bags.// --------- -------------- --------------// color # of other color(s)// However, there can be a line with a bag that has no contents// faded blue bags contain no other bags.// ---------- --// color No contents// every bag is described with 2 words followed by the word "bag(s)"// bags that contain no other bags just have the word "no"// let bagColorRegex = NSRegularExpression("^[a-z]* [a-z]*")// let bagContentsRegex = NSRegularExpression("[0-9]+ [a-z]* [a-z]*")varluggageRack:Dictionary<String,Bag>=[:]forruleinpuzzleInput{letbagColor=rule.regex(pattern:"^[a-z]* [a-z]*")letbagContentsArray=rule.regex(pattern:"[0-9]+ [a-z]* [a-z]*")varbagContentsDict:Dictionary<String,Int>=[:]forbaginbagContentsArray{letbagComponents=bag.components(separatedBy:" ")letbagContentsCount=bagComponents[0]letbagContentsDescriptor=bagComponents[1]+" "+bagComponents[2]bagContentsDict[bagContentsDescriptor]=Int(bagContentsCount)}letnewBag=Bag(color:bagColor[0],contents:bagContentsDict)luggageRack[newBag.color]=newBag}returnluggageRack}funcdoesItHave(bags:Dictionary<String,Bag>,bag:Bag,desiredColor:String)->Bool{ifbag.color==desiredColor{returntrue}forbagKeyinbag.contents.keyswheredoesItHave(bags:bags,bag:bags[bagKey]!,desiredColor:desiredColor){returntrue}returnfalse}letpuzzleInput=readFile(path:"input_7")letbags=parseInput(puzzleInput:puzzleInput)vardayOneCount:Int=0forbaginbags.valueswheredoesItHave(bags:bags,bag:bag,desiredColor:"shiny gold")&&bag.color!="shiny gold"{dayOneCount+=1}print("Part 1: \(dayOneCount)")
Part Duex
//// main.swift// advent_of_code_7//// Created by skg on 12/7/20.//importFoundation// i love the internet// https://exceptionshub.com/swift-extract-regex-matches.htmlextensionString{funcregex(pattern:String)->[String]{do{letregex=tryNSRegularExpression(pattern:pattern,options:NSRegularExpression.Options(rawValue:0))letnsstr=selfasNSStringletall=NSRange(location:0,length:nsstr.length)varmatches:[String]=[String]()regex.enumerateMatches(in:self,options:NSRegularExpression.MatchingOptions(rawValue:0),range:all){(result:NSTextCheckingResult?,_,_)inifletr=result{letresult=nsstr.substring(with:r.range)asStringmatches.append(result)}}returnmatches}catch{return[String]()}}}finalclassBag{varcolor:Stringvarcontents:Dictionary<String,Int>init(color:String,contents:Dictionary<String,Int>){self.color=colorself.contents=contents}}funcreadFile(path:String)->[String]{errno=0iffreopen(path,"r",stdin)==nil{perror(path)return[]}varinputArray=[String]()whileletline=readLine(){inputArray.append(line)}returninputArray}funcparseInput(puzzleInput:[String])->Dictionary<String,Bag>{// There are two types of strings// light red bags contain 1 bright white bag, 2 muted yellow bags.// --------- -------------- --------------// color # of other color(s)// However, there can be a line with a bag that has no contents// faded blue bags contain no other bags.// ---------- --// color No contents// every bag is described with 2 words followed by the word "bag(s)"// bags that contain no other bags just have the word "no"// let bagColorRegex = NSRegularExpression("^[a-z]* [a-z]*")// let bagContentsRegex = NSRegularExpression("[0-9]+ [a-z]* [a-z]*")varluggageRack:Dictionary<String,Bag>=[:]forruleinpuzzleInput{letbagColor=rule.regex(pattern:"^[a-z]* [a-z]*")letbagContentsArray=rule.regex(pattern:"[0-9]+ [a-z]* [a-z]*")varbagContentsDict:Dictionary<String,Int>=[:]forbaginbagContentsArray{letbagComponents=bag.components(separatedBy:" ")letbagContentsCount=bagComponents[0]letbagContentsDescriptor=bagComponents[1]+" "+bagComponents[2]bagContentsDict[bagContentsDescriptor]=Int(bagContentsCount)}letnewBag=Bag(color:bagColor[0],contents:bagContentsDict)luggageRack[newBag.color]=newBag}returnluggageRack}funccountBag(bags:Dictionary<String,Bag>,desiredColor:String)->Int{varcount:Int=0for(bagKey,bagValue)inbags[desiredColor]!.contents{count=count+(bagValue*(countBag(bags:bags,desiredColor:bagKey)+1))}returncount}letpuzzleInput=readFile(path:"input_7")letbags=parseInput(puzzleInput:puzzleInput)print("Part 2: \(countBag(bags:bags,desiredColor:"shiny gold"))")
Swift!
Repo link
Part 1
Part Duex