Chris Smith 4 år sedan
förälder
incheckning
94539f0d78
Signerad av: Chris Smith <chris@chameth.com> GPG-nyckel ID: 3A2D4BBDC4A3C9A9
3 ändrade filer med 66 tillägg och 0 borttagningar
  1. 2
    0
      04/answers.txt
  2. 1
    0
      04/input.txt
  3. 63
    0
      04/main.go

+ 2
- 0
04/answers.txt Visa fil

@@ -0,0 +1,2 @@
1
+979
2
+635

+ 1
- 0
04/input.txt Visa fil

@@ -0,0 +1 @@
1
+256310-732736

+ 63
- 0
04/main.go Visa fil

@@ -0,0 +1,63 @@
1
+package main
2
+
3
+import (
4
+	"github.com/csmith/aoc-2019/common"
5
+	"strings"
6
+)
7
+
8
+func matchesRules(input int) (hasAnyRun bool, hasTwoRun bool, next int) {
9
+	last := 10
10
+	run := 0
11
+
12
+	for input > 0 {
13
+		digit := input % 10
14
+
15
+		if digit > last {
16
+			// Simple optimisation: if we hit a digit that's larger than the previous (running right-to-left) then
17
+			// we can predict the next possible number that might match. e.g. 1234111 => 1234444.
18
+			for input < 100000 {
19
+				input = 10*input + digit
20
+			}
21
+			return false, false, input
22
+		} else if digit == last {
23
+			run++
24
+		} else {
25
+			hasTwoRun = hasTwoRun || run == 1
26
+			hasAnyRun = hasAnyRun || run > 0
27
+			last = digit
28
+			run = 0
29
+		}
30
+
31
+		input = input / 10
32
+	}
33
+
34
+	hasTwoRun = hasTwoRun || run == 1
35
+	hasAnyRun = hasAnyRun || run > 0
36
+	return
37
+}
38
+
39
+func main() {
40
+	var (
41
+		input = strings.Split(common.ReadFileAsStrings("04/input.txt")[0], "-")
42
+		from  = common.MustAtoi(input[0])
43
+		to    = common.MustAtoi(input[1])
44
+		part1 = 0
45
+		part2 = 0
46
+	)
47
+
48
+	for i := from; i <= to; i++ {
49
+		p1, p2, n := matchesRules(i)
50
+		if p1 {
51
+			part1++
52
+		}
53
+		if p2 {
54
+			part2++
55
+		}
56
+		if n > 0 {
57
+			i = n - 1
58
+		}
59
+	}
60
+
61
+	println(part1)
62
+	println(part2)
63
+}

Laddar…
Avbryt
Spara