package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { fmt.Println(part1(readData("example.txt"), 11, 7), 12) fmt.Println(part1(readData("data.txt"), 101, 103)) part2(readData("data.txt"), 101, 103) } type Bot struct { x, y int dx, dy int } func part1(bots []Bot, w, h int) int { q1, q2, q3, q4 := 0, 0, 0, 0 hw := w / 2 hh := h / 2 for _, b := range bots { x := ((b.x+b.dx*100)%w + w) % w y := ((b.y+b.dy*100)%h + h) % h if x > hw { if y < hh { q2++ } else if y > hh { q4++ } } else if x < hw { if y < hh { q1++ } else if y > hh { q3++ } } } return q1 * q2 * q3 * q4 } func part2(bots []Bot, w, h int) { fp, err := os.Create("output.txt") if err != nil { panic(err) } buffer := make([]byte, (w+1)*h) for y := range h { buffer[y*(w+1)+w] = '\n' } for i := range 10000 { for y := range h { for x := range w { buffer[y*(w+1)+x] = ' ' } } for _, b := range bots { x := ((b.x+b.dx*i)%w + w) % w y := ((b.y+b.dy*i)%h + h) % h buffer[y*(w+1)+x] = 'X' } // So fucking stupid if strings.Index(string(buffer), "XXXXXXXX") >= 0 { fmt.Println(i) fmt.Fprintln(fp, i) fp.Write(buffer) } } } func parsePair(s string) (x, y int) { nums := strings.Split(s[2:], ",") x, _ = strconv.Atoi(nums[0]) y, _ = strconv.Atoi(nums[1]) return } func readData(fileName string) (bots []Bot) { fp, _ := os.Open(fileName) scanner := bufio.NewScanner(fp) for scanner.Scan() { line := strings.Split(strings.TrimSpace(scanner.Text()), " ") x, y := parsePair(line[0]) dx, dy := parsePair(line[1]) bots = append(bots, Bot{x, y, dx, dy}) } return }