day 5a complete
This commit is contained in:
3
day5/day 5a/go.mod
Normal file
3
day5/day 5a/go.mod
Normal file
@ -0,0 +1,3 @@
|
||||
module PWD
|
||||
|
||||
go 1.17
|
77
day5/day 5a/main.go
Normal file
77
day5/day 5a/main.go
Normal file
@ -0,0 +1,77 @@
|
||||
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))
|
||||
}
|
||||
|
||||
func run(content *[][]int, gameMap *[1000][1000]int) int {
|
||||
for i := 0; i < len((*content)); i++ {
|
||||
fillMap(gameMap, (*content)[i][0], (*content)[i][1], (*content)[i][2], (*content)[i][3])
|
||||
}
|
||||
|
||||
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, x1 int, y1 int, x2 int, y2 int) {
|
||||
|
||||
if x1 == x2 {
|
||||
for i := int(math.Min(float64(y1), float64(y2))); i <= int(math.Max(float64(y1), float64(y2))); i++ {
|
||||
(*gameMap)[i][x1]++
|
||||
}
|
||||
} else if y1 == y2 {
|
||||
//fill horizontal line
|
||||
for i := int(math.Min(float64(x1), float64(x2))); i <= int(math.Max(float64(x1), float64(x2))); i++ {
|
||||
(*gameMap)[y1][i]++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
10
day5/day 5a/testInput
Normal file
10
day5/day 5a/testInput
Normal file
@ -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
|
Reference in New Issue
Block a user