code.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type op byte
  9. const (
  10. addx op = iota
  11. noop
  12. )
  13. type command struct {
  14. instruction op
  15. value int
  16. }
  17. func readInput(file *os.File) []command {
  18. scanner := bufio.NewScanner(file)
  19. var commands []command
  20. for scanner.Scan() {
  21. line := scanner.Text()
  22. if line == "" {
  23. continue
  24. }
  25. var current command
  26. if line[0] == 'n' {
  27. current.instruction = noop
  28. } else {
  29. current.instruction = addx
  30. n, err := fmt.Sscanf(line, "addx %d", &current.value)
  31. if n != 1 || err != nil {
  32. log.Fatal("Can't parse input:", err, line)
  33. }
  34. }
  35. commands = append(commands, current)
  36. }
  37. return commands
  38. }
  39. func part1(commands []command) int {
  40. cycles := 0
  41. currentLimit := 20
  42. absoluteLimit := 221
  43. result := 0
  44. x := 1
  45. for i := range commands {
  46. if cycles > absoluteLimit {
  47. break
  48. }
  49. if commands[i].instruction == noop {
  50. cycles++
  51. } else {
  52. cycles += 2
  53. if cycles >= currentLimit {
  54. result += currentLimit * x
  55. currentLimit += 40
  56. }
  57. x += commands[i].value
  58. }
  59. }
  60. return result
  61. }
  62. func main() {
  63. if len(os.Args) < 2 {
  64. log.Fatal("You need to specify a file!")
  65. }
  66. filePath := os.Args[1]
  67. file, err := os.Open(filePath)
  68. if err != nil {
  69. log.Fatalf("Failed to open %s!\n", filePath)
  70. }
  71. commands := readInput(file)
  72. fmt.Println("Part1:", part1(commands))
  73. }