This commit is contained in:
Gregory 2022-05-05 19:39:59 +03:00
parent 3656193dfa
commit b79f1a5d06
4 changed files with 195 additions and 0 deletions

59
d13/d13_test.go Normal file
View file

@ -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)
}

81
d13/p1.go Normal file
View file

@ -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)
}

52
d13/p2.go Normal file
View file

@ -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)
}

View file

@ -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)))
}