adventofcode/2018/day02/day02.go

77 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-12-02 05:21:34 +00:00
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
}
2018-12-02 05:25:41 +00:00
var diff, diffIdx int
2018-12-02 05:21:34 +00:00
for k := range v1 {
if v1[k] != v2[k] {
diff++
2018-12-02 05:25:41 +00:00
diffIdx = k
2018-12-02 05:21:34 +00:00
}
if diff > 1 {
continue
}
}
if diff == 1 {
2018-12-02 05:25:41 +00:00
fmt.Println(v1[:diffIdx] + v1[diffIdx+1:])
2018-12-02 05:21:34 +00:00
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
}