mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
day02 solution
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"adventofcode/util"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// brute force it...
|
||||
// assume only one valid answer
|
||||
func main() {
|
||||
input := util.ReadFile("../input.txt")
|
||||
lines := strings.Split(input, "\n")
|
||||
for i := 0; i < len(lines); i++ {
|
||||
for j := i + 1; j < len(lines); j++ {
|
||||
if sameChars := getSameCharacters(lines[i], lines[j]); sameChars != "" {
|
||||
fmt.Println("common letters are", sameChars)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getSameCharacters(str1, str2 string) string {
|
||||
var mismatchSeen bool
|
||||
var sameChars string
|
||||
for i := 0; i < len(str1); i++ {
|
||||
if str1[i] == str2[i] {
|
||||
sameChars += string(str1[i])
|
||||
} else if mismatchSeen {
|
||||
// if a mismatch has already been seen, then it's 2 characters off
|
||||
// return an empty string
|
||||
return ""
|
||||
} else {
|
||||
mismatchSeen = true
|
||||
}
|
||||
}
|
||||
return sameChars
|
||||
}
|
||||
Reference in New Issue
Block a user