mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 20:53:30 +02:00
day13 cleanup
This commit is contained in:
+35
-60
@@ -28,18 +28,12 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("Running part", part)
|
fmt.Println("Running part", part)
|
||||||
|
|
||||||
if part == 1 {
|
ans := transparentOrigamiDay13(input, part)
|
||||||
ans := part1(input)
|
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
fmt.Println("Output:", ans)
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
} else {
|
|
||||||
ans := part2(input)
|
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func part1(input string) int {
|
func transparentOrigamiDay13(input string, part int) int {
|
||||||
parts := strings.Split(input, "\n\n")
|
parts := strings.Split(input, "\n\n")
|
||||||
|
|
||||||
coords := map[[2]int]bool{}
|
coords := map[[2]int]bool{}
|
||||||
@@ -48,19 +42,6 @@ func part1(input string) int {
|
|||||||
sp := strings.Split(line, ",")
|
sp := strings.Split(line, ",")
|
||||||
coords[[2]int{cast.ToInt(sp[0]), cast.ToInt(sp[1])}] = true
|
coords[[2]int{cast.ToInt(sp[0]), cast.ToInt(sp[1])}] = true
|
||||||
}
|
}
|
||||||
fmt.Println(len(coords))
|
|
||||||
|
|
||||||
// // printing is a pita
|
|
||||||
// grid := make([][]int, 15)
|
|
||||||
// for i := range grid {
|
|
||||||
// grid[i] = make([]int, 15)
|
|
||||||
// }
|
|
||||||
// for c := range coords {
|
|
||||||
// grid[c[1]][c[0]] = 1
|
|
||||||
// }
|
|
||||||
// for _, row := range grid {
|
|
||||||
// fmt.Println(row)
|
|
||||||
// }
|
|
||||||
|
|
||||||
for _, fold := range strings.Split(parts[1], "\n") {
|
for _, fold := range strings.Split(parts[1], "\n") {
|
||||||
cap := regexp.MustCompile(`fold along (x|y)=(\d+)`).FindStringSubmatch(fold)
|
cap := regexp.MustCompile(`fold along (x|y)=(\d+)`).FindStringSubmatch(fold)
|
||||||
@@ -70,9 +51,6 @@ func part1(input string) int {
|
|||||||
dir := cap[0]
|
dir := cap[0]
|
||||||
foldCoord := cast.ToInt(cap[1])
|
foldCoord := cast.ToInt(cap[1])
|
||||||
|
|
||||||
fmt.Println("folding on", dir, foldCoord)
|
|
||||||
|
|
||||||
// fmt.Println(fold, dir, line)
|
|
||||||
// dots will never appear exactly on a fold line
|
// dots will never appear exactly on a fold line
|
||||||
isFoldOnX := dir == "x"
|
isFoldOnX := dir == "x"
|
||||||
nextMap := map[[2]int]bool{}
|
nextMap := map[[2]int]bool{}
|
||||||
@@ -83,7 +61,6 @@ func part1(input string) int {
|
|||||||
foldCoord - (c[0] - foldCoord),
|
foldCoord - (c[0] - foldCoord),
|
||||||
c[1],
|
c[1],
|
||||||
}
|
}
|
||||||
fmt.Println(c, "to", folded)
|
|
||||||
nextMap[folded] = true
|
nextMap[folded] = true
|
||||||
} else {
|
} else {
|
||||||
nextMap[c] = true
|
nextMap[c] = true
|
||||||
@@ -97,7 +74,6 @@ func part1(input string) int {
|
|||||||
c[0],
|
c[0],
|
||||||
foldCoord - (c[1] - foldCoord),
|
foldCoord - (c[1] - foldCoord),
|
||||||
}
|
}
|
||||||
fmt.Println(c, "to", folded)
|
|
||||||
nextMap[folded] = true
|
nextMap[folded] = true
|
||||||
} else {
|
} else {
|
||||||
nextMap[c] = true
|
nextMap[c] = true
|
||||||
@@ -107,43 +83,42 @@ func part1(input string) int {
|
|||||||
|
|
||||||
coords = nextMap
|
coords = nextMap
|
||||||
|
|
||||||
// TODO just break for part 1
|
// return after one fold for part 1?
|
||||||
// break
|
if part == 1 {
|
||||||
}
|
return len(coords)
|
||||||
|
|
||||||
// printing is a pita
|
|
||||||
max := 0
|
|
||||||
for c := range coords {
|
|
||||||
if c[0] > max {
|
|
||||||
max = c[0]
|
|
||||||
}
|
|
||||||
if c[1] > max {
|
|
||||||
max = c[1]
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
grid := make([][]int, max+1)
|
// printing is a pita but necessary for reading part2
|
||||||
for i := range grid {
|
if part == 2 {
|
||||||
grid[i] = make([]int, max+1)
|
max := 0
|
||||||
}
|
for c := range coords {
|
||||||
for c := range coords {
|
if c[0] > max {
|
||||||
grid[c[1]][c[0]] = 1
|
max = c[0]
|
||||||
}
|
}
|
||||||
for _, row := range grid {
|
if c[1] > max {
|
||||||
str := ""
|
max = c[1]
|
||||||
for _, val := range row {
|
|
||||||
if val == 1 {
|
|
||||||
str += "#"
|
|
||||||
} else {
|
|
||||||
str += "."
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Println(str)
|
|
||||||
|
grid := make([][]int, max+1)
|
||||||
|
for i := range grid {
|
||||||
|
grid[i] = make([]int, max+1)
|
||||||
|
}
|
||||||
|
for c := range coords {
|
||||||
|
grid[c[1]][c[0]] = 1
|
||||||
|
}
|
||||||
|
for _, row := range grid {
|
||||||
|
str := ""
|
||||||
|
for _, val := range row {
|
||||||
|
if val == 1 {
|
||||||
|
str += "#"
|
||||||
|
} else {
|
||||||
|
str += "."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println(str)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(coords)
|
|
||||||
}
|
|
||||||
|
|
||||||
func part2(input string) int {
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-33
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,53 +27,30 @@ var example = `6,10
|
|||||||
fold along y=7
|
fold along y=7
|
||||||
fold along x=5`
|
fold along x=5`
|
||||||
|
|
||||||
func Test_part1(t *testing.T) {
|
func Test_transparentOrigamiDay13(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
|
part int
|
||||||
want int
|
want int
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "example",
|
name: "example",
|
||||||
input: example,
|
input: example,
|
||||||
|
part: 1,
|
||||||
want: 17,
|
want: 17,
|
||||||
},
|
},
|
||||||
// {
|
|
||||||
// name: "actual",
|
|
||||||
// input: input,
|
|
||||||
// want: 0,
|
|
||||||
// },
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got := part1(tt.input); got != tt.want {
|
|
||||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_part2(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
{
|
{
|
||||||
name: "example",
|
name: "actual",
|
||||||
input: example,
|
part: 1,
|
||||||
want: 0,
|
input: input,
|
||||||
|
want: 731,
|
||||||
},
|
},
|
||||||
// {
|
|
||||||
// name: "actual",
|
|
||||||
// input: input,
|
|
||||||
// want: 0,
|
|
||||||
// },
|
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(fmt.Sprintf("%s part%d", tt.name, tt.part), func(t *testing.T) {
|
||||||
if got := part2(tt.input); got != tt.want {
|
if got := transparentOrigamiDay13(tt.input, tt.part); got != tt.want {
|
||||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
t.Errorf("transparentOrigamiDay13() = %v, want %v", got, tt.want)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user