112 lines
2.1 KiB
Go
112 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func mustAtoi(line string) int {
|
|
i, _ := strconv.Atoi(line)
|
|
return i
|
|
}
|
|
|
|
func main() {
|
|
start := time.Now()
|
|
partOne()
|
|
duration := time.Since(start)
|
|
partTwo()
|
|
duration2 := time.Since(start)
|
|
fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
|
|
}
|
|
|
|
func makeScanner(test bool) *bufio.Scanner {
|
|
var f *os.File
|
|
if test {
|
|
f, _ = os.Open("inputs/testinput")
|
|
} else {
|
|
f, _ = os.Open("inputs/input")
|
|
}
|
|
reader := bufio.NewReader(f)
|
|
return bufio.NewScanner(reader)
|
|
}
|
|
|
|
func partOne() {
|
|
scanner := makeScanner(false)
|
|
|
|
register := 1
|
|
cycleCount := 0
|
|
signalStrength := 0
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
switch {
|
|
case line == "noop":
|
|
cycleCount++
|
|
if cycleCount%40 == 20 {
|
|
signalStrength += (cycleCount) * register
|
|
}
|
|
default:
|
|
s := strings.Split(line, " ")
|
|
x := mustAtoi(s[1])
|
|
cycleCount++
|
|
if cycleCount%40 == 20 {
|
|
signalStrength += (cycleCount) * register
|
|
}
|
|
cycleCount++
|
|
if cycleCount%40 == 20 {
|
|
signalStrength += (cycleCount) * register
|
|
}
|
|
register += x
|
|
}
|
|
}
|
|
fmt.Println(signalStrength)
|
|
}
|
|
|
|
func partTwo() {
|
|
scanner := makeScanner(false)
|
|
|
|
screen := make([]string, 241)
|
|
|
|
register := 1
|
|
cycleCount := 0
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
switch {
|
|
case line == "noop":
|
|
if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 {
|
|
screen[cycleCount] = "#"
|
|
} else {
|
|
screen[cycleCount] = "."
|
|
}
|
|
cycleCount++
|
|
default:
|
|
s := strings.Split(line, " ")
|
|
x := mustAtoi(s[1])
|
|
if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 {
|
|
screen[cycleCount] = "#"
|
|
} else {
|
|
screen[cycleCount] = "."
|
|
}
|
|
cycleCount++
|
|
if register == cycleCount%40 || register-1 == cycleCount%40 || register+1 == cycleCount%40 {
|
|
screen[cycleCount] = "#"
|
|
} else {
|
|
screen[cycleCount] = "."
|
|
}
|
|
cycleCount++
|
|
register += x
|
|
}
|
|
}
|
|
fmt.Println(screen[1:40])
|
|
fmt.Println(screen[41:80])
|
|
fmt.Println(screen[81:120])
|
|
fmt.Println(screen[121:160])
|
|
fmt.Println(screen[161:200])
|
|
fmt.Println(screen[201:240])
|
|
}
|