Skip to content

Commit

Permalink
DayThree
Browse files Browse the repository at this point in the history
  • Loading branch information
ayoubzulfiqar committed Dec 3, 2024
1 parent be8a16f commit dcbf11c
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 0 deletions.
48 changes: 48 additions & 0 deletions 2024/Go/Day3/part_1.go
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
}
53 changes: 53 additions & 0 deletions 2024/Go/Day3/part_2.go
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
}
13 changes: 13 additions & 0 deletions 2024/Python/Day3/part_1.py
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
31 changes: 31 additions & 0 deletions 2024/Python/Day3/part_2.py
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

0 comments on commit dcbf11c

Please sign in to comment.