Compare commits
4 Commits
a0ce63e5a5
...
8ad1b166f3
| Author | SHA1 | Date | |
|---|---|---|---|
| 8ad1b166f3 | |||
| 1f3b42b266 | |||
| 9521677ca8 | |||
| 8938992384 |
@@ -8,6 +8,7 @@ import (
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
||||
_ "advent-of-code/internal/2015/DayFive"
|
||||
_ "advent-of-code/internal/2015/DayFour"
|
||||
_ "advent-of-code/internal/2015/DayOne"
|
||||
_ "advent-of-code/internal/2015/DayThree"
|
||||
|
||||
77
internal/2015/DayFive/code.go
Normal file
77
internal/2015/DayFive/code.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package dayfive
|
||||
|
||||
import (
|
||||
"advent-of-code/internal/registry"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
registry.Register("2015D5", ParseInput, PartOne, PartTwo)
|
||||
}
|
||||
|
||||
func ParseInput(filepath string) []string {
|
||||
content, _ := os.ReadFile(filepath)
|
||||
return strings.Split(string(content), "\n")
|
||||
}
|
||||
|
||||
func PartOne(data []string) int {
|
||||
count := 0
|
||||
for _, line := range data {
|
||||
vowelCount := strings.Count(line, "a") + strings.Count(line, "e") + strings.Count(line, "i") + strings.Count(line, "o") + strings.Count(line, "u")
|
||||
if vowelCount < 3 {
|
||||
continue
|
||||
}
|
||||
|
||||
hasDoubleLetter := false
|
||||
for i := 1; i < len(line); i++ {
|
||||
if line[i] == line[i-1] {
|
||||
hasDoubleLetter = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasDoubleLetter {
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.Contains(line, "ab") || strings.Contains(line, "cd") || strings.Contains(line, "pq") || strings.Contains(line, "xy") {
|
||||
continue
|
||||
}
|
||||
|
||||
count++
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func PartTwo(data []string) int {
|
||||
count := 0
|
||||
for _, line := range data {
|
||||
if len(line) < 4 {
|
||||
continue
|
||||
}
|
||||
|
||||
hasNonOverlappingPair := false
|
||||
for i := 0; i < len(line)-1; i++ {
|
||||
pair := line[i : i+2]
|
||||
if strings.Contains(line[i+2:], pair) {
|
||||
hasNonOverlappingPair = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasNonOverlappingPair {
|
||||
continue
|
||||
}
|
||||
|
||||
hasRepeatingLetterWithGap := false
|
||||
for i := 0; i < len(line)-2; i++ {
|
||||
if line[i] == line[i+2] {
|
||||
hasRepeatingLetterWithGap = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if hasRepeatingLetterWithGap {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
32
internal/2015/DayFive/code_test.go
Normal file
32
internal/2015/DayFive/code_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package dayfive
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
input := []string{
|
||||
"ugknbfddgicrmopn",
|
||||
"aaa",
|
||||
"jchzalrnumimnmhp",
|
||||
"haegwjzuvuyypxyu",
|
||||
"dvszwmarrgswjxmb",
|
||||
}
|
||||
expected := 2
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
input := []string{
|
||||
"qjhvhtzxzqqjkmpb",
|
||||
"xxyxx",
|
||||
"uurcxstgmygtbstg",
|
||||
"ieodomkazucvgmuy",
|
||||
}
|
||||
expected := 2
|
||||
got := PartTwo(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartTwo() = %d, want %d", got, expected)
|
||||
}
|
||||
}
|
||||
1000
internal/data/2015/DayFive/input.txt
Normal file
1000
internal/data/2015/DayFive/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user