re-organise repo
This commit is contained in:
77
2022/go/day10/day10.go
Normal file
77
2022/go/day10/day10.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package day10
|
||||
|
||||
import (
|
||||
"adventofcode2022/utils"
|
||||
_ "fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func Part1(input string) int {
|
||||
lines := strings.Split(input, "\n")
|
||||
cycle := 0
|
||||
strength := 0
|
||||
x := 1
|
||||
for _, line := range lines {
|
||||
cmd := strings.Split(line, " ")
|
||||
switch {
|
||||
case cmd[0] == "noop":
|
||||
strength,cycle = cycles_1(1, cycle, strength, x)
|
||||
case cmd[0] == "addx":
|
||||
strength, cycle = cycles_1(2, cycle, strength, x)
|
||||
x += utils.MustAtoi(cmd[1])
|
||||
}
|
||||
}
|
||||
return strength
|
||||
}
|
||||
|
||||
func Part2(input string) string {
|
||||
lines := strings.Split(input, "\n")
|
||||
cycle := 0
|
||||
x := 1
|
||||
crt := [6][40]string{}
|
||||
for _, line := range lines {
|
||||
cmd := strings.Split(line, " ")
|
||||
switch {
|
||||
case cmd[0] == "noop":
|
||||
cycle = cycles_2(1, cycle, x, &crt)
|
||||
case cmd[0] == "addx":
|
||||
cycle = cycles_2(2, cycle, x, &crt)
|
||||
x += utils.MustAtoi(cmd[1])
|
||||
}
|
||||
}
|
||||
output := "\n"
|
||||
for i:=0;i<6;i++ {
|
||||
for j:=0;j<40;j++ {
|
||||
output = output + crt[i][j]
|
||||
}
|
||||
output = output + "\n"
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
func cycles_1(num int, cycle int, strength int, x int) (int, int) {
|
||||
for i:=0;i<num;i++ {
|
||||
cycle++
|
||||
if cycle == 20 {
|
||||
strength = strength + (x * cycle)
|
||||
} else if (cycle - 20) % 40 == 0 {
|
||||
strength = strength + (x * cycle)
|
||||
}
|
||||
}
|
||||
return strength, cycle
|
||||
}
|
||||
|
||||
func cycles_2(num int, cycle int, x int, crt *[6][40]string) int {
|
||||
for i:=0;i<num;i++ {
|
||||
crt_x := cycle / 40
|
||||
crt_y := cycle % 40
|
||||
if x-1 == crt_y || x == crt_y || x+1 == crt_y {
|
||||
crt[crt_x][crt_y] = "#"
|
||||
} else {
|
||||
crt[crt_x][crt_y] = " "
|
||||
}
|
||||
cycle++
|
||||
}
|
||||
return cycle
|
||||
}
|
||||
314
2022/go/day10/day10_test.go
Normal file
314
2022/go/day10/day10_test.go
Normal file
@@ -0,0 +1,314 @@
|
||||
package day10
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPart1(t *testing.T) {
|
||||
r := Part1(
|
||||
`addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop`)
|
||||
require.Equal(t, 13140, r)
|
||||
}
|
||||
|
||||
func TestPart2(t *testing.T) {
|
||||
r := Part2(
|
||||
`addx 15
|
||||
addx -11
|
||||
addx 6
|
||||
addx -3
|
||||
addx 5
|
||||
addx -1
|
||||
addx -8
|
||||
addx 13
|
||||
addx 4
|
||||
noop
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 5
|
||||
addx -1
|
||||
addx -35
|
||||
addx 1
|
||||
addx 24
|
||||
addx -19
|
||||
addx 1
|
||||
addx 16
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -15
|
||||
noop
|
||||
noop
|
||||
addx -3
|
||||
addx 9
|
||||
addx 1
|
||||
addx -3
|
||||
addx 8
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -36
|
||||
noop
|
||||
addx 1
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 1
|
||||
noop
|
||||
addx -13
|
||||
addx 13
|
||||
addx 7
|
||||
noop
|
||||
addx 1
|
||||
addx -33
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 8
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx 17
|
||||
addx -9
|
||||
addx 1
|
||||
addx 1
|
||||
addx -3
|
||||
addx 11
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx -13
|
||||
addx -19
|
||||
addx 1
|
||||
addx 3
|
||||
addx 26
|
||||
addx -30
|
||||
addx 12
|
||||
addx -1
|
||||
addx 3
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -9
|
||||
addx 18
|
||||
addx 1
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 9
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx 2
|
||||
addx -37
|
||||
addx 1
|
||||
addx 3
|
||||
noop
|
||||
addx 15
|
||||
addx -21
|
||||
addx 22
|
||||
addx -6
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -10
|
||||
noop
|
||||
noop
|
||||
addx 20
|
||||
addx 1
|
||||
addx 2
|
||||
addx 2
|
||||
addx -6
|
||||
addx -11
|
||||
noop
|
||||
noop
|
||||
noop`)
|
||||
require.Equal(t, `##..##..##..##..##..##..##..##..##..##..
|
||||
###...###...###...###...###...###...###.
|
||||
####....####....####....####....####....
|
||||
#####.....#####.....#####.....#####.....
|
||||
######......######......######......####
|
||||
#######.......#######.......#######.....`, r)
|
||||
}
|
||||
140
2022/go/day10/input.txt
Normal file
140
2022/go/day10/input.txt
Normal file
@@ -0,0 +1,140 @@
|
||||
addx 1
|
||||
addx 4
|
||||
addx 21
|
||||
addx -20
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 3
|
||||
noop
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx 5
|
||||
addx 2
|
||||
addx 1
|
||||
noop
|
||||
addx -37
|
||||
addx 22
|
||||
addx -4
|
||||
addx -14
|
||||
addx 2
|
||||
addx 5
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -15
|
||||
addx 32
|
||||
addx -14
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
noop
|
||||
addx -13
|
||||
addx -2
|
||||
addx 18
|
||||
addx -36
|
||||
noop
|
||||
addx 11
|
||||
addx -7
|
||||
noop
|
||||
noop
|
||||
addx 6
|
||||
addx 22
|
||||
addx -21
|
||||
addx 3
|
||||
addx 2
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -16
|
||||
addx 17
|
||||
addx 2
|
||||
addx 5
|
||||
addx -11
|
||||
addx 15
|
||||
addx -15
|
||||
addx -24
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
addx 2
|
||||
addx -6
|
||||
addx 9
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx -3
|
||||
addx 4
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx -28
|
||||
addx 29
|
||||
noop
|
||||
addx 3
|
||||
addx -7
|
||||
addx -29
|
||||
noop
|
||||
addx 7
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -3
|
||||
addx 4
|
||||
addx 5
|
||||
addx 2
|
||||
addx 8
|
||||
addx -30
|
||||
addx 25
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx -10
|
||||
addx -24
|
||||
addx 2
|
||||
noop
|
||||
noop
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
addx 2
|
||||
addx 8
|
||||
noop
|
||||
noop
|
||||
addx -1
|
||||
addx -9
|
||||
addx 14
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
Reference in New Issue
Block a user