2021-12-13 20:53:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-12-16 21:11:39 +00:00
|
|
|
"AdventOfCode2021/shared"
|
2021-12-13 20:53:55 +00:00
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-12-14 22:37:28 +00:00
|
|
|
"strings"
|
2021-12-13 20:53:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
content := returnContent("../input")
|
2021-12-16 19:55:49 +00:00
|
|
|
//content := returnContent("../testInput")
|
2021-12-13 20:53:55 +00:00
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
sheet := make(map[shared.Coordinate]bool)
|
2021-12-14 22:37:28 +00:00
|
|
|
//var answer string
|
|
|
|
|
|
|
|
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])
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
sheet[shared.Coordinate{X: x, Y: y}] = true
|
2021-12-14 22:37:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for y := 0; y < 8; y++ {
|
|
|
|
for x := 0; x < 200; x++ {
|
2021-12-16 21:11:39 +00:00
|
|
|
if sheet[shared.Coordinate{X: x, Y: y}] {
|
2021-12-14 22:37:28 +00:00
|
|
|
fmt.Print("#")
|
|
|
|
} else {
|
|
|
|
fmt.Print(" ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Print("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
func FoldX(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
|
|
|
|
folded = make(map[shared.Coordinate]bool)
|
2021-12-14 22:37:28 +00:00
|
|
|
for mark := range sheet {
|
|
|
|
x := mark.X
|
|
|
|
if x > foldPoint {
|
|
|
|
//If the value is in the region that gets folded
|
|
|
|
x = 2*foldPoint - x
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
folded[shared.Coordinate{X: x, Y: mark.Y}] = true
|
2021-12-14 22:37:28 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
func FoldY(sheet map[shared.Coordinate]bool, foldPoint int) (folded map[shared.Coordinate]bool) {
|
|
|
|
folded = make(map[shared.Coordinate]bool)
|
2021-12-14 22:37:28 +00:00
|
|
|
for mark := range sheet {
|
2021-12-14 22:47:57 +00:00
|
|
|
y := mark.Y
|
2021-12-14 22:37:28 +00:00
|
|
|
if y > foldPoint {
|
|
|
|
//If the value is in the region that gets folded
|
|
|
|
y = 2*foldPoint - y
|
|
|
|
}
|
|
|
|
|
2021-12-16 21:11:39 +00:00
|
|
|
folded[shared.Coordinate{X: mark.X, Y: y}] = true
|
2021-12-14 22:37:28 +00:00
|
|
|
}
|
|
|
|
return
|
2021-12-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 22:37:28 +00:00
|
|
|
func returnContent(path string) *[]string {
|
2021-12-13 20:53:55 +00:00
|
|
|
//read file and return it as an array of integers
|
|
|
|
|
|
|
|
file, err := os.Open(path)
|
2021-12-14 22:37:28 +00:00
|
|
|
var content []string
|
2021-12-13 20:53:55 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unlucky, the file didn't open")
|
|
|
|
return &content
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
|
|
|
|
for scanner.Scan() {
|
2021-12-14 22:37:28 +00:00
|
|
|
content = append(content, scanner.Text())
|
2021-12-13 20:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &content
|
|
|
|
}
|