|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +type Machine struct { |
| 11 | + Indicator uint16 |
| 12 | + Buttons []uint16 |
| 13 | + ButtonsInt [][]int |
| 14 | + Joltage []int |
| 15 | +} |
| 16 | + |
| 17 | +func parseInput() []Machine { |
| 18 | + var list []Machine |
| 19 | + |
| 20 | + for scanner := bufio.NewScanner(os.Stdin); scanner.Scan(); { |
| 21 | + tokens := strings.Split(scanner.Text(), " ") |
| 22 | + |
| 23 | + indicatorS := strings.Trim(tokens[0], "[]") |
| 24 | + var indicator uint16 |
| 25 | + for i, c := range indicatorS { |
| 26 | + if c == '#' { |
| 27 | + indicator |= 1 << uint16(len(indicatorS)-i-1) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + var buttons []uint16 |
| 32 | + var buttonsInt [][]int |
| 33 | + for i := 1; i < len(tokens)-1; i++ { |
| 34 | + buttonS := strings.Split(strings.Trim(tokens[i], "()"), ",") |
| 35 | + buttonInt := make([]int, len(buttonS)) |
| 36 | + var button uint16 |
| 37 | + for j, b := range buttonS { |
| 38 | + n, _ := strconv.Atoi(b) |
| 39 | + buttonInt[j] = n |
| 40 | + button |= 1 << uint16(len(indicatorS)-n-1) |
| 41 | + } |
| 42 | + buttons = append(buttons, button) |
| 43 | + buttonsInt = append(buttonsInt, buttonInt) |
| 44 | + } |
| 45 | + |
| 46 | + joltageS := strings.Split(strings.Trim(tokens[len(tokens)-1], "{}"), ",") |
| 47 | + joltage := make([]int, len(joltageS)) |
| 48 | + for i := range joltageS { |
| 49 | + joltage[i], _ = strconv.Atoi(joltageS[i]) |
| 50 | + } |
| 51 | + list = append(list, Machine{indicator, buttons, buttonsInt, joltage}) |
| 52 | + } |
| 53 | + return list |
| 54 | +} |
0 commit comments