• 20 Posts
  • 101 Comments
Joined 2 years ago
cake
Cake day: September 17th, 2023

help-circle

  • Haven’t used Skype for years to talk to friends or family, but kept using it until now when I needed to call some place in my home country (usually banks) by “real” phone call, because support via internet either sucks or doesn’t exist at all. Now looking for alternatives…


  • I have mixed feelings about that company. They have some interesting things “going against the flow” like ditching the cloud and going back to on prem, hating on microservices, advocating against taking money from VCs, and now hiring juniors. On the other hand, the guy is a Musk fanboy and they push some anti-DEI bullshit. Also he’s a TypeScript hater for some reason…






  • Hey this was really cool! Thanks for sharing :)

    I’ve been thinking about trying it out for a while but not sure if it would work out for me. However I have an old Android with only Spotify installed that I take to the gym and I’m really liking it, maybe I will start using it more in other places as well.










  • Go

    Using a map to store u|v relations. Part 2 sorting with a custom compare function worked very nicely

    spoiler
    func main() {
    	file, _ := os.Open("input.txt")
    	defer file.Close()
    	scanner := bufio.NewScanner(file)
    
    	mapPages := make(map[string][]string)
    	rulesSection := true
    	middleSumOk := 0
    	middleSumNotOk := 0
    
    	for scanner.Scan() {
    		line := scanner.Text()
    		if line == "" {
    			rulesSection = false
    			continue
    		}
    
    		if rulesSection {
    			parts := strings.Split(line, "|")
    			u, v := parts[0], parts[1]
    			mapPages[u] = append(mapPages[u], v)
    		} else {
    			update := strings.Split(line, ",")
    			isOk := true
    
    			for i := 1; i < len(update); i++ {
    				u, v := update[i-1], update[i]
    				if !slices.Contains(mapPages[u], v) {
    					isOk = false
    					break
    				}
    			}
    
    			middlePos := len(update) / 2
    			if isOk {
    				middlePage, _ := strconv.Atoi(update[middlePos])
    				middleSumOk += middlePage
    			} else {
    				slices.SortFunc(update, func(u, v string) int {
    					if slices.Contains(mapPages[u], v) {
    						return -1
    					} else if slices.Contains(mapPages[v], u) {
    						return 1
    					}
    					return 0
    				})
    				middlePage, _ := strconv.Atoi(update[middlePos])
    				middleSumNotOk += middlePage
    			}
    		}
    	}
    
    	fmt.Println("Part 1:", middleSumOk)
    	fmt.Println("Part 2:", middleSumNotOk)
    }
    




  • Go

    Just a bunch of ifs and bounds checking. Part 2 was actually simpler.

    Code
    func part1(W [][]rune) {
    	m := len(W)
    	n := len(W[0])
    	xmasCount := 0
    
    	for i := 0; i < m; i++ {
    		for j := 0; j < n; j++ {
    			if W[i][j] != 'X' {
    				continue
    			}
    			if j < n-3 && W[i][j+1] == 'M' && W[i][j+2] == 'A' && W[i][j+3] == 'S' {
    				// Horizontal left to right
    				xmasCount++
    			}
    			if j >= 3 && W[i][j-1] == 'M' && W[i][j-2] == 'A' && W[i][j-3] == 'S' {
    				// Horizontal right to left
    				xmasCount++
    			}
    			if i < m-3 && W[i+1][j] == 'M' && W[i+2][j] == 'A' && W[i+3][j] == 'S' {
    				// Vertical up to down
    				xmasCount++
    			}
    			if i >= 3 && W[i-1][j] == 'M' && W[i-2][j] == 'A' && W[i-3][j] == 'S' {
    				// Vertical down to up
    				xmasCount++
    			}
    			if j < n-3 && i < m-3 && W[i+1][j+1] == 'M' && W[i+2][j+2] == 'A' && W[i+3][j+3] == 'S' {
    				// Diagonal left to right and up to down
    				xmasCount++
    			}
    			if j >= 3 && i < m-3 && W[i+1][j-1] == 'M' && W[i+2][j-2] == 'A' && W[i+3][j-3] == 'S' {
    				// Diagonal right to left and up to down
    				xmasCount++
    			}
    			if j < n-3 && i >= 3 && W[i-1][j+1] == 'M' && W[i-2][j+2] == 'A' && W[i-3][j+3] == 'S' {
    				// Diagonal left to right and down to up
    				xmasCount++
    			}
    			if j >= 3 && i >= 3 && W[i-1][j-1] == 'M' && W[i-2][j-2] == 'A' && W[i-3][j-3] == 'S' {
    				// Diagonal right to left and down to up
    				xmasCount++
    			}
    		}
    	}
    
    	fmt.Println(xmasCount)
    }
    
    func part2(W [][]rune) {
    	m := len(W)
    	n := len(W[0])
    	xmasCount := 0
    
    	for i := 0; i <= m-3; i++ {
    		for j := 0; j <= n-3; j++ {
    			if W[i+1][j+1] != 'A' {
    				continue
    			}
    			if W[i][j] == 'M' && W[i][j+2] == 'M' && W[i+2][j] == 'S' && W[i+2][j+2] == 'S' {
    				xmasCount++
    			} else if W[i][j] == 'M' && W[i][j+2] == 'S' && W[i+2][j] == 'M' && W[i+2][j+2] == 'S' {
    				xmasCount++
    			} else if W[i][j] == 'S' && W[i][j+2] == 'S' && W[i+2][j] == 'M' && W[i+2][j+2] == 'M' {
    				xmasCount++
    			} else if W[i][j] == 'S' && W[i][j+2] == 'M' && W[i+2][j] == 'S' && W[i+2][j+2] == 'M' {
    				xmasCount++
    			}
    		}
    	}
    
    	fmt.Println(xmasCount)
    }
    
    func main() {
    	file, _ := os.Open("input.txt")
    	defer file.Close()
    	scanner := bufio.NewScanner(file)
    
    	var W [][]rune
    	for scanner.Scan() {
    		line := scanner.Text()
    		W = append(W, []rune(line))
    	}
    
    	part1(W)
    	part2(W)
    }