Compare commits
4 Commits
021d16cfd7
...
497314aa8b
| Author | SHA1 | Date | |
|---|---|---|---|
| 497314aa8b | |||
| ce1cc9d5cf | |||
| b2f1a2902c | |||
| de59203640 |
3
2020/day06/go.mod
Normal file
3
2020/day06/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module 2020/day06
|
||||
|
||||
go 1.25.4
|
||||
2147
2020/day06/input.txt
Normal file
2147
2020/day06/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
45
2020/day06/main.go
Normal file
45
2020/day06/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseInput(file string) []string {
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read input file: %v", err)
|
||||
}
|
||||
return strings.Split(strings.TrimSpace(string(content)), "\n")
|
||||
}
|
||||
|
||||
func PartOne(input []string) int {
|
||||
total := 0
|
||||
groupAnswers := make(map[rune]bool)
|
||||
|
||||
for i, line := range input {
|
||||
if line != "" {
|
||||
for _, char := range line {
|
||||
if char >= 'a' && char <= 'z' {
|
||||
groupAnswers[char] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if line == "" || i == len(input)-1 {
|
||||
total += len(groupAnswers)
|
||||
groupAnswers = make(map[rune]bool)
|
||||
}
|
||||
}
|
||||
|
||||
total += len(groupAnswers)
|
||||
|
||||
return total
|
||||
}
|
||||
|
||||
func main() {
|
||||
input := parseInput("input.txt")
|
||||
fmt.Println("Part 1:", PartOne(input))
|
||||
}
|
||||
29
2020/day06/main_test.go
Normal file
29
2020/day06/main_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
var input = []string{
|
||||
"abc",
|
||||
"",
|
||||
"a",
|
||||
"b",
|
||||
"c",
|
||||
"",
|
||||
"ab",
|
||||
"ac",
|
||||
"",
|
||||
"a",
|
||||
"a",
|
||||
"a",
|
||||
"a",
|
||||
"",
|
||||
"b",
|
||||
}
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
expected := 11
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user