I am using Advent of Code to learn Golang, and here is the solution I came up with. Suggestions for improvements are always welcome!
Part 1:
packagemainimport("bufio""fmt""os""strings")// readLines reads a whole file into memory// and returns a slice of its lines.funcreadLines(pathstring)([]string,error){file,err:=os.Open(path)iferr!=nil{returnnil,err}deferfile.Close()varlines[]stringscanner:=bufio.NewScanner(file)forscanner.Scan(){lines=append(lines,scanner.Text())}returnlines,scanner.Err()}funcmain(){boxes,err:=readLines("input")iferr!=nil{panic(err)}vartwos,threesintfor_,box:=rangeboxes{// Iterate through each box letter-by-letter and check if letters appear// two or three times:two,three:=false,falsefor_,letter:=rangebox{if!two&&strings.Count(box,string(letter))==2{twos++two=true}elseif!three&&strings.Count(box,string(letter))==3{threes++three=true}iftwo&&three{// We already found the maximum number of appearing letters we countbreak}}}checksum:=twos*threesfmt.Printf("Checksum is: %d\n",checksum)}
Part 2:
packagemainimport("bufio""fmt""os")// readLines reads a whole file into memory// and returns a slice of its lines.funcreadLines(pathstring)([]string,error){file,err:=os.Open(path)iferr!=nil{returnnil,err}deferfile.Close()varlines[]stringscanner:=bufio.NewScanner(file)forscanner.Scan(){lines=append(lines,scanner.Text())}returnlines,scanner.Err()}funcfindSimilar(boxes[]string)string{// For each box name we remove the i'th element and store the rest of the// name inside a map:visited:=make(map[int]map[string]bool)for_,box:=rangeboxes{// Go through each box:fori:=rangebox{// Remove i'th character from box name:subname:=box[:i]+box[i+1:]// Check if we have already visited a similar box name:_,ok:=visited[i][subname]if!ok{// If we have never encountered a similar box name, add it:_,ok:=visited[i]if!ok{sub:=make(map[string]bool)visited[i]=sub}visited[i][subname]=true}else{// We have encounter a similar box name, return it:returnsubname}}}return""}funcmain(){boxes,err:=readLines("input")iferr!=nil{panic(err)}subname:=findSimilar(boxes)fmt.Println(subname)}
My idea was to use a dictionary and store the subnames and see if we encounter one we have already visited. Since there should only be one match we can immediately return it. I learned a lot about using maps in Golang!
I am also using Python that I have more experience with to cross check solutions. I have tried to implement it with readability in mind, not performance.
I am using Advent of Code to learn Golang, and here is the solution I came up with. Suggestions for improvements are always welcome!
Part 1:
Part 2:
My idea was to use a dictionary and store the subnames and see if we encounter one we have already visited. Since there should only be one match we can immediately return it. I learned a lot about using maps in Golang!
I am also using Python that I have more experience with to cross check solutions. I have tried to implement it with readability in mind, not performance.
Part 1:
Part 2: