Browse Source

Solve 2020/2/first

master
Garrit Franke 3 years ago
parent
commit
561ea715dd
  1. 3
      2020/2/first/go.mod
  2. 1000
      2020/2/first/input.txt
  3. 49
      2020/2/first/main.go

3
2020/2/first/go.mod

@ -0,0 +1,3 @@
module 2
go 1.15

1000
2020/2/first/input.txt

File diff suppressed because it is too large Load Diff

49
2020/2/first/main.go

@ -0,0 +1,49 @@
package main
import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)
func main() {
result := 0
data, err := ioutil.ReadFile("input.txt")
if err != nil {
fmt.Println("File reading error", err)
return
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
components := strings.FieldsFunc(string(line), split)
min, _ := strconv.Atoi(components[0])
max, _ := strconv.Atoi(components[1])
target := components[2]
password := components[3]
matches := 0
for _, letter := range strings.Split(password, "") {
if letter == target {
matches++
}
}
if matches >= min && matches <= max {
result++
}
matches = 0
}
fmt.Println(result)
}
func split(r rune) bool {
return r == '-' || r == ' ' || r == ':'
}
Loading…
Cancel
Save