-
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
9753914
commit 25d593f
Showing
4 changed files
with
246 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,90 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"math" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// Function to parse the input into systems of equations | ||
func formulae() [][]int { | ||
file, err := os.Open("input.txt") | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
|
||
} | ||
defer file.Close() | ||
|
||
// Read the entire file content into a string | ||
var input string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
input += scanner.Text() + "\n" | ||
} | ||
if err := scanner.Err(); err != nil { | ||
fmt.Println("Error reading file:", err) | ||
|
||
} | ||
// Split input by double newlines | ||
sections := strings.Split(input, "\n\n") | ||
var systems [][]int | ||
re := regexp.MustCompile(`\d+`) | ||
|
||
// Extract integers from each section | ||
for _, section := range sections { | ||
matches := re.FindAllString(section, -1) | ||
system := make([]int, len(matches)) | ||
for i, match := range matches { | ||
system[i], _ = strconv.Atoi(match) | ||
} | ||
systems = append(systems, system) | ||
} | ||
return systems | ||
} | ||
|
||
// Function to solve the system of equations and compute the result | ||
func run(machines [][]int, shift int) int { | ||
result := 0 | ||
|
||
for _, machine := range machines { | ||
ax, ay, bx, by, px, py := float64(machine[0]), float64(machine[1]), float64(machine[2]), float64(machine[3]), float64(machine[4]), float64(machine[5]) | ||
px += float64(shift) | ||
py += float64(shift) | ||
|
||
// Avoid division by zero or invalid calculations | ||
if by == 0 || ax == ay*bx/by { | ||
continue | ||
} | ||
|
||
// Solve the system of equations | ||
a := (px - py*bx/by) / (ax - ay*bx/by) | ||
b := (py - a*ay) / by | ||
|
||
// Round the results using math.Round for better precision | ||
ra := math.Round(a) | ||
rb := math.Round(b) | ||
|
||
// Check if the solution is valid | ||
if int(ra)*int(ax)+int(rb)*int(bx) == int(px) && int(ra)*int(ay)+int(rb)*int(by) == int(py) && ra >= 0 && rb >= 0 { | ||
result += int(ra)*3 + int(rb) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func fewestTokenToWin() { | ||
// Open the input.txt file | ||
|
||
// Initialize the systems from the input | ||
machines := formulae() | ||
|
||
// Part 1: Solve without the shift | ||
fmt.Println("p1", run(machines, 0)) | ||
|
||
// Part 2: Solve with a large shift | ||
|
||
} |
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,81 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"math" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
func formulas() [][]int { | ||
file, err := os.Open("input.txt") | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
|
||
} | ||
defer file.Close() | ||
|
||
// Read the entire file content into a string | ||
var input string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
input += scanner.Text() + "\n" | ||
} | ||
if err := scanner.Err(); err != nil { | ||
fmt.Println("Error reading file:", err) | ||
|
||
} | ||
// Split input by double newlines | ||
sections := strings.Split(input, "\n\n") | ||
var systems [][]int | ||
re := regexp.MustCompile(`\d+`) | ||
|
||
// Extract integers from each section | ||
for _, section := range sections { | ||
matches := re.FindAllString(section, -1) | ||
system := make([]int, len(matches)) | ||
for i, match := range matches { | ||
system[i], _ = strconv.Atoi(match) | ||
} | ||
systems = append(systems, system) | ||
} | ||
return systems | ||
} | ||
|
||
// Function to solve the system of equations and compute the result | ||
func largerRun(machines [][]int, shift int) int { | ||
result := 0 | ||
|
||
for _, machine := range machines { | ||
ax, ay, bx, by, px, py := float64(machine[0]), float64(machine[1]), float64(machine[2]), float64(machine[3]), float64(machine[4]), float64(machine[5]) | ||
px += float64(shift) | ||
py += float64(shift) | ||
|
||
// Avoid division by zero or invalid calculations | ||
if by == 0 || ax == ay*bx/by { | ||
continue | ||
} | ||
|
||
// Solve the system of equations | ||
a := (px - py*bx/by) / (ax - ay*bx/by) | ||
b := (py - a*ay) / by | ||
|
||
// Round the results using math.Round for better precision | ||
ra := math.Round(a) | ||
rb := math.Round(b) | ||
|
||
// Check if the solution is valid | ||
if int(ra)*int(ax)+int(rb)*int(bx) == int(px) && int(ra)*int(ay)+int(rb)*int(by) == int(py) && ra >= 0 && rb >= 0 { | ||
result += int(ra)*3 + int(rb) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func fewestTokenToSpend() { | ||
runMachine := formulas() | ||
fmt.Println("p2", run(runMachine, 10000000000000)) | ||
} |
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,37 @@ | ||
def fewestTokenToWin() -> int: | ||
# Parse the constants out of each test | ||
with open("./input.txt") as file: | ||
con = file.read() | ||
machines = [] | ||
for block in con.strip().split("\n\n"): | ||
button_a, button_b, prize = block.split("\n") | ||
c1, c4 = [int(num.split("+")[1]) for num in button_a.split(": ")[1].split(", ")] | ||
c2, c5 = [int(num.split("+")[1]) for num in button_b.split(": ")[1].split(", ")] | ||
c3, c6 = [int(num.split("=")[1]) for num in prize.split(": ")[1].split(", ")] | ||
machines.append({"c1": c1, "c2": c2, "c3": c3, "c4": c4, "c5": c5, "c6": c6}) | ||
|
||
def calculateSum() -> int: | ||
total= 0 | ||
for _, machine in enumerate(machines): | ||
c1, c2, c3, c4, c5, c6 = ( | ||
machine["c1"], | ||
machine["c2"], | ||
machine["c3"], | ||
machine["c4"], | ||
machine["c5"], | ||
machine["c6"], | ||
) | ||
|
||
# Solve for a and b | ||
b = (c1 * c6 - c4 * c3) / (c1 * c5 - c4 * c2) | ||
a = (c3 - c2 * b) / c1 | ||
|
||
# Check if a and b are integers | ||
if a.is_integer() and b.is_integer(): | ||
total += a * 3 + b | ||
print(int(total)) | ||
return total | ||
|
||
return calculateSum() | ||
|
||
|
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,38 @@ | ||
def fewestTokenToSpend() -> int: | ||
with open("./input.txt") as file: | ||
con = file.read() | ||
# Parse the constants out of each test | ||
machines = [] | ||
for block in con.strip().split("\n\n"): | ||
button_a, button_b, prize = block.split("\n") | ||
c1, c4 = [int(num.split("+")[1]) for num in button_a.split(": ")[1].split(", ")] | ||
c2, c5 = [int(num.split("+")[1]) for num in button_b.split(": ")[1].split(", ")] | ||
c3, c6 = [ | ||
int(num.split("=")[1]) + 10000000000000 | ||
for num in prize.split(": ")[1].split(", ") | ||
] | ||
machines.append({"c1": c1, "c2": c2, "c3": c3, "c4": c4, "c5": c5, "c6": c6}) | ||
|
||
def calculateSum(): | ||
total = 0 | ||
for machine in machines: | ||
c1, c2, c3, c4, c5, c6 = ( | ||
machine["c1"], | ||
machine["c2"], | ||
machine["c3"], | ||
machine["c4"], | ||
machine["c5"], | ||
machine["c6"], | ||
) | ||
|
||
# Solve for a and b | ||
b = (c1 * c6 - c4 * c3) / (c1 * c5 - c4 * c2) | ||
a = (c3 - c2 * b) / c1 | ||
|
||
# Check if a and b are integers | ||
if a.is_integer() and b.is_integer(): | ||
total += a * 3 + b | ||
print(int(total)) | ||
return total | ||
|
||
return calculateSum() |