From f7af519052bff018c49da96ea112d2afab50c9ac Mon Sep 17 00:00:00 2001 From: Carlos Sousa Date: Thu, 19 Dec 2024 21:24:10 +0100 Subject: [PATCH] added 2024 day 03 solutio106921067n --- Advent of Code/2024/Day 03/simple_puzzle | 3 + Advent of Code/2024/Day 03/solution.go | 94 +++++++++++++++++++----- 2 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 Advent of Code/2024/Day 03/simple_puzzle diff --git a/Advent of Code/2024/Day 03/simple_puzzle b/Advent of Code/2024/Day 03/simple_puzzle new file mode 100644 index 0000000..19d111b --- /dev/null +++ b/Advent of Code/2024/Day 03/simple_puzzle @@ -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)) diff --git a/Advent of Code/2024/Day 03/solution.go b/Advent of Code/2024/Day 03/solution.go index eb0fbf2..c5120e7 100644 --- a/Advent of Code/2024/Day 03/solution.go +++ b/Advent of Code/2024/Day 03/solution.go @@ -1,40 +1,96 @@ package main import ( - "bufio" "fmt" - "strconv" "log" "os" "regexp" + "strconv" ) +//var puzzle_input_file = "simple_puzzle" var puzzle_input_file = "puzzle_input" -var regex_pattern_mul = `mul\((\d+),(\d+)\)` -var regex_pattern_numbers = `(\d+)` +var multiplier_expression = `mul\((\d+),(\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) - if err != nil { - log.Fatal(err) + +func find_dos_donts(input_string string) int { + do_dont_regex := regexp.MustCompile(do_dont_expression) + 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 - file_scanner := bufio.NewScanner(file) - mul_regex := regexp.MustCompile(regex_pattern_mul) - - for file_scanner.Scan(){ - current_line := file_scanner.Text() - all_matches := mul_regex.FindAllString(current_line, -1) - for _, value := range all_matches { - numbers_regex := regexp.MustCompile(regex_pattern_numbers) + mul_regex := regexp.MustCompile(multiplier_expression) + //all_matches := mul_regex.FindAllString(input_string, -1) + all_matches := mul_regex.FindAllString(input_string, -1) + for _, value := range all_matches{ + numbers_regex := regexp.MustCompile(digits_expression) numbers_matches := numbers_regex.FindAllString(value, -1) first_number, _ := strconv.Atoi(numbers_matches[0]) second_number, _ := strconv.Atoi(numbers_matches[1]) multiplication_result := first_number * second_number 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) }