|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | +) |
| 8 | + |
| 9 | +func doubleExact(p []rune) bool { |
| 10 | + de := false |
| 11 | + last := p[0] |
| 12 | + count := 1 |
| 13 | + freq := []int{} |
| 14 | + for i := 1; i < len(p); i++ { |
| 15 | + if p[i] == last { |
| 16 | + count++ |
| 17 | + } else { |
| 18 | + freq = append(freq, count) |
| 19 | + last = p[i] |
| 20 | + count = 1 |
| 21 | + } |
| 22 | + } |
| 23 | + freq = append(freq, count) |
| 24 | + for _, k := range freq { |
| 25 | + if k == 2 { |
| 26 | + de = true |
| 27 | + } |
| 28 | + } |
| 29 | + return de |
| 30 | +} |
| 31 | + |
| 32 | +func noDecreasing(p []rune) bool { |
| 33 | + nodec := false |
| 34 | + if p[0] <= p[1] && p[1] <= p[2] && p[2] <= p[3] && |
| 35 | + p[3] <= p[4] && p[4] <= p[5] { |
| 36 | + nodec = true |
| 37 | + } |
| 38 | + return nodec |
| 39 | +} |
| 40 | + |
| 41 | +func inLimits(p []rune) bool { |
| 42 | + inlim := false |
| 43 | + pass, err := strconv.Atoi(string(p)) |
| 44 | + if err != nil { |
| 45 | + log.Fatal(err) |
| 46 | + } |
| 47 | + if pass >= 183564 && pass <= 657474 { |
| 48 | + inlim = true |
| 49 | + } |
| 50 | + // Your puzzle input is 183564-657474 |
| 51 | + return inlim |
| 52 | +} |
| 53 | + |
| 54 | +func main() { |
| 55 | + f := "188888" |
| 56 | + first := []rune(f) |
| 57 | + count := 0 |
| 58 | + for i0 := first[0]; i0 <= rune(54); i0++ { |
| 59 | + for i1 := i0; i1 <= rune(57); i1++ { |
| 60 | + for i2 := i1; i2 <= rune(57); i2++ { |
| 61 | + for i3 := i2; i3 <= rune(57); i3++ { |
| 62 | + for i4 := i3; i4 <= rune(57); i4++ { |
| 63 | + for i5 := i4; i5 <= rune(57); i5++ { |
| 64 | + p := []rune{i0, i1, i2, i3, i4, i5} |
| 65 | + double := doubleExact(p) |
| 66 | + inlim := inLimits(p) |
| 67 | + nodec := noDecreasing(p) |
| 68 | + if double && inlim && nodec { |
| 69 | + count++ |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fmt.Println("count: ", count) |
| 79 | +} |
| 80 | + |
| 81 | +// 0 -> ascii decimal 48 |
| 82 | +// ... |
| 83 | +// 9 -> ascii decimal 57 |
| 84 | + |
| 85 | +// Your puzzle input is 183564-657474 |
| 86 | +// Find how many passwords meet the criteria: |
| 87 | +// 1. It is a six-digit number. |
| 88 | +// 2. The value is within the range given in your puzzle input. |
| 89 | +// 3. Two adjacent digits are the same (like 22 in 122345). |
| 90 | +// 4. Going from left to right, the digits never decrease; they only ever |
| 91 | +// increase or stay the same (like 111123 or 135679). |
| 92 | +// |
| 93 | +// Other than the range rule, the following are true: |
| 94 | +// 111111 meets these criteria (double 11, never decreases). |
| 95 | +// 223450 does not meet these criteria (decreasing pair of digits 50). |
| 96 | +// 123789 does not meet these criteria (no double). |
| 97 | +// |
| 98 | +// How many different passwords within the range given in your puzzle input |
| 99 | +// meet these criteria? |
0 commit comments