From b79f1a5d0683033bf937b7da7527549d3d42e56e Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 5 May 2022 19:39:59 +0300 Subject: [PATCH] d13 --- d13/d13_test.go | 59 +++++++++++++++++++++++++++++++++++ d13/p1.go | 81 +++++++++++++++++++++++++++++++++++++++++++++++++ d13/p2.go | 52 +++++++++++++++++++++++++++++++ main.go | 3 ++ 4 files changed, 195 insertions(+) create mode 100644 d13/d13_test.go create mode 100644 d13/p1.go create mode 100644 d13/p2.go diff --git a/d13/d13_test.go b/d13/d13_test.go new file mode 100644 index 0000000..4765603 --- /dev/null +++ b/d13/d13_test.go @@ -0,0 +1,59 @@ +package d13 + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestP1(t *testing.T) { + result := P1(`6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5`) + + assert.EqualValues(t, 17, result) +} + +func TestP2(t *testing.T) { + result := P2(`6,10 +0,14 +9,10 +0,3 +10,4 +4,11 +6,0 +6,12 +4,1 +0,13 +10,12 +3,4 +3,0 +8,4 +1,10 +2,14 +8,10 +9,0 + +fold along y=7 +fold along x=5`) + + assert.EqualValues(t, 16, result) +} diff --git a/d13/p1.go b/d13/p1.go new file mode 100644 index 0000000..f6a469e --- /dev/null +++ b/d13/p1.go @@ -0,0 +1,81 @@ +package d13 + +import ( + "strconv" + "strings" +) + +type point struct { + x int + y int +} + +type fold struct { + line int + axis string +} + +type paper map[point]bool + +func getPaperAndFolds(in string) (paper, []fold) { + paper := make(paper, 0) + folds := make([]fold, 0) + + parts := strings.Split(strings.TrimSpace(in), "\n\n") + + for _, pnt := range strings.Split(parts[0], "\n") { + xy := strings.Split(pnt, ",") + x, _ := strconv.Atoi(xy[0]) + y, _ := strconv.Atoi(xy[1]) + + paper[point{x, y}] = true + } + + for _, fld := range strings.Split(parts[1], "\n") { + dirAndLine := strings.Split(strings.Split(fld, " ")[2], "=") + line, _ := strconv.Atoi(dirAndLine[1]) + + folds = append(folds, fold{line, dirAndLine[0]}) + } + + return paper, folds +} + +func foldUp(paper paper, y int) paper { + for pnt := range paper { + if pnt.y > y { + newY := y - (pnt.y - y) + delete(paper, pnt) + paper[point{pnt.x, newY}] = true + } + } + + return paper +} + +func foldLeft(paper paper, x int) paper { + for pnt := range paper { + if pnt.x > x { + newX := x - (pnt.x - x) + delete(paper, pnt) + paper[point{newX, pnt.y}] = true + } + } + + return paper +} + +func P1(in string) int { + paper, folds := getPaperAndFolds(in) + + for _, fold := range folds { + if fold.axis == "y" { + paper = foldUp(paper, fold.line) + } else { + paper = foldLeft(paper, fold.line) + } + break + } + + return len(paper) +} diff --git a/d13/p2.go b/d13/p2.go new file mode 100644 index 0000000..74eb6aa --- /dev/null +++ b/d13/p2.go @@ -0,0 +1,52 @@ +package d13 + +import "fmt" + +func P2(in string) int { + paper, folds := getPaperAndFolds(in) + + for _, fold := range folds { + if fold.axis == "y" { + paper = foldUp(paper, fold.line) + } else { + paper = foldLeft(paper, fold.line) + } + } + + xMax := 0 + yMax := 0 + + for point := range paper { + if point.x > xMax { + xMax = point.x + } + + if point.y > yMax { + yMax = point.y + } + } + + lines := make([][]string, yMax+1) + + for y := range lines { + lines[y] = make([]string, xMax+1) + } + + for point := range paper { + lines[point.y][point.x] = "#" + } + + for _, line := range lines { + for _, dot := range line { + if dot != "" { + fmt.Print("#") + } else { + fmt.Print(" ") + + } + } + fmt.Println() + } + + return len(paper) +} diff --git a/main.go b/main.go index 6343d02..9aa0020 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "github.com/fotonmoton/aoc2021/d10" "github.com/fotonmoton/aoc2021/d11" "github.com/fotonmoton/aoc2021/d12" + "github.com/fotonmoton/aoc2021/d13" "github.com/fotonmoton/aoc2021/d2" "github.com/fotonmoton/aoc2021/d3" "github.com/fotonmoton/aoc2021/d4" @@ -55,4 +56,6 @@ func main() { fmt.Printf("day 11 part 2: %v\n", d11.P2(client.Day(11))) fmt.Printf("day 12: %v\n", d12.P1(client.Day(12))) fmt.Printf("day 12 part 2: %v\n", d12.P2(client.Day(12))) + fmt.Printf("day 13: %v\n", d13.P1(client.Day(13))) + fmt.Printf("day 13 part 2: %v\n", d13.P2(client.Day(13))) }