2018 day 5 done

This commit is contained in:
Brian Buller 2018-12-05 08:57:22 -06:00
parent 797bb1d62e
commit 8d07292616
4 changed files with 99 additions and 0 deletions

96
2018/day05/day05.go Normal file
View File

@ -0,0 +1,96 @@
package main
import (
"bufio"
"fmt"
"log"
"math"
"os"
"strconv"
"strings"
)
func main() {
inp := StdinToString()
fmt.Println("= Part 1 =")
fmt.Println(part1(inp))
fmt.Println("\n= Part 2 =")
fmt.Println(part2(inp))
}
func part1(inp string) int {
last := inp
curr := react(last)
for last != curr {
last = curr
curr = react(last)
}
return len(curr)
}
// useShortest will skip runs where there are fewer letters in the input than the current shortest...
// It reduced _my_ runtime by about 3 minutes, but may fail depending on your input
var useShortest bool
func part2(inp string) int {
// If yours fails, try setting this to false
useShortest = true
shortest := len(inp)
var shortestCount int
var count int
for i := 65; i <= 90; i++ {
var try string
if useShortest {
count = strings.Count(inp, string(i))
if count < shortestCount {
continue
}
}
for j := range inp {
if int(inp[j]) == i || int(inp[j]) == i+32 {
continue
}
try += string(inp[j])
}
v := part1(try)
if v < shortest {
shortest = v
shortestCount = count
}
}
return shortest
}
func react(v string) string {
var ret string
for i := 0; i < len(v); i++ {
if i < len(v)-1 && Abs(v[i], v[i+1]) == 32 {
i++
continue
}
ret += string(v[i])
}
return ret
}
func StdinToString() string {
var input string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input += scanner.Text()
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}
func Abs(val1, val2 byte) int {
return int(math.Abs(float64(val1) - float64(val2)))
}

1
2018/day05/go.mod Normal file
View File

@ -0,0 +1 @@
module day05

1
2018/day05/input Normal file

File diff suppressed because one or more lines are too long

1
2018/day05/testinput Normal file
View File

@ -0,0 +1 @@
dabAcCaCBAcCcaDA