-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25123f0
commit fd2aafd
Showing
4 changed files
with
351 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<x,y> v=<dx,dy>) | ||
_, 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<x,y> v=<dx,dy>) | ||
_, 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() | ||
// } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<x,y>) and dx,dy for velocity (v=<dx,dy>) | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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=<x,y> v=<dx,dy>) | ||
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() |