day 1
This commit is contained in:
commit
fd08122fb3
4 changed files with 4774 additions and 0 deletions
97
day1/main.go
Normal file
97
day1/main.go
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func readLines(filename string) []string {
|
||||
raw, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return strings.Split(string(raw), "\n")
|
||||
}
|
||||
|
||||
func parseLine(line string) (int, error) {
|
||||
var c int
|
||||
switch {
|
||||
case strings.HasPrefix(line, "R"):
|
||||
c = 1
|
||||
case strings.HasPrefix(line, "L"):
|
||||
c = -1
|
||||
default:
|
||||
return 0, fmt.Errorf("invalid line")
|
||||
}
|
||||
x, err := strconv.Atoi(line[1:])
|
||||
if err != nil {
|
||||
return c, err
|
||||
}
|
||||
if x <= 0 {
|
||||
return x, fmt.Errorf("non-positive value")
|
||||
}
|
||||
return c * x, nil
|
||||
}
|
||||
|
||||
func countZeros(lines []string) int {
|
||||
count := 0
|
||||
current := 50
|
||||
max := 100
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
x, err := parseLine(line)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
current = (current + x) % max
|
||||
if current == 0 {
|
||||
count++
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func countClicks(lines []string) int {
|
||||
count := 0
|
||||
current := 50
|
||||
max := 100
|
||||
for _, line := range lines {
|
||||
if line == "" {
|
||||
continue
|
||||
}
|
||||
x, err := parseLine(line)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
if x > 0 {
|
||||
x--
|
||||
current++
|
||||
if current == 0 || current == max {
|
||||
count++
|
||||
current = 0
|
||||
}
|
||||
} else if x < 0 {
|
||||
x++
|
||||
current--
|
||||
if current == 0 || current == -max {
|
||||
count++
|
||||
current = 0
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
func main() {
|
||||
lines := readLines("part1.txt")
|
||||
println(countZeros(lines))
|
||||
println(countClicks(lines))
|
||||
}
|
||||
10
day1/part0.txt
Normal file
10
day1/part0.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
L68
|
||||
L30
|
||||
R48
|
||||
L5
|
||||
R60
|
||||
L55
|
||||
L1
|
||||
L99
|
||||
R14
|
||||
L82
|
||||
4664
day1/part1.txt
Normal file
4664
day1/part1.txt
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue