summaryrefslogtreecommitdiff
path: root/2024/go/src/day14/main.go
blob: b9441d8df1c50db91d2026d32944ae36c3a2a756 (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
151
152
153
154
155
156
157
158
159
160
161
package main

import (
	"fmt"
	"os"
	"strconv"
	"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 IVec2 struct {
	x int
	y int
}

type Robot struct {
	Pos      IVec2
	Velocity IVec2
}

func parseInput(data string) []Robot {
	var robots []Robot
	for _, line := range strings.Split(data, "\n") {
		var pos IVec2
		var vel IVec2
		if len(line) > 0 {
			splitted_line := strings.Split(line, " ")
			position := strings.Split(splitted_line[0][2:], ",")
			velcocity := strings.Split(splitted_line[1][2:], ",")
			pos.x, _ = strconv.Atoi(position[1])
			pos.y, _ = strconv.Atoi(position[0])
			vel.x, _ = strconv.Atoi(velcocity[1])
			vel.y, _ = strconv.Atoi(velcocity[0])
			robots = append(robots, Robot{Pos: pos, Velocity: vel})
		}
	}

	return robots
}

type Grid = [][]int

func mod(a, b int) int {
	r := a % b
	if r < 0 {
		r += b
	}
	return r
}

func printGrid(grid [][]int) {
	for _, row := range grid {
		for i, cell := range row {
			if i != len(row)-1 {
				fmt.Printf("%d,", cell)
			} else {
				fmt.Printf("%d", cell)
			}
		}
		fmt.Println()
	}
}

func plotMap(robots []Robot, height, width int) Grid {
	grid := make([][]int, width)
	for i := range grid {
		grid[i] = make([]int, height)
	}
	for i := 0; i < width; i++ {
		for j := 0; j < height; j++ {
			grid[i][j] = 0
		}
	}
	for _, robot := range robots {
		grid[robot.Pos.x][robot.Pos.y] += 1
	}
	return grid
}

func plotNewMap(grid Grid, robots []Robot, height, width, seconds int) Grid {
	for _, robot := range robots {
		grid[robot.Pos.x][robot.Pos.y] -= 1
		newPos := IVec2{x: mod(robot.Pos.x+robot.Velocity.x*seconds, width),
			y: mod(robot.Pos.y+robot.Velocity.y*seconds, height)}
		grid[newPos.x][newPos.y] += 1
	}
	return grid
}

func countQuadrants(grid Grid) int64 {
	w, h := len(grid), len(grid[0])
	quadrants := [4]int{0, 0, 0, 0}
	for i := 0; i < w; i++ {
		if i == w/2 {
			continue
		}
		for j := 0; j < h; j++ {
			if j == h/2 {
				continue
			}
			row := 0
			if i >= w/2 {
				row = 1
			}
			col := 0
			if j >= h/2 {
				col = 1
			}
			quadrantIndex := row*2 + col
			quadrants[quadrantIndex] += grid[i][j]
		}
	}
	var ans int64 = 1
	for _, cnt := range quadrants {
		ans *= int64(cnt)
	}
	return ans

}

func solve_part_one(data string) int64 {
	robots := parseInput(data)
	// oldMap := plotMap(robots, 11, 7)
	// newMap := plotNewMap(oldMap, robots, 11, 7, 100)
	oldMap := plotMap(robots, 101, 103)
	newMap := plotNewMap(oldMap, robots, 101, 103, 100)
	ans := countQuadrants(newMap)
	return ans
}

func solve_part_two(data string) int64 {
	robots := parseInput(data)
	// oldMap := plotMap(robots, 11, 7)
	// newMap := plotNewMap(oldMap, robots, 11, 7, 100)
	oldMap := plotMap(robots, 101, 103)
	// newMap := plotNewMap(oldMap, robots, 101, 103, 100)
	for i := 1; i <= 100; i++ {
		newMap := plotNewMap(oldMap, robots, 101, 103, i)
		printGrid(newMap)
		fmt.Println()
	}
	// ans := countQuadrants(newMap)
	return 42

}

func main() {
	// test := FileRead("../input/day14.test")
	prod := FileRead("../input/day14.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(prod))
}