From 83dde11bd7d6dfe2c1d1af72e90256aeb6a70759 Mon Sep 17 00:00:00 2001 From: omagdy Date: Sun, 15 Dec 2024 17:51:30 +0200 Subject: Day 15 part 1 done. --- 2024/go/input/day15_1.test | 21 +++++++ 2024/go/input/day15_2.test | 10 +++ 2024/go/src/day15/main.go | 150 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 181 insertions(+) create mode 100644 2024/go/input/day15_1.test create mode 100644 2024/go/input/day15_2.test create mode 100644 2024/go/src/day15/main.go (limited to '2024') diff --git a/2024/go/input/day15_1.test b/2024/go/input/day15_1.test new file mode 100644 index 0000000..84cf1fb --- /dev/null +++ b/2024/go/input/day15_1.test @@ -0,0 +1,21 @@ +########## +#..O..O.O# +#......O.# +#.OO..O.O# +#..O@..O.# +#O#..O...# +#O..O..O.# +#.OO.O.OO# +#....O...# +########## + +^v>^vv^v>v<>v^v<<><>>v^v^>^<<<><^ +vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<^<^^>>>^<>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^v^^<^^vv< +<>^^^^>>>v^<>vvv^>^^^vv^^>v<^^^^v<>^>vvvv><>>v^<<^^^^^ +^><^><>>><>^^<<^^v>>><^^>v>>>^v><>^v><<<>vvvv>^<><<>^>< +^>><>^v<><^vvv<^^<><^v<<<><<<^^<^>>^<<<^>>^v^>>^v>vv>^<<^v<>><<><<>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^ +<><^^>^^^<>^vv<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<> +^^>vv<^v^v^<>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<>< +v^^>>><<^^<>>^v^v^<<>^<^v^v><^<<<><<^vv>>v>v^<<^ diff --git a/2024/go/input/day15_2.test b/2024/go/input/day15_2.test new file mode 100644 index 0000000..8163605 --- /dev/null +++ b/2024/go/input/day15_2.test @@ -0,0 +1,10 @@ +######## +#..O.O.# +##@.O..# +#...O..# +#.#.O..# +#...O..# +#......# +######## + +<^^>>>vv>v<< diff --git a/2024/go/src/day15/main.go b/2024/go/src/day15/main.go new file mode 100644 index 0000000..becf95f --- /dev/null +++ b/2024/go/src/day15/main.go @@ -0,0 +1,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)) +} -- cgit v1.2.3