From 1d9b8196d9638a03844cbd1d2fe98da8554ce9a6 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sun, 25 Feb 2024 15:40:20 -0500 Subject: [PATCH] Day 2 part 1 --- main.go | 3 +++ solutions/day02.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 solutions/day02.go diff --git a/main.go b/main.go index 3ba9719..ebb6772 100644 --- a/main.go +++ b/main.go @@ -8,4 +8,7 @@ import ( func main() { fmt.Println("Day 01 part 1:\t", solutions.Day01Part01(false)) fmt.Println("Day 01 part 2:\t", solutions.Day01Part02(false)) + + fmt.Println("Day 02 part 1:\t", solutions.Day02Part01(false)) + fmt.Println("Day 02 part 2:\t", solutions.Day02Part02(false)) } diff --git a/solutions/day02.go b/solutions/day02.go new file mode 100644 index 0000000..08d5b79 --- /dev/null +++ b/solutions/day02.go @@ -0,0 +1,60 @@ +package solutions + +import ( + "strconv" + "strings" +) + +func checkPassword(input string) bool { + dashPosition := strings.Index(input, "-") + spacePosition := strings.Index(input, " ") + colonPosition := strings.Index(input, ":") + + amountMin, err := strconv.Atoi(input[:dashPosition]) + if err != nil { + panic("Error converting to int") + } + amountMax, err := strconv.Atoi(input[dashPosition+1 : spacePosition]) + if err != nil { + panic("Error converting to int") + } + + letter := input[spacePosition+1 : colonPosition] + rest := input[colonPosition+2:] + + charMap := make(map[string]int) + + for _, char := range strings.Split(rest, "") { + if _, ok := charMap[char]; ok { + charMap[char] += 1 + } else { + charMap[char] = 1 + } + } + + charAmount := charMap[letter] + + if charAmount >= amountMin && charAmount <= amountMax { + return true + } + + return false +} + +func Day02Part01(isTest bool) int { + input := ReadInput("02", isTest) + values := strings.Split(input, "\n") + correctPasswords := 0 + + for _, i := range values { + if checkPassword(i) { + correctPasswords += 1 + } + } + + return correctPasswords +} + +func Day02Part02(isTest bool) int { + return -1 +}