Skip to content

Commit aabe804

Browse files
committed
day04 p01 and p02
1 parent 7a45b55 commit aabe804

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

day04/day04_p01.go

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

day04/day04_p02.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)