diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-15 18:53:13 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:33:16 +0100 |
commit | 6f40d23c5ed72ef7238c838f2373c1f202cb62bb (patch) | |
tree | f46ea615ac996d01b52394fd2c9b413467cc8512 /day14/main.go | |
parent | be26f6cb3526757e089e879c0c3b53db5d1d363e (diff) |
Fucking day 14
Diffstat (limited to 'day14/main.go')
-rw-r--r-- | day14/main.go | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/day14/main.go b/day14/main.go new file mode 100644 index 0000000..a4a2da2 --- /dev/null +++ b/day14/main.go @@ -0,0 +1,100 @@ +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 +} |