From 6ac55cbfdffd36aa337ff0a0b6c643b0148c3138 Mon Sep 17 00:00:00 2001 From: j-james Date: Tue, 23 Jun 2020 11:54:52 -0700 Subject: Day Four --- four.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 four.go diff --git a/four.go b/four.go new file mode 100644 index 0000000..7c901dc --- /dev/null +++ b/four.go @@ -0,0 +1,68 @@ +package main + +import ( + "fmt" + "io/ioutil" + "math" + "os" + "strconv" +) + +func main() { + smallest, largest, total, details := -1, -1, 0, 0 + if len(os.Args) < 2 { + panic("runtime error: missing operand") + } + file, err := ioutil.ReadFile(os.Args[1]) + if err != nil { + panic(err) + } + for i := 0; i < len(file); i++ { + if string(file[i]) == "-" { + smallest, err = strconv.Atoi(string(file[0:i])) + if err != nil { + panic(err) + } + largest, err = strconv.Atoi(string(file[i+1 : len(file)-1])) + if err != nil { + panic(err) + } + break + } + } + + for i := smallest; i <= largest; i++ { + if criteria(i, false) { + total++ + } + if criteria(i, true) { + details++ + } + } + fmt.Println(total) + fmt.Println(details) +} + +func criteria(password int, stage bool) bool { + prev := 0 + for i := 0; i < 6; i++ { + if password/int(math.Pow(10, float64(5-i)))%10 < prev { + return false + } + prev = password / int(math.Pow(10, float64(5-i))) % 10 + } + pascii := strconv.Itoa(password) + for i := 0; i < len(pascii)-1; i++ { + if string(pascii[i]) == string(pascii[i+1]) { + if !(len(pascii)-i > 2 && string(pascii[i]) == string(pascii[i+2])) { + if !stage { // Gross hack + return true + } + if !(i > 0 && string(pascii[i]) == string(pascii[i-1])) { + return true + } + } + } + } + return false +} -- cgit v1.2.3-70-g09d2