adventofcode/2018/day02/day02.go

77 lines
1.1 KiB
Go

package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func main() {
inp := StdinToStringSlice()
part1(inp)
part2(inp)
}
func part1(inp []string) {
var twos, threes int
for _, v := range inp {
var vintwos, vinthrees bool
for i := range v {
if !vintwos && strings.Count(v, string(v[i])) == 2 {
twos++
vintwos = true
}
if !vinthrees && strings.Count(v, string(v[i])) == 3 {
threes++
vinthrees = true
}
}
}
fmt.Println(twos * threes)
}
func part2(inp []string) {
for _, v1 := range inp {
for _, v2 := range inp {
if v1 == v2 {
continue
}
var diff, diffIdx int
for k := range v1 {
if v1[k] != v2[k] {
diff++
diffIdx = k
}
if diff > 1 {
continue
}
}
if diff == 1 {
fmt.Println(v1[:diffIdx] + v1[diffIdx+1:])
return
}
}
}
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(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
}