-
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
be8a16f
commit dcbf11c
Showing
4 changed files
with
145 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,48 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
func additionOfMultiplication() int { | ||
// Open the input file | ||
file, err := os.Open("./input.txt") | ||
// fmt.Println(file) | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
return 0 | ||
} | ||
defer file.Close() | ||
|
||
// Read the file content | ||
scanner := bufio.NewScanner(file) | ||
var corruptedMemory string | ||
for scanner.Scan() { | ||
corruptedMemory += scanner.Text() | ||
} | ||
|
||
// Define the regex pattern | ||
pattern := `mul\((\d+),(\d+)\)` | ||
re := regexp.MustCompile(pattern) | ||
|
||
// Find all matches | ||
matches := re.FindAllStringSubmatch(corruptedMemory, -1) | ||
|
||
// Calculate the sum of the products | ||
total := 0 | ||
for _, match := range matches { | ||
if len(match) == 3 { | ||
x, _ := strconv.Atoi(match[1]) | ||
y, _ := strconv.Atoi(match[2]) | ||
total += x * y | ||
} | ||
} | ||
|
||
// Print and return the result | ||
fmt.Println(total) | ||
return total | ||
} |
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
func resultOfEnabledMultiply() uint32 { | ||
// Open the input file | ||
file, err := os.Open("./input.txt") | ||
// fmt.Println(file) | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
return 0 | ||
} | ||
defer file.Close() | ||
|
||
// Read the file content | ||
scanner := bufio.NewScanner(file) | ||
var corruptedMemory string | ||
for scanner.Scan() { | ||
corruptedMemory += scanner.Text() | ||
} | ||
// Compile the regex pattern | ||
re := regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)|don't\(\)|do\(\)`) | ||
|
||
// Set the enabled flag to true | ||
enabled := true | ||
total := uint32(0) | ||
|
||
// Iterate over the regex matches | ||
matches := re.FindAllStringSubmatch(corruptedMemory, -1) | ||
for _, match := range matches { | ||
switch match[0] { | ||
case "do()": | ||
enabled = true | ||
case "don't()": | ||
enabled = false | ||
default: | ||
if enabled { | ||
// Parse the numbers and multiply them | ||
x, _ := strconv.Atoi(match[1]) | ||
y, _ := strconv.Atoi(match[2]) | ||
total += uint32(x * y) | ||
} | ||
} | ||
} | ||
|
||
return total | ||
} |
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,13 @@ | ||
import re | ||
|
||
|
||
def additionOfMultiplication(): | ||
with open("./input.txt", "r") as file: | ||
corruptedMemory = file.read() | ||
pattern = r"mul\((\d+),(\d+)\)" | ||
matches = re.findall(pattern, corruptedMemory) | ||
|
||
# Calculate the sum of the products | ||
total = sum(int(x) * int(y) for x, y in matches) | ||
print(total) | ||
return total |
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,31 @@ | ||
import re | ||
|
||
|
||
def resultOfEnabledMultiply(): | ||
with open("./input.txt", "r") as file: | ||
corruptedMemory = file.read() | ||
# Regular expressions for each instruction type | ||
mulPattern = r"mul\((\d+),(\d+)\)" | ||
doPattern = r"do\(\)" | ||
dontPattern = r"don't\(\)" | ||
|
||
# Split memory into individual characters or instructions | ||
instructions = re.split(r"(do\(\)|don't\(\)|mul\(\d+,\d+\))", corruptedMemory) | ||
|
||
# Remove empty strings and irrelevant characters | ||
instructions = [instr for instr in instructions if instr and not instr.isspace()] | ||
|
||
# Tracking the state and the result | ||
enabled = True | ||
total = 0 | ||
|
||
for instr in instructions: | ||
if re.match(doPattern, instr): | ||
enabled = True | ||
elif re.match(dontPattern, instr): | ||
enabled = False | ||
elif re.match(mulPattern, instr) and enabled: | ||
# Extract numbers from the mul(X, Y) instruction | ||
x, y = map(int, re.findall(r"\d+", instr)) | ||
total += x * y | ||
return total |