From 76da133e307ed9a5073fac8b554296597981ac26 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Mon, 4 Dec 2023 09:57:37 +0000 Subject: [PATCH 1/3] Working first part of day04 --- day04/inputs | 1 + day04/scratchcards.go | 72 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 120000 day04/inputs create mode 100644 day04/scratchcards.go diff --git a/day04/inputs b/day04/inputs new file mode 120000 index 0000000..a80bb2b --- /dev/null +++ b/day04/inputs @@ -0,0 +1 @@ +../inputs \ No newline at end of file diff --git a/day04/scratchcards.go b/day04/scratchcards.go new file mode 100644 index 0000000..4980508 --- /dev/null +++ b/day04/scratchcards.go @@ -0,0 +1,72 @@ +package main + +import ( + "fmt" + "strings" + "bufio" + "os" +// "strconv" + "regexp" +) + +func check(e error) { + if e != nil { + panic(e) + } +} + +func PrintAndWait(x ...any) { + fmt.Print(x) + fmt.Scanln() +} + +func CalcScore (s []string, t *int) { + // Split the two sets into winning and my numbers + tempwinningNumbers := s[0] + tempMyNumbers := s[1] + // Regex to populate a string with numbers + renum := regexp.MustCompile("[0-9]+") + myNumbers := renum.FindAllString(tempMyNumbers, -1) + winningNumbers := renum.FindAllString(tempwinningNumbers, -1) + + matches := 0 + for i := range myNumbers { + for j := range winningNumbers { + if (myNumbers[i] == winningNumbers[j]) { + matches += 1 + continue + } + } + } + if(matches > 0) { + tempTotal := 1 + for i := 0; i < matches - 1; i++ { + tempTotal *= 2 + } + *t += tempTotal + } + +} + +func main() { + file, err := os.Open("./inputs/day04_input") + check(err) + defer file.Close() + + var total int = 0 + var totalPoint *int = &total + + scanner := bufio.NewScanner(file) + + // Scan every line + for scanner.Scan() { + // e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 + line := scanner.Text() + cardAndNumbers := strings.Split(line, ":") + // At this point, cardAndNumbers has "Card N" + // We don't need this information (yet?) + allNumbers := strings.Split(cardAndNumbers[1], "|") + CalcScore(allNumbers, totalPoint) + } + fmt.Printf("The scratchcards are worth %d points.", total) +} From 8871be0bc9440ecc5163122d4d9c694d8f7112b7 Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Mon, 4 Dec 2023 13:34:48 +0000 Subject: [PATCH 2/3] First draft of working part two, heavily unoptimized --- day04/scratchcards.go | 68 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 13 deletions(-) diff --git a/day04/scratchcards.go b/day04/scratchcards.go index 4980508..fabc6c7 100644 --- a/day04/scratchcards.go +++ b/day04/scratchcards.go @@ -20,24 +20,47 @@ func PrintAndWait(x ...any) { fmt.Scanln() } -func CalcScore (s []string, t *int) { +func SplitSets(s []string) ([]string, []string) { + // Split the two sets into winning and my numbers tempwinningNumbers := s[0] tempMyNumbers := s[1] // Regex to populate a string with numbers renum := regexp.MustCompile("[0-9]+") myNumbers := renum.FindAllString(tempMyNumbers, -1) - winningNumbers := renum.FindAllString(tempwinningNumbers, -1) + winningNumbers := renum.FindAllString(tempwinningNumbers, -1) - matches := 0 - for i := range myNumbers { - for j := range winningNumbers { - if (myNumbers[i] == winningNumbers[j]) { + return myNumbers, winningNumbers +} + +func FindMatches(myNum []string, winNum []string) int { + matches := 0 + for i := range myNum { + for j := range winNum { + if (myNum[i] == winNum[j]) { matches += 1 continue } } } + return matches +} + +func CalcTickets(s []string, index int, tpr []int ) { + + myNumbers, winningNumbers := SplitSets(s) + matches := FindMatches(myNumbers, winningNumbers) + if (matches > 0) { + for i := index; i < index + matches; i++ { + tpr[i+1] += 1 + } + } +} + +func CalcScore (s []string, t *int) { + + myNumbers, winningNumbers := SplitSets(s) + matches := FindMatches(myNumbers, winningNumbers) if(matches > 0) { tempTotal := 1 for i := 0; i < matches - 1; i++ { @@ -45,7 +68,6 @@ func CalcScore (s []string, t *int) { } *t += tempTotal } - } func main() { @@ -53,20 +75,40 @@ func main() { check(err) defer file.Close() + // The total is a simple sum var total int = 0 var totalPoint *int = &total - + // To keep track of the amount of tickets, an array as long + // as the number of lines + var lines []string scanner := bufio.NewScanner(file) - - // Scan every line for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + ticketsPerRow := make([]int, len(lines)) + tprPoint := ticketsPerRow[0:len(lines)] + + for i := range ticketsPerRow { + ticketsPerRow[i] = 1 + } + + // Scan every line + for i := 0; i < len(lines); i++ { // e.g.: Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 - line := scanner.Text() - cardAndNumbers := strings.Split(line, ":") + cardAndNumbers := strings.Split(lines[i], ":") // At this point, cardAndNumbers has "Card N" // We don't need this information (yet?) allNumbers := strings.Split(cardAndNumbers[1], "|") CalcScore(allNumbers, totalPoint) + for j := 0; j < ticketsPerRow[i]; j++ { + CalcTickets(allNumbers, i, tprPoint) + } } - fmt.Printf("The scratchcards are worth %d points.", total) + + numTickets := 0 + for i := range ticketsPerRow { + numTickets += ticketsPerRow[i] + } + fmt.Printf("The scratchcards are worth %d points.\n", total) + fmt.Printf("In total, I have %d scratchcards.", numTickets) } From 36b2f6feb37655a22479ffdc8f3ac84dc098b67b Mon Sep 17 00:00:00 2001 From: Davide Oddone Date: Mon, 4 Dec 2023 13:49:18 +0000 Subject: [PATCH 3/3] Optimized my code by 140300% just by doing things correctly --- day04/scratchcards.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/day04/scratchcards.go b/day04/scratchcards.go index fabc6c7..2f299bb 100644 --- a/day04/scratchcards.go +++ b/day04/scratchcards.go @@ -51,8 +51,11 @@ func CalcTickets(s []string, index int, tpr []int ) { myNumbers, winningNumbers := SplitSets(s) matches := FindMatches(myNumbers, winningNumbers) if (matches > 0) { - for i := index; i < index + matches; i++ { - tpr[i+1] += 1 + for j := 0; j < tpr[index]; j++ { + for i := index; i < index + matches; i++ { + tpr[i+1] += 1 + } + } } } @@ -100,9 +103,7 @@ func main() { // We don't need this information (yet?) allNumbers := strings.Split(cardAndNumbers[1], "|") CalcScore(allNumbers, totalPoint) - for j := 0; j < ticketsPerRow[i]; j++ { - CalcTickets(allNumbers, i, tprPoint) - } + CalcTickets(allNumbers, i, tprPoint) } numTickets := 0