]> fbox.kageds.com Git - adventofcode.git/blob - 2022/go/day10/day10.go
day3 and 4
[adventofcode.git] / 2022 / go / day10 / day10.go
1 package day10
2
3 import (
4 "adventofcode2022/utils"
5 _ "fmt"
6 "strings"
7 )
8
9 func Part1(input string) int {
10 lines := strings.Split(input, "\n")
11 cycle := 0
12 strength := 0
13 x := 1
14 for _, line := range lines {
15 cmd := strings.Split(line, " ")
16 switch {
17 case cmd[0] == "noop":
18 strength,cycle = cycles_1(1, cycle, strength, x)
19 case cmd[0] == "addx":
20 strength, cycle = cycles_1(2, cycle, strength, x)
21 x += utils.MustAtoi(cmd[1])
22 }
23 }
24 return strength
25 }
26
27 func Part2(input string) string {
28 lines := strings.Split(input, "\n")
29 cycle := 0
30 x := 1
31 crt := [6][40]string{}
32 for _, line := range lines {
33 cmd := strings.Split(line, " ")
34 switch {
35 case cmd[0] == "noop":
36 cycle = cycles_2(1, cycle, x, &crt)
37 case cmd[0] == "addx":
38 cycle = cycles_2(2, cycle, x, &crt)
39 x += utils.MustAtoi(cmd[1])
40 }
41 }
42 output := "\n"
43 for i:=0;i<6;i++ {
44 for j:=0;j<40;j++ {
45 output = output + crt[i][j]
46 }
47 output = output + "\n"
48 }
49
50 return output
51 }
52
53 func cycles_1(num int, cycle int, strength int, x int) (int, int) {
54 for i:=0;i<num;i++ {
55 cycle++
56 if cycle == 20 {
57 strength = strength + (x * cycle)
58 } else if (cycle - 20) % 40 == 0 {
59 strength = strength + (x * cycle)
60 }
61 }
62 return strength, cycle
63 }
64
65 func cycles_2(num int, cycle int, x int, crt *[6][40]string) int {
66 for i:=0;i<num;i++ {
67 crt_x := cycle / 40
68 crt_y := cycle % 40
69 if x-1 == crt_y || x == crt_y || x+1 == crt_y {
70 crt[crt_x][crt_y] = "#"
71 } else {
72 crt[crt_x][crt_y] = " "
73 }
74 cycle++
75 }
76 return cycle
77 }