code.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "unicode"
  10. )
  11. type operation struct {
  12. op string
  13. a byte
  14. isBNumber bool
  15. b int
  16. bC byte
  17. }
  18. func readInput(file string) [][]operation {
  19. content, err := ioutil.ReadFile(file)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. lines := strings.Split(string(content), "\n")
  24. var input [][]operation
  25. var operations []operation
  26. for _, line := range lines {
  27. if line == "" {
  28. continue
  29. }
  30. parts := strings.Split(line, " ")
  31. if len(parts) < 2 {
  32. log.Fatal("Invalid line: ", line)
  33. }
  34. if parts[0] == "inp" && len(operations) > 0 {
  35. input = append(input, operations)
  36. operations = []operation{}
  37. }
  38. current := operation{op: parts[0], a: parts[1][0]}
  39. if len(parts) > 2 {
  40. if unicode.IsDigit(rune(parts[2][0])) {
  41. current.b, err = strconv.Atoi(parts[2])
  42. if err != nil {
  43. log.Fatal(err)
  44. }
  45. current.isBNumber = true
  46. } else {
  47. current.bC = parts[2][0]
  48. }
  49. }
  50. operations = append(operations, current)
  51. }
  52. if len(operations) > 0 {
  53. input = append(input, operations)
  54. }
  55. return input
  56. }
  57. func main() {
  58. if len(os.Args) < 2 {
  59. log.Fatal("Please provide a file name as argument")
  60. }
  61. input := readInput(os.Args[1])
  62. fmt.Println(input)
  63. }