code.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "log"
  6. "os"
  7. )
  8. type Rotation struct {
  9. Direction byte
  10. Clicks int
  11. }
  12. func readInput(file *os.File) []Rotation {
  13. scanner := bufio.NewScanner(file)
  14. var rotations []Rotation
  15. for scanner.Scan() {
  16. line := scanner.Text()
  17. if line == "" {
  18. break
  19. }
  20. var direction byte
  21. var clicks int
  22. n, err := fmt.Sscanf(line, "%c%d", &direction, &clicks)
  23. if n != 2 || err != nil {
  24. log.Fatalf("Bad input: %s", line)
  25. }
  26. rotations = append(rotations, Rotation{Direction: direction, Clicks: clicks})
  27. }
  28. return rotations
  29. }
  30. func part1(rotations []Rotation) int {
  31. var zeros int
  32. dial := 50
  33. for _, rotation := range rotations {
  34. if rotation.Direction == 'L' {
  35. dial -= rotation.Clicks
  36. } else {
  37. dial += rotation.Clicks
  38. }
  39. dial %= 100
  40. if dial < 0 {
  41. dial += 100
  42. }
  43. if dial == 0 {
  44. zeros++
  45. }
  46. }
  47. return zeros
  48. }
  49. func main() {
  50. if len(os.Args) < 2 {
  51. log.Fatal("You need to specify a file!")
  52. }
  53. filePath := os.Args[1]
  54. file, err := os.Open(filePath)
  55. if err != nil {
  56. log.Fatalf("Failed to open %s!\n", filePath)
  57. }
  58. rotations := readInput(file)
  59. fmt.Println("Part1:", part1(rotations))
  60. }