day1.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package main
  2. import (
  3. "bufio"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "strconv"
  8. )
  9. func main() {
  10. args := flag.Bool("example", false, "example or input, just type the '-example' for example")
  11. // Read input from file
  12. flag.Parse()
  13. filename := "input.txt"
  14. if *args {
  15. filename = "example.txt"
  16. }
  17. file, err := os.Open(filename)
  18. if err != nil {
  19. panic("Can't read the file")
  20. }
  21. defer file.Close()
  22. reader := bufio.NewReader(file)
  23. dial := 50
  24. zero_times := 0
  25. cross_zero_times := 0
  26. was_zero := false
  27. for {
  28. instruction, _, _ := reader.ReadLine()
  29. if len(instruction) == 0 {
  30. break
  31. }
  32. direction := string(instruction[0])
  33. amount, _ := strconv.Atoi(string(instruction[1:]))
  34. if amount > 100 {
  35. cross_zero_times += amount / 100
  36. amount = amount % 100
  37. }
  38. switch direction {
  39. case "R":
  40. dial = dial + amount
  41. if dial > 100 {
  42. cross_zero_times += 1
  43. }
  44. case "L":
  45. dial = dial - amount
  46. if (dial < 0) && !was_zero {
  47. dial = 100 + dial
  48. cross_zero_times += 1
  49. }
  50. }
  51. dial = (dial%100 + 100) % 100
  52. fmt.Println(dial)
  53. if dial == 0 {
  54. zero_times += 1
  55. was_zero = true
  56. } else {
  57. was_zero = false
  58. }
  59. }
  60. fmt.Println("Zero Times", zero_times)
  61. fmt.Println("Cross Zero Times and Sum", cross_zero_times, cross_zero_times+zero_times)
  62. }