Cathode-ray tube
Problem is basically keeping track of a running sum.
package problems
import (
"fmt"
"strings"
"github.com/sooflee/advent/internal"
)
type day10 struct {
lines []string
}
func Day10(part int, file string) int {
in := day10{
lines: internal.Read(file),
}
switch part {
case 1:
return in.part_one()
case 2:
return in.part_two()
default:
return 0
}
}
func update_signal_strength(signal_strength *int, cycle_number int, curr_X int) {
if cycle_number%40 == 20 {
curr_strength := curr_X * cycle_number
*signal_strength = *signal_strength + curr_strength
}
}
func (input day10) part_one() int {
curr_X := 1
cycle_number := 0
signal_strength := new(int)
*signal_strength = 0
for _, line := range input.lines {
if line == "noop" {
cycle_number++
update_signal_strength(signal_strength, cycle_number, curr_X)
} else {
cycle_number++
update_signal_strength(signal_strength, cycle_number, curr_X)
cycle_number++
update_signal_strength(signal_strength, cycle_number, curr_X)
parsed := strings.Split(line, " ")
curr_X = curr_X + internal.StrToInt(parsed[1])
}
}
return *signal_strength
}
func mark_output(output *[240]string, curr_X int, cycle_number int) *[240]string {
if cycle_number > 239 || cycle_number < 0 {
return output
}
if cycle_number%40 == curr_X-1 || cycle_number%40 == curr_X || cycle_number%40 == curr_X+1 {
output[cycle_number] = "#"
} else {
output[cycle_number] = "."
}
return output
}
func print_output(output *[240]string) {
for i := 0; i < 6; i++ {
fmt.Println(output[i*40 : (i+1)*40-1])
}
}
func (input day10) part_two() int {
curr_X := 1
cycle_number := 0
output := new([240]string)
curr_X_arr := [240]int{}
for _, line := range input.lines {
if line == "noop" {
cycle_number++
curr_X_arr[cycle_number-1] = curr_X
} else {
cycle_number++
curr_X_arr[cycle_number-1] = curr_X
cycle_number++
curr_X_arr[cycle_number-1] = curr_X
parsed := strings.Split(line, " ")
curr_X = curr_X + internal.StrToInt(parsed[1])
}
}
for i, curr_X_val := range curr_X_arr {
output = mark_output(output, curr_X_val, i)
}
print_output(output)
return 0
}