package main import ( "fmt" "os" "strings" ) func readFile(f string) [][]string { file, err := os.ReadFile(f) if err != nil { panic(err) } ret := [][]string{} for _, line := range strings.Split(string(file), "\n") { ret = append(ret, strings.Split(line, "")) } return ret } func lookAround(l [][]string, y int, x int) int { yToCheck := []int{(y-1), y, (y+1)} xToCheck := []int{(x-1), x, (x+1)} numNearby := 0 for _, curY := range yToCheck { if curY < 0 || curY > (len(l)-1) { continue } for _, curX := range xToCheck { if curX < 0 || curX > (len(l[curY])-1) { continue } if curX == x && curY == y { continue } if l[curY][curX] == "@" { numNearby++ } } } return numNearby } func removeRolls (l [][]string, toRemove [][]int) [][]string { for _, r := range toRemove { l[r[0]][r[1]] = "x" } return l } func runForklift (l [][]string) (int,[][]string) { rollsToRemove := [][]int{} numReachable := 0 for y := range l { for x := range l[y] { if l[y][x] != "@"{ continue } else if lookAround(l, y, x) < 4 { numReachable++ rollsToRemove = append(rollsToRemove, []int{y,x}) } } } return numReachable, removeRolls(l, rollsToRemove) } func partOne() (int) { input := readFile("input.txt") numReachable := 0 for y := range input { for x := range input[y] { if input[y][x] == "."{ continue } else if lookAround(input, y, x) < 4 { numReachable++ } } } return numReachable } func partTwo() int { input := readFile("input.txt") num := 0 numReachable := -1 for numReachable != 0 { if numReachable != -1 { num += numReachable } numReachable = 0 numReachable, input = runForklift(input) } num += numReachable return num } func main() { fmt.Println(partOne()) fmt.Println(partTwo()) }