aboutsummaryrefslogtreecommitdiff
path: root/2019/go/day04.go
diff options
context:
space:
mode:
authorj-james2022-12-12 02:15:44 +0000
committerj-james2022-12-12 02:18:55 +0000
commita7ba598a54a8883c83cb9ef2b04d653eecd92e28 (patch)
tree06d362d3a88c615d8747ddf9a961bf491a8575a5 /2019/go/day04.go
parent13007178188cd89b31e3e41090e8a8ed64867c13 (diff)
Reorganize 2019 solutions
Diffstat (limited to '2019/go/day04.go')
-rw-r--r--2019/go/day04.go68
1 files changed, 68 insertions, 0 deletions
diff --git a/2019/go/day04.go b/2019/go/day04.go
new file mode 100644
index 0000000..7c901dc
--- /dev/null
+++ b/2019/go/day04.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
+}