Skip to content

Instantly share code, notes, and snippets.

@shanesaravia
Created September 28, 2020 18:58
Show Gist options
  • Save shanesaravia/9f4728d0e1928587a34522395e85fb44 to your computer and use it in GitHub Desktop.
Save shanesaravia/9f4728d0e1928587a34522395e85fb44 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strconv"
"strings"
)
func conv_to_intslice(puzzle_input string) []int {
// Split into slice
str_code_range := strings.Split(puzzle_input, "-")
// convert to ints
code_range := []int{}
for _, i := range str_code_range {
j, err := strconv.Atoi(i)
if err != nil {
panic(err)
}
code_range = append(code_range, j)
}
return code_range
}
func check_rules(code int) bool {
// convert to string
val_str := strconv.Itoa(code)
adjacent_count := 1
rule_adjacent := false
var last int
for pos, digit_char := range val_str {
digit, _ := strconv.Atoi(string(digit_char))
// Set first
if pos == 0 {
last = digit
continue
}
if digit < last {
return false
} else if digit == last {
adjacent_count++
} else {
if adjacent_count == 2 {
rule_adjacent = true
} else {
adjacent_count = 1
}
}
last = digit
}
if rule_adjacent || adjacent_count == 2 {
return true
} else {
return false
}
}
func filter_code(puzzle_input string) {
possible_matches := 0
code_range := conv_to_intslice(puzzle_input)
for code := code_range[0]; code < code_range[1]; code++ {
if check_rules(code) {
possible_matches++
}
}
fmt.Println(possible_matches)
}
func main() {
var puzzle_input = "178416-676461"
filter_code(puzzle_input)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment