Added Day 4

This commit is contained in:
Logan G 2023-12-05 04:34:26 -07:00
parent f87d8d7cf5
commit 12f09dc71f
Signed by: logan
GPG key ID: E328528C921E7A7A
3 changed files with 77 additions and 0 deletions

BIN
day4/d4p1.ods Normal file

Binary file not shown.

71
day4/d4p2.go Normal file
View file

@ -0,0 +1,71 @@
// +build linux
package main
import (
"fmt"
//"io"
//"bufio"
"os"
"strings"
"regexp"
)
func find_num_wins(winners string, numbers string) int {
wins := 0
for _, number := range strings.Split(numbers, " ") {
for _, winner := range strings.Split(winners, " ") {
if number == winner && len(number) > 0 {
wins++
}
}
}
return wins
}
func main() {
if len(os.Args) < 2 {
panic("You did not specify an input file.")
}
rawInput, err := os.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
stringInput := strings.Trim(string(rawInput), "\n")
stringInput = regexp.MustCompile(`(Card.*:\ )`).ReplaceAllString(stringInput, "")
lines := strings.Split(stringInput, "\n")
var cards []int
cards = make([]int, len(lines))
for i,_ := range cards {
cards[i] = 1
}
sum := 0
for card, game := range lines {
if len(game) < 10 {
continue
}
game_ := strings.Split(game, " | ")
winners := game_[0]
numbers := game_[1]
wins := find_num_wins(numbers, winners)
sum += cards[card]
for i := 0; i < wins; i++ {
cards[card+i+1] += cards[card]
}
fmt.Println(card+1, cards[card])
}
fmt.Println("Answer: ", sum)
}

6
day4/sample1 Normal file
View file

@ -0,0 +1,6 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11