“I fixed the problem by putting /* eslint-disable */ at the top of a file”
“I fixed the problem by putting /* eslint-disable */ at the top of a file”
deleted by creator
Using a map to store u|v relations. Part 2 sorting with a custom compare function worked very nicely
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)
}
Same, I initially also thought a|b and a|c implies a|c. However when I drew the graph of the example on paper, I suspected that all relations will be given, and coded it with that assumption, that turned out to be correct
Just a bunch of ifs and bounds checking. Part 2 was actually simpler.
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)
}
Honestly this is soo much better, I’m not proud of my code at all haha. Thanks for sharing, definitely adding that | to my bag of tricks
Part 1, just find the regex groups, parse to int, and done.
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
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)
}
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
Yes indeed it’s pretty technical and content-heavy. It helped me that I took a class in university that covered many of the topics, still a few things went over my head. Maybe it’s time I do a re-read of selected chapters
If you’re asking for real, Designing Data-Intensive Applications is probably the best I’ve ever read (if you’re into that kind of thing)
I remember when I got here it was a nightmare for the first few months when I didn’t have strong auth. It took several months to open a bank account and you basically can’t do anything without that stuff
Oh yes this QR code bullshit drives me nuts back home too
Been thinking of trying this. Thing is nobody uses cash anymore around here (Nordics). In supermarkets I know for sure I can use cash, but restaurants, bars or small business it’s going to be hit and miss. I guess I’d have to endure the awkwardness of asking in every single place if they take cash or not
Another one of this kind is rm -rf node_modules && npm install
Your .env with production credentials is now up on a public repo
“I no longer build software; I now make furniture out of wood”
This would be the best way. Unfortunately they made it the other way around. A screen at the door shows the code, and you scan it with the app.
In my previous gym the code was on the app, but I’m not sure anymore if it was static or it changed over time. But the reader on the door was awful, I used to spend a good 3 minutes trying different angles with my phone to make it recognize the code.
Yeah I’ve been saying this to people. Don’t get mad at GDPR, get mad at companies who harvest your data
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…