Solve day three
This commit is contained in:
parent
b8fb971fa9
commit
fc9342dd3d
6 changed files with 89 additions and 1095 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
*.txt
|
||||
95
three.go
95
three.go
|
|
@ -1,95 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func is_safe(levels []int) bool {
|
||||
all_diffs_neg := true
|
||||
all_diffs_pos := true
|
||||
|
||||
prev := levels[0]
|
||||
for i, level := range levels {
|
||||
if i == 0 {
|
||||
prev = level
|
||||
continue
|
||||
}
|
||||
|
||||
diff := level - prev
|
||||
if diff > 3 || diff < -3 || diff == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
all_diffs_pos = all_diffs_pos && diff > 0
|
||||
all_diffs_neg = all_diffs_neg && diff < 0
|
||||
prev = level
|
||||
}
|
||||
|
||||
return all_diffs_neg || all_diffs_pos
|
||||
}
|
||||
|
||||
func is_safe_with_dampener(levels []int) bool {
|
||||
if is_safe(levels) {
|
||||
return true
|
||||
}
|
||||
|
||||
for i := range levels {
|
||||
damped_levels := make([]int, 0, len(levels)-1)
|
||||
for j, val := range levels {
|
||||
if i == j {
|
||||
continue
|
||||
}
|
||||
|
||||
damped_levels = append(damped_levels, val)
|
||||
}
|
||||
|
||||
if is_safe(damped_levels) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func main() {
|
||||
f, err := os.Open("./two.txt")
|
||||
defer f.Close()
|
||||
check(err)
|
||||
|
||||
n_safe := 0
|
||||
n_dampener_safe := 0
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
|
||||
line := scanner.Text()
|
||||
fields := strings.Fields(line)
|
||||
|
||||
levels := make([]int, 0, 5)
|
||||
|
||||
for _, field := range fields {
|
||||
level, err := strconv.Atoi(field)
|
||||
check(err)
|
||||
levels = append(levels, level)
|
||||
}
|
||||
if is_safe(levels) {
|
||||
n_safe += 1
|
||||
}
|
||||
if is_safe_with_dampener(levels) {
|
||||
n_dampener_safe += 1
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(n_safe)
|
||||
fmt.Println(n_dampener_safe)
|
||||
}
|
||||
88
three/three.go
Normal file
88
three/three.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var mul_re *regexp.Regexp = regexp.MustCompile(`mul\(([0-9]+),([0-9]+)\)`)
|
||||
|
||||
func check(e error) {
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
|
||||
func handle_muls(input string) int {
|
||||
total := 0
|
||||
|
||||
matches := mul_re.FindAllStringSubmatch(input, -1)
|
||||
for _, match := range matches {
|
||||
first, err := strconv.Atoi(match[1])
|
||||
check(err)
|
||||
second, err := strconv.Atoi(match[2])
|
||||
check(err)
|
||||
|
||||
total += first * second
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
func next(matches [][]int, start int) [][]int {
|
||||
for i, match := range matches {
|
||||
if match[0] > start {
|
||||
return matches[i:]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
instructions, err := os.ReadFile("./three.txt")
|
||||
check(err)
|
||||
|
||||
str_instructions := string(instructions)
|
||||
|
||||
// Find all mul instructions
|
||||
fmt.Println(handle_muls(str_instructions))
|
||||
|
||||
// Handle disables and enables
|
||||
total := 0
|
||||
enable_re := regexp.MustCompile(`do\(\)`)
|
||||
disable_re := regexp.MustCompile(`don't\(\)`)
|
||||
dos := enable_re.FindAllStringIndex(str_instructions, -1)
|
||||
donts := disable_re.FindAllStringIndex(str_instructions, -1)
|
||||
|
||||
enabled := true
|
||||
done := false
|
||||
|
||||
start_i := 0
|
||||
end_i := 0
|
||||
|
||||
for !done {
|
||||
if enabled {
|
||||
donts = next(donts, start_i)
|
||||
if donts == nil {
|
||||
total += handle_muls(str_instructions[start_i:])
|
||||
done = true
|
||||
} else {
|
||||
end_i = donts[0][0]
|
||||
total += handle_muls(str_instructions[start_i : end_i+1])
|
||||
enabled = false
|
||||
}
|
||||
} else {
|
||||
dos = next(dos, end_i)
|
||||
if dos == nil {
|
||||
done = true
|
||||
} else {
|
||||
start_i = dos[0][1]
|
||||
enabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println(dos)
|
||||
fmt.Println(donts)
|
||||
fmt.Println(total)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue