From 27f270dd4ac2272e5af1947c647f6d3c63c23d20 Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 14 Dec 2021 22:37:28 +0000 Subject: [PATCH] day 13b complete took a while, output was a bit... broken :( --- day13/day 13b/main.go | 92 +++++++++++++++++++++++++++++++++++++++---- day13/input | 1 - 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/day13/day 13b/main.go b/day13/day 13b/main.go index 2c4f028..d55e5dc 100644 --- a/day13/day 13b/main.go +++ b/day13/day 13b/main.go @@ -5,19 +5,100 @@ import ( "fmt" "os" "strconv" + "strings" ) func main() { content := returnContent("../input") //content := returnContent("testInput") + sheet := make(map[Coordinate]bool) + //var answer string + + for y := 0; y < 1000; y++ { + for x := 0; x < 1000; x++ { + sheet[Coordinate{X: x, Y: y}] = false + } + } + + for _, line := range *content { + + if strings.HasPrefix(line, "fold") { + //Fold instructions + instruction := strings.Split(line, "=") + + if strings.Contains(instruction[0], "x") { + foldPoint, _ := strconv.Atoi(instruction[1]) + sheet = FoldX(sheet, foldPoint) + } + + if strings.Contains(instruction[0], "y") { + foldPoint, _ := strconv.Atoi(instruction[1]) + sheet = FoldY(sheet, foldPoint) + } + + } else if line != "" { + //mapping instructions + coordinates := strings.Split(line, ",") + x, _ := strconv.Atoi(coordinates[0]) + y, _ := strconv.Atoi(coordinates[1]) + + sheet[Coordinate{X: x, Y: y}] = true + + } + } + + for y := 0; y < 8; y++ { + for x := 0; x < 200; x++ { + if sheet[Coordinate{X: x, Y: y}] { + fmt.Print("#") + } else { + fmt.Print(" ") + } + } + fmt.Print("\n") + } + } -func returnContent(path string) *[][]int { +func FoldX(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { + folded = make(map[Coordinate]bool) + for mark := range sheet { + x := mark.X + if x > foldPoint { + //If the value is in the region that gets folded + x = 2*foldPoint - x + } + + folded[Coordinate{X: x, Y: mark.Y}] = true + } + return +} + +func FoldY(sheet map[Coordinate]bool, foldPoint int) (folded map[Coordinate]bool) { + folded = make(map[Coordinate]bool) + for mark := range sheet { + y := mark.X + if y > foldPoint { + //If the value is in the region that gets folded + y = 2*foldPoint - y + } + + folded[Coordinate{X: mark.X, Y: y}] = true + } + return +} + +type Coordinate struct { + X int + Y int +} + +func returnContent(path string) *[]string { //read file and return it as an array of integers file, err := os.Open(path) - var content [][]int + var content []string if err != nil { fmt.Println("Unlucky, the file didn't open") @@ -28,12 +109,7 @@ func returnContent(path string) *[][]int { scanner := bufio.NewScanner(file) for scanner.Scan() { - nums := []int{} - for _, char := range scanner.Text() { - num, _ := strconv.Atoi(string(char)) - nums = append(nums, num) - } - content = append(content, nums) + content = append(content, scanner.Text()) } return &content diff --git a/day13/input b/day13/input index ff66650..4efcf29 100644 --- a/day13/input +++ b/day13/input @@ -926,7 +926,6 @@ 723,325 833,773 211,415 - fold along x=655 fold along y=447 fold along x=327