summaryrefslogtreecommitdiff
path: root/2024/go/src/day15/main.go
blob: becf95f53e1d5c273d931772a0400c9c1e2cc862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main

import (
	"fmt"
	"os"
	"strings"
)

func FileRead(path string) string {
	file, err := os.ReadFile(path)
	if err != nil {
		fmt.Println("Couldn't Read file: ", err)
	}
	return string(file)
}

type Grid = [][]byte

type Point struct {
	x int
	y int
}

type Input struct {
	warehouse  Grid
	directions []byte
}

func getRobotsPos(warehouse Grid) Point {
	for i := 0; i < len(warehouse); i++ {
		for j := 0; j < len(warehouse[0]); j++ {
			if warehouse[i][j] == '@' {
				return Point{i, j}
			}
		}
	}
	return Point{0, 0}
}

func parseInput(data string) Input {
	splitted_data := strings.Split(data, "\n\n")
	directions := []byte(splitted_data[1])
	var warehouse Grid
	for _, line := range strings.Split(splitted_data[0], "\n") {
		row := []byte(line)
		warehouse = append(warehouse, row)
	}
	return Input{
		warehouse:  warehouse,
		directions: directions,
	}
}

func printDirs(dirs []byte) {
	for _, dir := range dirs {
		fmt.Printf(string(dir))
	}
}

func printGrid(grid [][]byte) {
	for _, row := range grid {
		for _, cell := range row {
			fmt.Printf(string(cell))
		}
		fmt.Println()
	}
}

func inBounds(x, y, w, h int) bool {
	return x >= 0 && x < w && y >= 0 && y < h
}

func isBlock(x, y int, warehouse Grid) bool {
	return warehouse[x][y] == 'O'
}

func isWall(x, y int, warehouse Grid) bool {
	return warehouse[x][y] == '#'
}

func simulate(input Input) Grid {
	dirMap := map[byte]Point{
		'<': {0, -1},
		'>': {0, 1},
		'^': {-1, 0},
		'v': {1, 0},
	}
	w, h := len(input.warehouse), len(input.warehouse[0])
	robotPos := getRobotsPos(input.warehouse)
	for _, dir := range input.directions {
		dx, dy := dirMap[dir].x, dirMap[dir].y
		nx, ny := robotPos.x+dx, robotPos.y+dy
		// printGrid(input.warehouse)
		if inBounds(nx, ny, w, h) && !isWall(nx, ny, input.warehouse) {
			if input.warehouse[nx][ny] == '.' {
				input.warehouse[nx][ny], input.warehouse[robotPos.x][robotPos.y] = input.warehouse[robotPos.x][robotPos.y], input.warehouse[nx][ny]
				robotPos.x = nx
				robotPos.y = ny
			} else if isBlock(nx, ny, input.warehouse) {
				step := 0
				for isBlock(nx+dx*step, ny+dy*step, input.warehouse) {
					step++
				}
				if !isWall(nx+dx*step, ny+dy*step, input.warehouse) {
					for i := step; i >= 1; i-- {
						input.warehouse[nx+dx*(i-1)][ny+dy*(i-1)], input.warehouse[nx+dx*(i)][ny+dy*(i)] = input.warehouse[nx+dx*(i)][ny+dy*(i)], input.warehouse[nx+dx*(i-1)][ny+dy*(i-1)]
					}
					input.warehouse[nx][ny], input.warehouse[robotPos.x][robotPos.y] = input.warehouse[robotPos.x][robotPos.y], input.warehouse[nx][ny]
					robotPos.x = nx
					robotPos.y = ny
				}
			}
		}
	}
	return input.warehouse
}

func sumGpsCoordinates(warehouse Grid) int {
	ans := 0
	for i, row := range warehouse {
		for j, cell := range row {
			if cell == 'O' {
				ans += i*100 + j
			}
		}
	}
	return ans
}

func solve_part_one(data string) int {
	input := parseInput(data)
	// printGrid(input.warehouse)
	// printDirs(input.directions)
	// fmt.Println()
	warehosue := simulate(input)
	return sumGpsCoordinates(warehosue)
}

func solve_part_two(data string) int {
	return 42
}

func main() {
	test := FileRead("../input/day15_1.test")
	prod := FileRead("../input/day15.prod")
	fmt.Println("Part_1 test: ", solve_part_one(test))
	fmt.Println("Part_1 prod: ", solve_part_one(prod))
	// fmt.Println("Part_2 test: ", solve_part_two(test))
	// fmt.Println("Part_2 prod: ", solve_part_two(test))
}