Przeglądaj źródła

Able to read lenses

Piotr Czajkowski 1 rok temu
rodzic
commit
800cf4547a
1 zmienionych plików z 44 dodań i 1 usunięć
  1. 44 1
      15/code.go

+ 44 - 1
15/code.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"log"
 	"os"
+	"strconv"
 	"strings"
 )
 
@@ -25,11 +26,15 @@ func readInput(path string) []string {
 	return parts
 }
 
+const (
+	Max = 256
+)
+
 func hash(text string) int {
 	var current int
 	for i := range text {
 		current += int(text[i])
-		current = current * 17 % 256
+		current = current * 17 % Max
 	}
 
 	return current
@@ -44,6 +49,43 @@ func part1(steps []string) int {
 	return result
 }
 
+type Lens struct {
+	label string
+	power int
+}
+
+func getBoxes(steps []string) [][]Lens {
+	lenses := make([][]Lens, 256)
+	for i := range steps {
+		if strings.Contains(steps[i], "=") {
+			parts := strings.Split(steps[i], "=")
+			if len(parts) != 2 {
+				log.Fatalf("Problem reading step %s", steps[i])
+			}
+
+			lens := Lens{label: parts[0]}
+			n, err := strconv.Atoi(parts[1])
+			if err != nil {
+				log.Fatalf("Problem converting number %s: %s", parts[1], err)
+			}
+
+			lens.power = n
+			fmt.Println(lens)
+		} else {
+			label := strings.TrimRight(steps[i], "-")
+			fmt.Println(label)
+		}
+	}
+
+	return lenses
+}
+
+func part2(steps []string) int {
+	var result int
+	getBoxes(steps)
+	return result
+}
+
 func main() {
 	if len(os.Args) < 2 {
 		log.Fatal("You need to specify a file!")
@@ -51,4 +93,5 @@ func main() {
 
 	steps := readInput(os.Args[1])
 	fmt.Println("Part1:", part1(steps))
+	fmt.Println("Part2:", part2(steps))
 }