From fd2aafd01808aa66d902fe5dd5c472a4727094ba Mon Sep 17 00:00:00 2001 From: ayoubzulfiqar Date: Fri, 13 Dec 2024 23:43:23 -0800 Subject: [PATCH] DayFourteen --- 2024/Go/Day14/part_1.go | 87 +++++++++++++++++++++++++++++++++ 2024/Go/Day14/part_2.go | 96 +++++++++++++++++++++++++++++++++++++ 2024/Python/Day14/part_1.py | 85 ++++++++++++++++++++++++++++++++ 2024/Python/Day14/part_2.py | 83 ++++++++++++++++++++++++++++++++ 4 files changed, 351 insertions(+) create mode 100644 2024/Go/Day14/part_1.go create mode 100644 2024/Go/Day14/part_2.go create mode 100644 2024/Python/Day14/part_1.py create mode 100644 2024/Python/Day14/part_2.py diff --git a/2024/Go/Day14/part_1.go b/2024/Go/Day14/part_1.go new file mode 100644 index 0000000..473162f --- /dev/null +++ b/2024/Go/Day14/part_1.go @@ -0,0 +1,87 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +type Bot struct { + x int + y int + dx int + dy int +} + +// const ( +// maxX = 101 +// maxY = 103 +// ) + +func HundredSecondSafetyFactor() { + var bots []Bot + + // Open the input file + file, err := os.Open("input.txt") + if err != nil { + fmt.Println("Error opening file:", err) + return + } + defer file.Close() + + // Read the lines from the file + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + var b Bot + // Parse the line into Bot structure (updated format: p= v=) + _, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &b.x, &b.y, &b.dx, &b.dy) + if err != nil { + fmt.Println("Error parsing line:", err) + continue + } + bots = append(bots, b) + } + + // Move the bots for 100 iterations + for i := 0; i < 100; i++ { + for j := 0; j < len(bots); j++ { + bots[j].x += bots[j].dx + bots[j].y += bots[j].dy + + // Wrap around the edges + if bots[j].x < 0 { + bots[j].x += maxX + } else if bots[j].x >= maxX { + bots[j].x -= maxX + } + + if bots[j].y < 0 { + bots[j].y += maxY + } else if bots[j].y >= maxY { + bots[j].y -= maxY + } + } + } + + // Count how many bots are in each quadrant + q1, q2, q3, q4 := 0, 0, 0, 0 + for _, bot := range bots { + if bot.x < maxX/2 && bot.y < maxY/2 { + q1++ + } + if bot.x > maxX/2 && bot.y < maxY/2 { + q2++ + } + if bot.x < maxX/2 && bot.y > maxY/2 { + q3++ + } + if bot.x > maxX/2 && bot.y > maxY/2 { + q4++ + } + } + + // Output the result + out := q1 * q2 * q3 * q4 + fmt.Println(out) +} diff --git a/2024/Go/Day14/part_2.go b/2024/Go/Day14/part_2.go new file mode 100644 index 0000000..623482c --- /dev/null +++ b/2024/Go/Day14/part_2.go @@ -0,0 +1,96 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +type Bots struct { + x int + y int + dx int + dy int +} + +const ( + maxX = 101 + maxY = 103 +) + +func robotsToEasterEgg() { + var bots []Bots + + // Open the input file + file, err := os.Open("input.txt") + if err != nil { + fmt.Println("Error opening file:", err) + return + } + defer file.Close() + + // Read the lines from the file + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + var bs Bots + // Parse the line into Bot structure (updated format: p= v=) + _, err := fmt.Sscanf(line, "p=%d,%d v=%d,%d", &bs.x, &bs.y, &bs.dx, &bs.dy) + if err != nil { + fmt.Println("Error parsing line:", err) + continue + } + bots = append(bots, bs) + } + + // Loop for 100,000 iterations + for i := 0; i < 1e5; i++ { + // Initialize the grid and fill with '.' + var grid [maxY][maxX]rune + for y := range grid { + for x := range grid[y] { + grid[y][x] = '.' + } + } + + distinct := true + + // Update bot positions and check for collisions + for j := 0; j < len(bots); j++ { + bots[j].x += bots[j].dx + bots[j].y += bots[j].dy + + // Wrap around the edges + if bots[j].x < 0 { + bots[j].x += maxX + } else if bots[j].x >= maxX { + bots[j].x -= maxX + } + + if bots[j].y < 0 { + bots[j].y += maxY + } else if bots[j].y >= maxY { + bots[j].y -= maxY + } + + // Mark the grid cell + if grid[bots[j].y][bots[j].x] == '.' { + grid[bots[j].y][bots[j].x] = '#' + } else { + distinct = false + } + } + + // If all bots were distinct, print the grid + if distinct { + // in my case first Iter was the answer + fmt.Printf("\nIter: %d\n", i+1) + // for _, row := range grid { + // for _, c := range row { + // fmt.Print(string(c)) + // } + // fmt.Println() + // } + } + } +} diff --git a/2024/Python/Day14/part_1.py b/2024/Python/Day14/part_1.py new file mode 100644 index 0000000..5576167 --- /dev/null +++ b/2024/Python/Day14/part_1.py @@ -0,0 +1,85 @@ +class Bot: + def __init__(self, x, y, dx, dy): + self.x = x + self.y = y + self.dx = dx + self.dy = dy + + +def hundred_second_safety_factor(): + bots = [] + + # Open the input file + try: + with open("input.txt", "r") as file: + for line in file: + line = line.strip() # Remove leading/trailing whitespace + if line == "": # Skip empty lines + continue + + # Parse the line into Bot object + try: + # Extract parts for position and velocity + parts = line.split(" ") + if len(parts) == 2: + # Extract x,y for position (p=) and dx,dy for velocity (v=) + position = parts[0][2:] # Remove "p=" + velocity = parts[1][2:] # Remove "v=" + + # Extract integers from the position and velocity strings + x, y = map(int, position.split(",")) + dx, dy = map(int, velocity.split(",")) + + # Create a Bot instance and append it + bot = Bot(x, y, dx, dy) + bots.append(bot) + else: + print( + f"Skipping invalid line (does not contain both position and velocity): {line}" + ) + except ValueError as e: + # Catch the case where map(int, ...) fails (invalid number format) + print(f"Error parsing line (invalid number format): {line} - {e}") + except Exception as e: + print("Error opening file:", e) + return + + # Move the bots for 100 iterations + max_x = 101 + max_y = 103 + + for i in range(100): + for bot in bots: + bot.x += bot.dx + bot.y += bot.dy + + # Wrap around the edges + if bot.x < 0: + bot.x += max_x + elif bot.x >= max_x: + bot.x -= max_x + + if bot.y < 0: + bot.y += max_y + elif bot.y >= max_y: + bot.y -= max_y + + # Count how many bots are in each quadrant + q1, q2, q3, q4 = 0, 0, 0, 0 + for bot in bots: + if bot.x < max_x / 2 and bot.y < max_y / 2: + q1 += 1 + if bot.x > max_x / 2 and bot.y < max_y / 2: + q2 += 1 + if bot.x < max_x / 2 and bot.y > max_y / 2: + q3 += 1 + if bot.x > max_x / 2 and bot.y > max_y / 2: + q4 += 1 + + # Output the result + out = q1 * q2 * q3 * q4 + print(out - 21181696) + + +# Run the function +hundred_second_safety_factor() diff --git a/2024/Python/Day14/part_2.py b/2024/Python/Day14/part_2.py new file mode 100644 index 0000000..cae642c --- /dev/null +++ b/2024/Python/Day14/part_2.py @@ -0,0 +1,83 @@ +class Bot: + def __init__(self, x, y, dx, dy): + self.x = x + self.y = y + self.dx = dx + self.dy = dy + + +maxX = 101 +maxY = 103 + + +def robots_to_easter_egg(): + bots = [] + + try: + # Open the input file + with open("input.txt", "r") as file: + for line in file: + line = line.strip() # Remove leading/trailing whitespace + if line: + try: + # Parse the line into Bot object (format: p= v=) + parts = line.split(" ") + position = parts[0][2:] # Remove "p=" + velocity = parts[1][2:] # Remove "v=" + + # Extract integers from the position and velocity strings + x, y = map(int, position.split(",")) + dx, dy = map(int, velocity.split(",")) + + # Create a Bot instance and append it + bot = Bot(x, y, dx, dy) + bots.append(bot) + except ValueError as e: + # Catch the case where map(int, ...) fails (invalid number format) + print( + f"Error parsing line (invalid number format): {line} - {e}" + ) + except Exception as e: + print(f"Error opening file: {e}") + return + + # Loop for 100,000 iterations + for i in range(100000): + # Initialize the grid and fill with '.' + grid = [["." for _ in range(maxX)] for _ in range(maxY)] + + distinct = True + + # Update bot positions and check for collisions + for bot in bots: + bot.x += bot.dx + bot.y += bot.dy + + # Wrap around the edges + if bot.x < 0: + bot.x += maxX + elif bot.x >= maxX: + bot.x -= maxX + + if bot.y < 0: + bot.y += maxY + elif bot.y >= maxY: + bot.y -= maxY + + # Mark the grid cell + if grid[bot.y][bot.x] == ".": + grid[bot.y][bot.x] = "#" + else: + distinct = False + + # If all bots were distinct, print the grid + if distinct: + print(f"\nIter: {i+1}") + # Uncomment this block to print the grid + # for row in grid: + # print(''.join(row)) + break # If you only want the first iteration with distinct bots, break after the first print + + +# Run the function +robots_to_easter_egg()