advent2022

Advent of Code 2022 Solutions
git clone git://bsandro.tech/advent2022
Log | Files | Refs | README | LICENSE

commit 5440938450c4f686a61b0d3acbec74f0858fa0ba
parent f0ffa82775a10d7fcc735c700966c446aba33c07
Author: bsandro <email@bsandro.tech>
Date:   Fri,  9 Dec 2022 06:36:21 +0000

Day 09 part 1

Diffstat:
Aday09/input.txt | 2000+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday09/main.go | 126+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday09/sample.txt | 8++++++++
3 files changed, 2134 insertions(+), 0 deletions(-)

diff --git a/day09/input.txt b/day09/input.txt @@ -0,0 +1,2000 @@ +U 2 +D 2 +R 1 +D 2 +R 2 +D 2 +R 2 +D 1 +L 2 +U 2 +L 2 +R 1 +L 1 +U 1 +L 2 +D 1 +R 2 +D 1 +U 2 +R 2 +D 2 +U 2 +R 2 +L 1 +D 2 +R 2 +U 2 +R 1 +L 1 +U 2 +R 1 +U 2 +L 1 +R 2 +L 1 +R 1 +U 2 +L 2 +R 1 +D 2 +U 1 +R 1 +L 1 +U 2 +D 2 +U 2 +R 1 +D 1 +L 2 +U 1 +R 2 +L 2 +D 1 +U 2 +D 2 +R 2 +D 2 +R 2 +D 1 +R 1 +D 1 +U 2 +L 1 +R 2 +D 2 +L 2 +R 1 +L 2 +R 1 +D 1 +R 2 +D 2 +U 2 +L 2 +U 2 +R 1 +L 1 +D 2 +U 1 +D 2 +L 2 +D 1 +U 2 +R 1 +D 2 +L 2 +U 2 +D 2 +R 2 +L 1 +R 2 +L 1 +U 2 +L 2 +U 2 +R 1 +D 2 +U 1 +D 2 +R 2 +U 2 +D 1 +U 1 +L 1 +R 1 +U 2 +R 2 +D 1 +R 2 +L 1 +U 2 +L 2 +R 1 +D 1 +R 2 +U 3 +R 2 +D 1 +L 1 +U 2 +D 1 +U 2 +D 3 +U 2 +R 3 +L 3 +R 2 +L 1 +U 2 +L 2 +U 2 +D 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+D 5 +R 2 +L 7 +U 7 +D 8 +L 17 +U 1 +R 16 +U 9 +L 13 +U 12 +R 2 +U 16 +D 8 +U 17 +L 10 +R 5 +U 11 +D 17 +L 15 +R 11 +U 7 +R 9 +L 4 +D 17 +L 2 +R 7 +D 17 +L 7 +U 12 +L 3 +U 13 +D 2 +L 8 +R 3 +D 17 +U 1 +R 17 +L 2 +U 17 +R 10 +U 1 +R 14 +U 8 +L 17 +D 3 +L 13 +D 10 +R 1 +L 11 +U 10 +L 14 +D 15 +R 14 +L 9 +U 1 +R 11 +L 13 +U 12 +L 9 +U 14 +L 12 +U 11 +L 6 +R 4 +D 2 +L 14 +R 13 +U 16 +L 2 +R 11 +D 18 +R 13 +U 3 +D 15 +L 3 +R 12 +U 10 +L 5 +R 3 +L 11 +U 14 +L 15 +R 3 +L 7 +D 5 +U 1 +R 6 +U 14 +D 17 +U 2 +D 15 +U 14 +D 1 +L 7 +D 1 +R 13 +L 9 +U 5 +L 2 +D 14 +U 14 +R 4 +U 14 +R 11 +U 8 +L 9 +D 15 +L 13 +U 17 +L 2 +U 1 +R 11 +U 7 +D 3 +U 8 +L 16 +D 3 +R 11 +L 17 +D 15 +R 16 +U 18 +D 8 +L 4 +U 2 +D 6 +R 15 +D 11 +L 12 +R 9 +D 18 +R 5 +D 1 +R 12 +L 5 +R 6 +L 6 +U 2 +L 15 +R 6 +L 16 +D 17 +U 11 +R 13 +L 10 +D 7 +L 15 +D 19 +R 17 +U 1 +R 19 +L 16 +U 18 +R 5 +L 6 +D 19 +L 1 +R 15 +L 16 +U 17 +D 18 +U 14 +L 19 +R 18 +L 12 +U 10 +D 2 +L 7 +R 1 +U 15 +L 1 +R 14 +U 10 +D 15 +U 4 +L 1 +U 11 +D 12 +U 7 +D 14 +R 14 +D 12 +L 17 +D 3 +R 6 +D 3 +R 4 +L 2 +R 5 +D 3 +L 10 +U 12 +L 12 +R 7 +D 12 +L 10 +R 10 +D 8 +L 13 +R 9 +U 18 +L 14 +R 1 +L 7 +D 18 +L 14 +U 11 +D 8 +R 13 +D 2 +R 10 +L 4 +R 1 +D 7 +U 18 +D 9 +L 5 +D 2 +U 2 +L 11 +R 2 +D 11 +L 13 +D 17 +L 7 +R 9 +L 9 +R 4 +D 7 diff --git a/day09/main.go b/day09/main.go @@ -0,0 +1,126 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "math" +) + +func abs(a int) int { + return int(math.Abs(float64(a))) +} + +type Pos struct { + x, y int +} +func (p Pos) Diff(o Pos) Pos { + var diff Pos + diff.x = p.x - o.x + diff.y = p.y - o.y + return diff +} +func (p Pos) Dist(o Pos) int { + // returns maximum of distances by X or Y axis + diff := p.Diff(o) + dx := abs(diff.x) + dy := abs(diff.y) + if dx > dy { + return dx + } else { + return dy + } +} +type Rope struct { + head, tail Pos + visited []Pos +} +func (r *Rope) AddVisited(p Pos) { + add := true + for _, v := range r.visited { + if v.x == p.x && v.y == p.y { + add = false + break + } + } + if add { + r.visited = append(r.visited, p) + } +} +func (r *Rope) MoveTail() bool { + if r.head.Dist(r.tail) > 1 { + d := r.head.Diff(r.tail) + if abs(d.x)==2 { + d.x /= 2 + } + if abs(d.y)==2 { + d.y /= 2 + } + r.tail.x += d.x + r.tail.y += d.y + return true + } + return false +} +func (r *Rope) Step(dx, dy int) { + r.head.x += dx + r.head.y += dy + if r.MoveTail() { + r.AddVisited(r.tail) + } +} +func (r *Rope) Move(dir string, dist int) { + //fmt.Println("direction:", dir, "distance:", dist) + for d:=0; d<dist; d++ { + switch dir { + case "L": + r.Step(-1, 0) + case "R": + r.Step(1, 0) + case "U": + r.Step(0, 1) + case "D": + r.Step(0, -1) + default: + log.Fatal("Invalid direction", dir) + } + } +} + +func main() { + if len(os.Args) > 1 { + day09(os.Args[1]) + } else if len(os.Args) == 1 { + fmt.Printf("usage: %s inputfile.txt\n", os.Args[0]) + } else { + fmt.Println("No input data") + } +} + +func day09(input_file string) { + fmt.Printf("day 09 input filename: %s\n", input_file) + input, err := os.Open(input_file) + if err != nil { + log.Fatal(err) + } + defer input.Close() + scanner := bufio.NewScanner(input) + var rope Rope + rope.AddVisited(rope.tail) + for scanner.Scan() { + in := scanner.Text() + var dir string + var dist int + found, err := fmt.Sscanf(in, "%s %d", &dir, &dist) + if err != nil || found != 2 { + log.Fatal("Invalid input string", in, err) + } + rope.Move(dir, dist) + } + if err = scanner.Err(); err != nil { + log.Fatal(err) + } + + fmt.Printf("visited: %d\n", len(rope.visited)) +} diff --git a/day09/sample.txt b/day09/sample.txt @@ -0,0 +1,8 @@ +R 4 +U 4 +L 3 +D 1 +R 4 +D 1 +L 5 +R 2