mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-05-18 19:13:27 +02:00
2015-day05: string validation
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var part int
|
||||||
|
flag.IntVar(&part, "part", 1, "part 1 or 2")
|
||||||
|
flag.Parse()
|
||||||
|
fmt.Println("Running part", part)
|
||||||
|
|
||||||
|
var ans int
|
||||||
|
if part == 1 {
|
||||||
|
ans = part1(util.ReadFile("./input.txt"))
|
||||||
|
} else {
|
||||||
|
ans = part2(util.ReadFile("./input.txt"))
|
||||||
|
}
|
||||||
|
fmt.Println("Output:", ans)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(input string) int {
|
||||||
|
var nice int
|
||||||
|
|
||||||
|
disallowPattern := regexp.MustCompile("(ab|cd|pq|xy)")
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
|
var vowels int
|
||||||
|
for _, char := range line {
|
||||||
|
if strings.ContainsRune("aeiou", char) {
|
||||||
|
vowels++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var hasDouble bool
|
||||||
|
for i := 0; i < len(line)-1; i++ {
|
||||||
|
if line[i] == line[i+1] {
|
||||||
|
hasDouble = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if vowels >= 3 && !disallowPattern.MatchString(line) && hasDouble {
|
||||||
|
nice++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nice
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(input string) int {
|
||||||
|
var nice int
|
||||||
|
|
||||||
|
// put a double for loop check inside of a separate function b/c it makes
|
||||||
|
// returning out of both loops possible, and avoids using a label which
|
||||||
|
// makes me sad
|
||||||
|
passesRule1 := func(line string) bool {
|
||||||
|
for i := 0; i < len(line)-2; i++ {
|
||||||
|
toMatch := line[i : i+2]
|
||||||
|
for j := i + 2; j < len(line)-1; j++ {
|
||||||
|
if line[j:j+2] == toMatch {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, line := range strings.Split(input, "\n") {
|
||||||
|
rule1 := passesRule1(line)
|
||||||
|
|
||||||
|
var rule2 bool
|
||||||
|
for i := 0; i < len(line)-2; i++ {
|
||||||
|
if line[i] == line[i+2] {
|
||||||
|
rule2 = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if rule1 && rule2 {
|
||||||
|
nice++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nice
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_part1(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"actual", util.ReadFile("input.txt"), 238},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := part1(tt.input); got != tt.want {
|
||||||
|
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_part2(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
input string
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"actual", util.ReadFile("input.txt"), 69},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := part2(tt.input); got != tt.want {
|
||||||
|
t.Errorf("part2() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user