Skip to content

Commit

Permalink
day 12 part 2 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
aleche28 committed Dec 12, 2024
1 parent 3ce2c25 commit 0b0fe7a
Showing 1 changed file with 27 additions and 46 deletions.
73 changes: 27 additions & 46 deletions solutions/alessio/day12/day12.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ func check(e error) {
}
}

func checkInBounds(cell Cell, rows int, cols int) bool {
return cell.r >= 0 && cell.r < rows && cell.c >= 0 && cell.c < cols
}

type Cell struct {
r, c int
}

func checkInBounds(cell Cell, rows int, cols int) bool {
return cell.r >= 0 && cell.r < rows && cell.c >= 0 && cell.c < cols
}

func dfs(curr Cell, lines []string, rows int, cols int, visited *map[Cell]bool, region *[]Cell) {
dirs := [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}

Expand Down Expand Up @@ -60,49 +60,34 @@ const (
W
)

type vertKey struct {
col, dir int
}

type horizKey struct {
row, dir int
type sideKey struct {
row, col, dir int
}

func countSides(r []Cell, grid []string, rows int, cols int) int {
symbol := grid[r[0].r][r[0].c]
dirs := [][]int{{-1, 0}, {0, 1}, {1, 0}, {0, -1}} // N, E, S, W
vertSides := make(map[vertKey][]int)
horizSides := make(map[horizKey][]int)
sides := make(map[sideKey][]int)
for _, cell := range r {
for d, dir := range dirs {
next := Cell{cell.r + dir[0], cell.c + dir[1]}
if !checkInBounds(next, rows, cols) || grid[next.r][next.c] != symbol {
// perimetral cell
if d == N {
horizSides[horizKey{cell.r, d}] = append(horizSides[horizKey{cell.r, d}], cell.c)
sides[sideKey{cell.r, -1, d}] = append(sides[sideKey{cell.r, -1, d}], cell.c)
} else if d == S {
horizSides[horizKey{cell.r + 1, d}] = append(horizSides[horizKey{cell.r + 1, d}], cell.c)
sides[sideKey{cell.r + 1, -1, d}] = append(sides[sideKey{cell.r + 1, -1, d}], cell.c)
} else if d == W {
vertSides[vertKey{cell.c, d}] = append(vertSides[vertKey{cell.c, d}], cell.r)
sides[sideKey{-1, cell.c, d}] = append(sides[sideKey{-1, cell.c, d}], cell.r)
} else {
vertSides[vertKey{cell.c + 1, d}] = append(vertSides[vertKey{cell.c + 1, d}], cell.r)
sides[sideKey{-1, cell.c + 1, d}] = append(sides[sideKey{-1, cell.c + 1, d}], cell.r)
}
}
}
}

cnt := 0
for _, v := range horizSides {
sort.Ints(v)
for i := 1; i < len(v); i++ {
if v[i]-v[i-1] > 1 {
cnt++
}
}
cnt++
}

for _, v := range vertSides {
for _, v := range sides {
sort.Ints(v)
for i := 1; i < len(v); i++ {
if v[i]-v[i-1] > 1 {
Expand All @@ -117,31 +102,27 @@ func countSides(r []Cell, grid []string, rows int, cols int) int {

func solve(grid []string, useSides bool) {
visited := make(map[Cell]bool)
regions := make([][]Cell, 0)
rows, cols := len(grid), len(grid[0])
for r := range rows {
for c := range cols {
res := 0

for r := 0; r < rows; r++ {
for c := 0; c < cols; c++ {
if !visited[Cell{r, c}] {
region := make([]Cell, 0)
region := []Cell{}
dfs(Cell{r, c}, grid, rows, cols, &visited, &region)
regions = append(regions, region)
area := len(region)
perimeter := 0
if !useSides {
perimeter = computePerimeter(region, grid, rows, cols)
} else {
perimeter = countSides(region, grid, rows, cols)
// fmt.Printf("- A region of %c plants with price %d * %d = %d\n", grid[region[0].r][region[0].c], area, perimeter, area*perimeter)
}
res += area * perimeter
}
}
}

res := 0
for _, r := range regions {
area := len(r)
perimeter := 0
if !useSides {
perimeter = computePerimeter(r, grid, rows, cols)
} else {
perimeter = countSides(r, grid, rows, cols)
fmt.Printf("- A region of %c plants with price %d * %d = %d\n", grid[r[0].r][r[0].c], area, perimeter, area*perimeter)
}
res += area * perimeter
}

fmt.Println(res)
}

Expand All @@ -154,7 +135,7 @@ func part2(grid []string) {
}

func main() {
data, err := os.ReadFile("./input12.txt")
data, err := os.ReadFile("./input12_def.txt")
check(err)
dataStr := strings.ReplaceAll(string(data), "\r\n", "\n")
lines := strings.Split(strings.Trim(dataStr, "\n"), "\n")
Expand Down

0 comments on commit 0b0fe7a

Please sign in to comment.