Created
December 5, 2018 07:17
-
-
Save mvmaasakkers/a02e33b9f354b7b18100aab1e5078246 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"flag" | |
"fmt" | |
"log" | |
"os" | |
"strings" | |
"unicode" | |
) | |
func readInput(filename string) string { | |
fileHandle, _ := os.Open(filename) | |
defer func() { | |
if err := fileHandle.Close(); err != nil { | |
log.Fatal(err) | |
} | |
}() | |
fileScanner := bufio.NewScanner(fileHandle) | |
input := "" | |
for fileScanner.Scan() { | |
line := fileScanner.Text() | |
if len(line) > 0 { | |
input = line | |
} | |
} | |
return strings.TrimSpace(input) | |
} | |
var file = flag.String("file", "./p1.txt", "file used for input") | |
func main() { | |
flag.Parse() | |
input := readInput(*file) | |
fmt.Println("Part 1:", part1(input)) | |
fmt.Println("Part 2:", part2(input)) | |
} | |
func part1(input string) (int) { | |
return len(produce(input)) | |
} | |
func produce(line string) string { | |
for { | |
changes := false | |
for k, g := range line { | |
if k > 0 { | |
if unicode.IsLower(g) && unicode.IsUpper(rune(line[k-1])) || unicode.IsLower(rune(line[k-1])) && unicode.IsUpper(g) { | |
if strings.ToLower(string(g)) == strings.ToLower(string(line[k-1])) { | |
line = line[:k-1] + line[k+1:] | |
changes = true | |
} | |
} | |
} | |
if changes { | |
break | |
} | |
} | |
if !changes { | |
break | |
} | |
} | |
return line | |
} | |
var alphabet = "abcdefghijklmnopqrstuvwxyz" | |
func part2(input string) (outcome int) { | |
outcome = len(input) | |
for _, c := range alphabet { | |
check := strings.Replace(strings.Replace(input, string(strings.ToUpper(string(c))), "", -1), string(c), "", -1) | |
l := len(produce(check)) | |
if l < outcome { | |
outcome = l | |
} | |
} | |
return outcome | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment