mirror of
https://github.com/zebrajr/coding-challenges.git
synced 2025-12-06 12:20:53 +01:00
added 2024 day 03 solutio106921067n
This commit is contained in:
parent
85438a4eb0
commit
f7af519052
3
Advent of Code/2024/Day 03/simple_puzzle
Normal file
3
Advent of Code/2024/Day 03/simple_puzzle
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
xmul(2,4)&mul[3,7]!don't()
|
||||||
|
^don't()_mul(5,5)+mul(32,64](mul(11,8)
|
||||||
|
undo()?mul(8,5))
|
||||||
|
|
@ -1,40 +1,96 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//var puzzle_input_file = "simple_puzzle"
|
||||||
var puzzle_input_file = "puzzle_input"
|
var puzzle_input_file = "puzzle_input"
|
||||||
var regex_pattern_mul = `mul\((\d+),(\d+)\)`
|
var multiplier_expression = `mul\((\d+),(\d+)\)`
|
||||||
var regex_pattern_numbers = `(\d+)`
|
var digits_expression = `(\d+)`
|
||||||
|
var do_dont_expression = `do\(\)|don't\(\)`
|
||||||
|
var string_do = `do()`
|
||||||
|
var string_dont = `don't()`
|
||||||
|
|
||||||
func main(){
|
|
||||||
file, err := os.Open(puzzle_input_file)
|
func find_dos_donts(input_string string) int {
|
||||||
if err != nil {
|
do_dont_regex := regexp.MustCompile(do_dont_expression)
|
||||||
log.Fatal(err)
|
parts := do_dont_regex.FindAllStringIndex(input_string, -1)
|
||||||
|
current_result := 0
|
||||||
|
next_start_index := 0
|
||||||
|
is_multiplier_enabled := true
|
||||||
|
for _, part := range parts{
|
||||||
|
match_start := part[0]
|
||||||
|
match_end := part[1]
|
||||||
|
// first match, so check start or string -> now
|
||||||
|
if is_multiplier_enabled {
|
||||||
|
string_to_check := input_string[next_start_index:match_start]
|
||||||
|
mul_result := find_mul_in_string(string_to_check)
|
||||||
|
current_result += mul_result
|
||||||
|
}
|
||||||
|
|
||||||
|
next_start_index = match_end
|
||||||
|
|
||||||
|
// check which operation we got coming next
|
||||||
|
new_operation := input_string[match_start:match_end]
|
||||||
|
if new_operation == string_do{
|
||||||
|
if is_multiplier_enabled == false {
|
||||||
|
is_multiplier_enabled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if new_operation == string_dont{
|
||||||
|
if is_multiplier_enabled == true {
|
||||||
|
is_multiplier_enabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
|
// before exiting, handle the rest of the text
|
||||||
|
if is_multiplier_enabled{
|
||||||
|
string_to_check := input_string[next_start_index:]
|
||||||
|
mul_result := find_mul_in_string(string_to_check)
|
||||||
|
current_result += mul_result
|
||||||
|
}
|
||||||
|
|
||||||
|
return current_result
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func find_mul_in_string(input_string string) int {
|
||||||
final_answer := 0
|
final_answer := 0
|
||||||
file_scanner := bufio.NewScanner(file)
|
mul_regex := regexp.MustCompile(multiplier_expression)
|
||||||
mul_regex := regexp.MustCompile(regex_pattern_mul)
|
//all_matches := mul_regex.FindAllString(input_string, -1)
|
||||||
|
all_matches := mul_regex.FindAllString(input_string, -1)
|
||||||
for file_scanner.Scan(){
|
for _, value := range all_matches{
|
||||||
current_line := file_scanner.Text()
|
numbers_regex := regexp.MustCompile(digits_expression)
|
||||||
all_matches := mul_regex.FindAllString(current_line, -1)
|
|
||||||
for _, value := range all_matches {
|
|
||||||
numbers_regex := regexp.MustCompile(regex_pattern_numbers)
|
|
||||||
numbers_matches := numbers_regex.FindAllString(value, -1)
|
numbers_matches := numbers_regex.FindAllString(value, -1)
|
||||||
first_number, _ := strconv.Atoi(numbers_matches[0])
|
first_number, _ := strconv.Atoi(numbers_matches[0])
|
||||||
second_number, _ := strconv.Atoi(numbers_matches[1])
|
second_number, _ := strconv.Atoi(numbers_matches[1])
|
||||||
multiplication_result := first_number * second_number
|
multiplication_result := first_number * second_number
|
||||||
final_answer += multiplication_result
|
final_answer += multiplication_result
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fmt.Println("Final Answer: ", final_answer)
|
return final_answer
|
||||||
|
}
|
||||||
|
|
||||||
|
func main(){
|
||||||
|
// if we read the file as a buffer like we did in the day 01 and 02
|
||||||
|
// we will get an error because we have multiple lines (6)
|
||||||
|
// therefore making the `final_answer_advanced` higher then it should
|
||||||
|
// since a new line will mean the mul(x,y) are valid
|
||||||
|
// imho that should be the right answer
|
||||||
|
input_file, err := os.ReadFile(puzzle_input_file)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
input_string := string(input_file)
|
||||||
|
|
||||||
|
final_answer := find_mul_in_string(input_string)
|
||||||
|
fmt.Println("Final Answer: ", final_answer)
|
||||||
|
|
||||||
|
final_answer_advanced := find_dos_donts(input_string)
|
||||||
|
fmt.Println("Final Answer with do() / don't(): ", final_answer_advanced)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user