Browse Source

Day 2

master
Chris Smith 4 years ago
parent
commit
d1e4bdc6fe
Signed by: Chris Smith <chris@chameth.com> GPG Key ID: 3A2D4BBDC4A3C9A9
9 changed files with 135 additions and 13 deletions
  1. 0
    0
      .docker/Dockerfile
  2. 0
    0
      .docker/entrypoint.sh
  3. 3
    9
      01/main.go
  4. 2
    0
      02/answers.txt
  5. 1
    0
      02/input.txt
  6. 57
    0
      02/main.go
  7. 47
    3
      common/io.go
  8. 24
    0
      common/strings.go
  9. 1
    1
      run.sh

docker/Dockerfile → .docker/Dockerfile View File


docker/entrypoint.sh → .docker/entrypoint.sh View File


+ 3
- 9
01/main.go View File

@@ -3,25 +3,19 @@ package main
3 3
 import (
4 4
 	"fmt"
5 5
 	"github.com/csmith/aoc-2019/common"
6
-	"strconv"
7 6
 )
8 7
 
9 8
 func fuel(mass int) int {
10 9
 	return (mass / 3) - 2
11 10
 }
12 11
 
13
-func fuelRequirements(input []string) (int, int) {
12
+func fuelRequirements(input []int) (int, int) {
14 13
 	var (
15 14
 		simpleTotal = 0
16 15
 		recursiveTotal = 0
17 16
 	)
18 17
 
19
-	for _, line := range input {
20
-		mass, err := strconv.Atoi(line)
21
-		if err != nil {
22
-			panic(err)
23
-		}
24
-
18
+	for _, mass := range input {
25 19
 		required := fuel(mass)
26 20
 		simpleTotal += required
27 21
 		for required > 0 {
@@ -33,7 +27,7 @@ func fuelRequirements(input []string) (int, int) {
33 27
 }
34 28
 
35 29
 func main() {
36
-	input := common.ReadLines("01/input.txt")
30
+	input := common.Atoi(common.ReadLines("01/input.txt"))
37 31
 	part1, part2 := fuelRequirements(input)
38 32
 	fmt.Println(part1)
39 33
 	fmt.Println(part2)

+ 2
- 0
02/answers.txt View File

@@ -0,0 +1,2 @@
1
+3101844
2
+8478

+ 1
- 0
02/input.txt View File

@@ -0,0 +1 @@
1
+1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,6,1,19,1,5,19,23,2,9,23,27,1,6,27,31,1,31,9,35,2,35,10,39,1,5,39,43,2,43,9,47,1,5,47,51,1,51,5,55,1,55,9,59,2,59,13,63,1,63,9,67,1,9,67,71,2,71,10,75,1,75,6,79,2,10,79,83,1,5,83,87,2,87,10,91,1,91,5,95,1,6,95,99,2,99,13,103,1,103,6,107,1,107,5,111,2,6,111,115,1,115,13,119,1,119,2,123,1,5,123,0,99,2,0,14,0

+ 57
- 0
02/main.go View File

@@ -0,0 +1,57 @@
1
+package main
2
+
3
+import (
4
+	"fmt"
5
+	"github.com/csmith/aoc-2019/common"
6
+	"sync"
7
+)
8
+
9
+func run(pool sync.Pool, input []int, noun, verb int) int {
10
+	state := pool.Get().([]int)
11
+	defer pool.Put(state)
12
+	copy(state, input)
13
+	state[1] = noun
14
+	state[2] = verb
15
+
16
+	var ip = 0
17
+	for {
18
+		var instr = state[ip]
19
+		if instr == 1 {
20
+			state[state[ip+3]] = state[state[ip+1]] + state[state[ip+2]]
21
+		} else if instr == 2 {
22
+			state[state[ip+3]] = state[state[ip+1]] * state[state[ip+2]]
23
+		} else if instr == 99 {
24
+			return state[0]
25
+		}
26
+		ip += 4
27
+	}
28
+}
29
+
30
+func findOutput(pool sync.Pool, input []int, target int) int {
31
+	res := make(chan int)
32
+
33
+	for n := 0; n < 100; n++ {
34
+		go func(n int) {
35
+			for v := 0; v < 100; v++ {
36
+				if run(pool, input, n, v) == target {
37
+					res <- 100*n + v
38
+				}
39
+			}
40
+		}(n)
41
+	}
42
+
43
+	return <-res
44
+}
45
+
46
+func main() {
47
+	input := common.Atoi(common.ReadCommas("02/input.txt"))
48
+
49
+	pool := sync.Pool{
50
+		New: func() interface{} {
51
+			return make([]int, len(input))
52
+		},
53
+	}
54
+
55
+	fmt.Println(run(pool, input, 12, 2))
56
+	fmt.Println(findOutput(pool, input, 19690720))
57
+}

+ 47
- 3
common/io.go View File

@@ -3,11 +3,24 @@ package common
3 3
 import (
4 4
 	"bufio"
5 5
 	"os"
6
+	"unicode"
7
+	"unicode/utf8"
6 8
 )
7 9
 
8 10
 // ReadLines reads all lines from the given path and returns them in a slice of strings.
9 11
 // If an error occurs, the function will panic.
10 12
 func ReadLines(path string) []string {
13
+	return readWithScanner(path, bufio.ScanLines)
14
+}
15
+
16
+// ReadCommas reads all data from the given path and returns a string slice
17
+// containing comma-delimited parts. If a
18
+// If an error occurs, the function will panic.
19
+func ReadCommas(path string) []string {
20
+	return readWithScanner(path, scanByCommas)
21
+}
22
+
23
+func readWithScanner(path string, splitFunc bufio.SplitFunc) []string {
11 24
 	file, err := os.Open(path)
12 25
 	if err != nil {
13 26
 		panic(err)
@@ -16,15 +29,46 @@ func ReadLines(path string) []string {
16 29
 		_ = file.Close()
17 30
 	}()
18 31
 
19
-	var lines []string
32
+	var parts []string
20 33
 	scanner := bufio.NewScanner(file)
34
+	scanner.Split(splitFunc)
21 35
 	for scanner.Scan() {
22
-		lines = append(lines, scanner.Text())
36
+		parts = append(parts, scanner.Text())
23 37
 	}
24 38
 
25 39
 	if scanner.Err() != nil {
26 40
 		panic(scanner.Err())
27 41
 	}
28 42
 
29
-	return lines
43
+	return parts
44
+}
45
+
46
+// scanByCommas is a split function for a Scanner that returns each
47
+// comma-separated section of text, with surrounding spaces deleted.
48
+// The definition of space is set by unicode.IsSpace.
49
+func scanByCommas(data []byte, atEOF bool) (advance int, token []byte, err error) {
50
+	// Skip leading spaces.
51
+	start := 0
52
+	for width := 0; start < len(data); start += width {
53
+		var r rune
54
+		r, width = utf8.DecodeRune(data[start:])
55
+		if !unicode.IsSpace(r) {
56
+			break
57
+		}
58
+	}
59
+
60
+	// Scan until comma, marking end of word.
61
+	for width, i := 0, start; i < len(data); i += width {
62
+		var r rune
63
+		r, width = utf8.DecodeRune(data[i:])
64
+		if r == ',' || unicode.IsSpace(r) {
65
+			return i + width, data[start:i], nil
66
+		}
67
+	}
68
+	// If we're at EOF, we have a final, non-empty, non-terminated word. Return it.
69
+	if atEOF && len(data) > start {
70
+		return len(data), data[start:], nil
71
+	}
72
+	// Request more data.
73
+	return start, nil, nil
30 74
 }

+ 24
- 0
common/strings.go View File

@@ -0,0 +1,24 @@
1
+package common
2
+
3
+import (
4
+	"strconv"
5
+)
6
+
7
+// MustAtoi converts the input string to an integer and panics on failure.
8
+func MustAtoi(input string) int {
9
+	res, err := strconv.Atoi(input)
10
+	if err != nil {
11
+		panic(err)
12
+	}
13
+	return res
14
+}
15
+
16
+// Atoi converts a slice of strings to a slice of integers.
17
+// It panics if any element couldn't be converted.
18
+func Atoi(input []string) []int {
19
+	var result []int
20
+	for _, element := range input {
21
+		result = append(result, MustAtoi(element))
22
+	}
23
+	return result
24
+}

+ 1
- 1
run.sh View File

@@ -6,7 +6,7 @@ docker image inspect $IMAGE >/dev/null 2>&1
6 6
 if [ $? -ne 0 ]
7 7
 then
8 8
     echo "One time setup: building docker image..."
9
-    cd docker
9
+    cd .docker
10 10
     docker build . -t $IMAGE
11 11
     cd ..
12 12
 fi

Loading…
Cancel
Save