Created
December 13, 2021 13:20
-
-
Save almendar/2b9374e11239f43f075d6e81e19de206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"strconv" | |
"strings" | |
) | |
type dot struct { | |
x, y int | |
} | |
type fold struct { | |
lineNumer int | |
direction string | |
} | |
const FOLD_PREFIX = "fold along " | |
func readInput(input string) ([]fold, []dot) { | |
bytes, err := ioutil.ReadFile(input) | |
if err != nil { | |
log.Fatal(err) | |
} | |
folds := make([]fold, 0, 8) | |
dots := make([]dot, 0, 32) | |
readingFolds := false | |
for _, line := range strings.Split(string(bytes), "\n") { | |
if strings.TrimSpace(line) == "" { | |
readingFolds = true | |
continue | |
} | |
if readingFolds { | |
trimmed := strings.TrimPrefix(line, FOLD_PREFIX) | |
tmp := strings.Split(trimmed, "=") | |
fold := fold{} | |
fold.direction = tmp[0] | |
lineNumber, err := strconv.Atoi(tmp[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fold.lineNumer = lineNumber | |
folds = append(folds, fold) | |
} else { | |
tmp := strings.Split(line, ",") | |
x, err := strconv.Atoi(tmp[0]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
y, err := strconv.Atoi(tmp[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
dots = append(dots, dot{x, y}) | |
} | |
} | |
return folds, dots | |
} | |
func foldPaper(paper map[dot]bool, instruction fold) { | |
if instruction.direction == "x" { | |
for currDot := range paper { | |
if currDot.x > instruction.lineNumer { | |
dx := currDot.x - instruction.lineNumer | |
newX := instruction.lineNumer - dx | |
delete(paper, currDot) | |
newDot := dot{newX, currDot.y} | |
paper[newDot] = true | |
} | |
} | |
} | |
if instruction.direction == "y" { | |
for currDot := range paper { | |
if currDot.y > instruction.lineNumer { | |
dy := currDot.y - instruction.lineNumer | |
newY := instruction.lineNumer - dy | |
delete(paper, currDot) | |
newDot := dot{currDot.x, newY} | |
paper[newDot] = true | |
} | |
} | |
} | |
} | |
func printPaper(paper map[dot]bool) { | |
fmt.Println() | |
maxX := -1 | |
maxY := -1 | |
for d := range paper { | |
if d.x > maxX { | |
maxX = d.x | |
} | |
if d.y > maxY { | |
maxY = d.y | |
} | |
} | |
for y := 0; y <= maxY; y++ { | |
for x := 0; x <= maxX; x++ { | |
ok := paper[dot{x, y}] | |
if !ok { | |
fmt.Print(" ") | |
} else { | |
fmt.Print("()") | |
} | |
} | |
fmt.Println() | |
} | |
fmt.Println() | |
} | |
func task1(input string) { | |
folds, dots := readInput(input) | |
// fmt.Printf("%v %v\n", folds, dots) | |
paper := make(map[dot]bool) | |
for _, v := range dots { | |
paper[v] = true | |
} | |
for _, fold := range folds { | |
foldPaper(paper, fold) | |
} | |
// printPaper(paper) | |
fmt.Printf("Day13 task1 %s - %v\n", input, len(paper)) | |
} | |
func task2(input string) { | |
folds, dots := readInput(input) | |
// fmt.Printf("%v %v\n", folds, dots) | |
paper := make(map[dot]bool) | |
for _, v := range dots { | |
paper[v] = true | |
} | |
// printPaper(paper) | |
for _, fold := range folds { | |
foldPaper(paper, fold) | |
} | |
fmt.Printf("Day13 task2 %s -\n", input) | |
printPaper(paper) | |
} | |
func main() { | |
task1("example.txt") | |
task1("input-task1.txt") | |
task2("input-task2.txt") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment