From 04982c4798a25a56af95dfecb11b1906dd4e54c0 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Sun, 5 Dec 2021 16:36:56 +0000 Subject: [PATCH] day 5b complete Had a couple of issues drawing the diagonal lines, got the x and y coordinates on the array mixed up --- day5/day 5b/go.mod | 3 ++ day5/day 5b/main.go | 121 ++++++++++++++++++++++++++++++++++++++++++ day5/day 5b/testInput | 10 ++++ 3 files changed, 134 insertions(+) create mode 100644 day5/day 5b/go.mod create mode 100644 day5/day 5b/main.go create mode 100644 day5/day 5b/testInput diff --git a/day5/day 5b/go.mod b/day5/day 5b/go.mod new file mode 100644 index 0000000..e938e2e --- /dev/null +++ b/day5/day 5b/go.mod @@ -0,0 +1,3 @@ +module PWD + +go 1.17 diff --git a/day5/day 5b/main.go b/day5/day 5b/main.go new file mode 100644 index 0000000..9e3bccc --- /dev/null +++ b/day5/day 5b/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "bufio" + "fmt" + "math" + "os" + "regexp" + "strconv" +) + +func main() { + content := returnContent("../input") + //content := returnContent("testInput") + + gameMap := [1000][1000]int{} + + fmt.Println(run(content, &gameMap)) + + for i := 0; i < len(gameMap); i++ { + fmt.Println(gameMap[i]) + } +} + +func run(content *[][]int, gameMap *[1000][1000]int) int { + for i := 0; i < len((*content)); i++ { + fillMap(gameMap, (*content)[i]) + } + + count := 0 + + for i := 0; i < len(gameMap); i++ { + for j := 0; j < len(gameMap[i]); j++ { + if gameMap[i][j] > 1 { + count++ + } + } + } + return count +} + +func fillMap(gameMap *[1000][1000]int, coords []int) { + + coord1 := []int{coords[0], coords[1]} + coord2 := []int{coords[2], coords[3]} + + if coord1[0] > coord2[0] { + //swap values so coord1 is far left value + coord1, coord2 = coord2, coord1 + } + + //4 types of line + + //diagonal up, + //diagonal down, + //horizontal + //vertical () + + //Check initially for vertical and horizontal lines + + if coord1[0] == coord2[0] { + //Continue down vertical line + for i := int(math.Min(float64(coord1[1]), float64(coord2[1]))); i <= int(math.Max(float64(coord1[1]), float64(coord2[1]))); i++ { + (*gameMap)[i][coord1[0]]++ + } + } else if coord1[1] == coord2[1] { + //fill horizontal line in + for i := coord1[0]; i <= coord2[0]; i++ { + (*gameMap)[coord1[1]][i]++ + } + } else { + //Now check for diagonal lines + + if coord1[1] <= coord2[1] { + //Diagonal up and to the right + height := coord1[1] + + for i := coord1[0]; i <= coord2[0]; i++ { + (*gameMap)[height][i]++ + height++ + } + } else { + //Diagonal down and to the right + height := coord1[1] + + for i := coord1[0]; i <= coord2[0]; i++ { + (*gameMap)[height][i]++ + height-- + } + } + + } + +} + +func returnContent(path string) *[][]int { + //read file and return it as an array of integers + + file, err := os.Open(path) + var content [][]int + + if err != nil { + fmt.Println("Unlucky, the file didn't open") + return &content + } + defer file.Close() + + scanner := bufio.NewScanner(file) + regex := regexp.MustCompile("([0-9]+)") + for scanner.Scan() { + stringValues := regex.FindAllString(scanner.Text(), 4) + intValues := []int{} + for _, v := range stringValues { + num, _ := strconv.Atoi(v) + intValues = append(intValues, num) + } + content = append(content, intValues) + } + + return &content +} diff --git a/day5/day 5b/testInput b/day5/day 5b/testInput new file mode 100644 index 0000000..1d4e36d --- /dev/null +++ b/day5/day 5b/testInput @@ -0,0 +1,10 @@ +0,9 -> 5,9 +8,0 -> 0,8 +9,4 -> 3,4 +2,2 -> 2,1 +7,0 -> 7,4 +6,4 -> 2,0 +0,9 -> 2,9 +3,4 -> 1,4 +0,0 -> 8,8 +5,5 -> 8,2 \ No newline at end of file