1
Fork 0
This repository has been archived on 2020-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
AdventOfCode2020/6/main.go

73 lines
1.1 KiB
Go

package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
type void struct{}
yes := void{}
group := map[rune]void{}
count := 0
newGroup := func() {
count += len(group)
group = map[rune]void{}
}
file, _ := os.Open("input.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if len(text) == 0 {
// Empty row, new group
newGroup()
} else {
for _, r := range strings.ReplaceAll(text, " ", "") {
group[r] = yes
}
}
}
// Last group
newGroup()
fmt.Println("Part 1:", count)
// Part 2
count = 0
isNewGroup := true
file2, _ := os.Open("input.txt")
defer file2.Close()
scanner = bufio.NewScanner(file2)
for scanner.Scan() {
text := scanner.Text()
if len(text) == 0 {
// Empty row, new group
newGroup()
isNewGroup = true
} else {
if isNewGroup {
// First person
for _, r := range strings.ReplaceAll(text, " ", "") {
group[r] = yes
}
isNewGroup = false
} else {
for r := range group {
if !strings.ContainsRune(text, r) {
delete(group, r)
}
}
}
}
}
newGroup()
fmt.Println("Part 2:", count)
}