Browse Source

Day 6

master
Jan-Lukas Else 4 months ago
parent
commit
ea17164b97
2 changed files with 2320 additions and 0 deletions
  1. +2248
    -0
      6/input.txt
  2. +72
    -0
      6/main.go

+ 2248
- 0
6/input.txt
File diff suppressed because it is too large
View File


+ 72
- 0
6/main.go View File

@ -0,0 +1,72 @@
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)
}

Loading…
Cancel
Save