reboot6675

joined 2 years ago
[โ€“] [email protected] 3 points 7 months ago (2 children)

Go

Part 1, just find the regex groups, parse to int, and done.

Part 1

func part1() {
	file, _ := os.Open("input.txt")
	defer file.Close()
	scanner := bufio.NewScanner(file)

	re := regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)`)
	product := 0

	for scanner.Scan() {
		line := scanner.Text()
		submatches := re.FindAllStringSubmatch(line, -1)

		for _, s := range submatches {
			a, _ := strconv.Atoi(s[1])
			b, _ := strconv.Atoi(s[2])
			product += (a * b)
		}
	}

	fmt.Println(product)
}

Part 2, not so simple. Ended up doing some weird hack with a map to check if the multiplication was enabled or not. Also instead of finding regex groups I had to find the indices, and then interpret what those mean... Not very readable code I'm afraid

Part2

func part2() {
	file, _ := os.Open("input.txt")
	defer file.Close()
	scanner := bufio.NewScanner(file)

	mulRE := regexp.MustCompile(`mul\(([0-9]{1,3}),([0-9]{1,3})\)`)
	doRE := regexp.MustCompile(`do\(\)`)
	dontRE := regexp.MustCompile(`don't\(\)`)
	product := 0
	enabled := true

	for scanner.Scan() {
		line := scanner.Text()
		doIndices := doRE.FindAllStringIndex(line, -1)
		dontIndices := dontRE.FindAllStringIndex(line, -1)
		mulSubIndices := mulRE.FindAllStringSubmatchIndex(line, -1)

		mapIndices := make(map[int]string)
		for _, do := range doIndices {
			mapIndices[do[0]] = "do"
		}
		for _, dont := range dontIndices {
			mapIndices[dont[0]] = "dont"
		}
		for _, mul := range mulSubIndices {
			mapIndices[mul[0]] = "mul"
		}

		nextMatch := 0

		for i := 0; i < len(line); i++ {
			val, ok := mapIndices[i]
			if ok && val == "do" {
				enabled = true
			} else if ok && val == "dont" {
				enabled = false
			} else if ok && val == "mul" {
				if enabled {
					match := mulSubIndices[nextMatch]
					a, _ := strconv.Atoi(string(line[match[2]:match[3]]))
					b, _ := strconv.Atoi(string(line[match[4]:match[5]]))
					product += (a * b)
				}
				nextMatch++
			}
		}
	}

	fmt.Println(product)
}

[โ€“] [email protected] 2 points 7 months ago

Really cool trick. I did a bunch of regex matching that I'm sure I won't remember how it works few weeks from now, this is so much readable

[โ€“] [email protected] 4 points 8 months ago

Go. I actually I haven't written a single line of Go since last year's AoC so it's a good excuse to go again (pun intended).

[โ€“] [email protected] 3 points 8 months ago

I liked Digital Minimalism by Cal Newport. I found myself reaching for my phone more often than I would few years back, and doomscrolling at nights, and I got some good tips from this book.

I wouldn't say it solved all of my issues and I will be taking a look at the other books recommended here. But it did help me reduce screen time and focus more on tasks without feeling that I have to look at my phone every 5 minutes.

[โ€“] [email protected] 5 points 8 months ago

Dragon Ball characters, named Bibidi, Babidi and Boo, respectively.

(Ok the last one is called Majin Boo but he's referred as Boo too)

[โ€“] [email protected] 5 points 8 months ago

Tbh these don't even look that bad. Even many times you peel one of these and they are totally fine on the inside

[โ€“] [email protected] 21 points 8 months ago

Cosmo and Wanda don't fool anyone nowadays

[โ€“] [email protected] 2 points 8 months ago

I feel you. And I work in tech (maybe you do too, idk). Point is, I'd need a bicycle AND a career change...

[โ€“] [email protected] 1 points 8 months ago

Oh whaat, hell yeah! Definitely gonna be having a look at this, thanks for sharing.

Still want that remake tho haha

[โ€“] [email protected] 2 points 8 months ago (3 children)

I want a remake of the original Harry Potter 1 and 2 games from the PS1. Not asking much, anything that looks nicer than this would do

[โ€“] [email protected] 3 points 9 months ago

Taringa, it was the go-to place for everything, especially content in spanish

view more: โ€น prev next โ€บ