From 0e180707c6ff597688a037f205ead680f886993f Mon Sep 17 00:00:00 2001 From: 808BiTT Date: Sat, 14 Dec 2024 07:26:17 -0500 Subject: [PATCH] day 3 done --- day3/main.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ day3/run.sh | 4 +++ 2 files changed, 82 insertions(+) create mode 100644 day3/main.go create mode 100755 day3/run.sh diff --git a/day3/main.go b/day3/main.go new file mode 100644 index 0000000..ff4799b --- /dev/null +++ b/day3/main.go @@ -0,0 +1,78 @@ +package main + +import ( + "fmt" + "os" + "regexp" + "strings" +) + +func main() { + if len(os.Args) < 2 { + panic("Provide input file") + } + inputFile := os.Args[1] + + part1 := part1(inputFile) + fmt.Printf("(%s) Part 1: %d\n", inputFile, part1) + + part2 := part2(inputFile) + fmt.Printf("(%s) Part 2: %d\n", inputFile, part2) +} + +func part2(inputFile string) int { + input, err := os.ReadFile(inputFile) + if err != nil { + panic(err) + } + + keepParts := make([]string, 0) + doParts := strings.Split(string(input), "do()") + for _, part := range doParts { + keepPart := strings.Split(part, "don't()")[0] + keepParts = append(keepParts, keepPart) + } + + total := 0 + for _, part := range keepParts { + exp := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)`) + occurrences := exp.FindAllString(string(part), -1) + for _, occ := range occurrences { + leftExp := regexp.MustCompile(`\((\d{1,3}),`) + rightExp := regexp.MustCompile(`,(\d{1,3})\)`) + leftNum := leftExp.FindStringSubmatch(occ)[1] + rightNum := rightExp.FindStringSubmatch(occ)[1] + total += toInt(leftNum) * toInt(rightNum) + } + } + return total +} + +func part1(inputFile string) int { + input, err := os.ReadFile(inputFile) + if err != nil { + panic(err) + } + + // regex for finding all mul(###,###) where # can be 1-3 digits + exp := regexp.MustCompile(`mul\((\d{1,3}),(\d{1,3})\)`) + occurrences := exp.FindAllString(string(input), -1) + + total := 0 + for _, occ := range occurrences { + leftExp := regexp.MustCompile(`\((\d{1,3}),`) + rightExp := regexp.MustCompile(`,(\d{1,3})\)`) + leftNum := leftExp.FindStringSubmatch(occ)[1] + rightNum := rightExp.FindStringSubmatch(occ)[1] + total += toInt(leftNum) * toInt(rightNum) + } + return total +} + +func toInt(s string) int { + var num int + for _, c := range s { + num = num*10 + int(c-'0') + } + return num +} diff --git a/day3/run.sh b/day3/run.sh new file mode 100755 index 0000000..2ea19e2 --- /dev/null +++ b/day3/run.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +go build -o ./bin/solution ./main.go +./bin/solution "$1"