Go
Part 2: Read the whole input in a rune matrix. Scan it column by column, store the numbers as you go, ignoring all spaces, and store the operand when you find it. When you hit an empty column or the end, do the operation and add it to the total.
spoiler
func part2() {
// file, _ := os.Open("sample.txt")
file, _ := os.Open("input.txt")
defer file.Close()
scanner := bufio.NewScanner(file)
chars := [][]rune{}
for scanner.Scan() {
chars = append(chars, []rune(scanner.Text()))
}
m := len(chars)
n := len(chars[0])
var op rune
nums := []int{}
total := 0
for j := range n {
current := []rune{}
for i := range m {
if chars[i][j] == '+' || chars[i][j] == '*' {
op = chars[i][j]
} else if chars[i][j] != ' ' {
current = append(current, chars[i][j])
}
}
if len(current) > 0 {
x, _ := strconv.Atoi(string(current))
nums = append(nums, x)
}
if len(current) == 0 || j == n-1 {
result := 0
if op == '*' {
result = 1
}
for _, x := range nums {
if op == '+' {
result = result + x
} else {
result = result * x
}
}
total += result
nums = []int{}
}
}
fmt.Println(total)
}






Go
Now I’m behind by 1 day, will try to catch up.
For part 2 I spent a good while thinking about it, then when I convinced myself my plan could work, struggled a bit with the implementation. But it worked in the end. Basically grid[i][j] is how many different ways you can reach a cell. Start at 1 on the S cell, then propagate the values down and keep adding up the nums when you reach cells through different paths. The answer is the sum of the nums in the last row.
spoiler
func part2() { // file, _ := os.Open("sample.txt") file, _ := os.Open("input.txt") defer file.Close() scanner := bufio.NewScanner(file) input := [][]rune{} for scanner.Scan() { line := []rune(scanner.Text()) input = append(input, line) } m := len(input) n := len(input[0]) grid := make([][]int, m) for i := range m { grid[i] = make([]int, n) } for i := range m { for j := range n { c := input[i][j] if i == 0 { if c == 'S' { grid[i][j] = 1 } continue } if c == '^' { grid[i][j-1] += grid[i-1][j] grid[i][j+1] += grid[i-1][j] } else { grid[i][j] = grid[i][j] + grid[i-1][j] } } } paths := 0 for j := range n { paths += grid[m-1][j] } fmt.Println(paths) }