Compare commits
4 Commits
8dde43f7ca
...
84cced41a5
| Author | SHA1 | Date | |
|---|---|---|---|
| 84cced41a5 | |||
| 42b41dc56d | |||
| 0b691ea9af | |||
| 49529a1de3 |
3
2020/day02/go.mod
Normal file
3
2020/day02/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module 2020/day02
|
||||
|
||||
go 1.25.4
|
||||
1000
2020/day02/input.txt
Normal file
1000
2020/day02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
82
2020/day02/main.go
Normal file
82
2020/day02/main.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseInput(file string) []string {
|
||||
content, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read input file: %v", err)
|
||||
}
|
||||
lines := strings.Split(string(content), "\n")
|
||||
var data []string
|
||||
for _, line := range lines {
|
||||
data = append(data, line)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func PartOne(data []string) int {
|
||||
valid := 0
|
||||
for _, line := range data {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(line, ": ")
|
||||
policy := parts[0]
|
||||
password := parts[1]
|
||||
|
||||
policyParts := strings.Split(policy, " ")
|
||||
rangeStr := policyParts[0]
|
||||
letter := policyParts[1]
|
||||
|
||||
rangeParts := strings.Split(rangeStr, "-")
|
||||
min, _ := strconv.Atoi(rangeParts[0])
|
||||
max, _ := strconv.Atoi(rangeParts[1])
|
||||
|
||||
count := strings.Count(password, letter)
|
||||
if count >= min && count <= max {
|
||||
valid++
|
||||
}
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
func PartTwo(data []string) int {
|
||||
valid := 0
|
||||
for _, line := range data {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
parts := strings.Split(line, ": ")
|
||||
policy := parts[0]
|
||||
password := parts[1]
|
||||
|
||||
policyParts := strings.Split(policy, " ")
|
||||
rangeStr := policyParts[0]
|
||||
letter := policyParts[1]
|
||||
|
||||
rangeParts := strings.Split(rangeStr, "-")
|
||||
firstPosition, _ := strconv.Atoi(rangeParts[0])
|
||||
secondPosition, _ := strconv.Atoi(rangeParts[1])
|
||||
|
||||
atFirstPosition := len(password) >= firstPosition && password[firstPosition-1] == letter[0]
|
||||
atSecondPosition := len(password) >= secondPosition && password[secondPosition-1] == letter[0]
|
||||
|
||||
if atFirstPosition != atSecondPosition {
|
||||
valid++
|
||||
}
|
||||
}
|
||||
return valid
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := parseInput("input.txt")
|
||||
fmt.Println("Part 1:", PartOne(data))
|
||||
fmt.Println("Part 2:", PartTwo(data))
|
||||
}
|
||||
29
2020/day02/main_test.go
Normal file
29
2020/day02/main_test.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestPartOne(t *testing.T) {
|
||||
input := []string{
|
||||
"1-3 a: abcde",
|
||||
"1-3 b: cdefg",
|
||||
"2-9 c: ccccccccc",
|
||||
}
|
||||
expected := 2
|
||||
got := PartOne(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne(%v) = %d, want %d", input, got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPartTwo(t *testing.T) {
|
||||
input := []string{
|
||||
"1-3 a: abcde",
|
||||
"1-3 b: cdefg",
|
||||
"2-9 c: ccccccccc",
|
||||
}
|
||||
expected := 1
|
||||
got := PartTwo(input)
|
||||
if got != expected {
|
||||
t.Errorf("PartOne(%v) = %d, want %d", input, got, expected)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user