logo

adventofcode

Code used to solve https://adventofcode.com/, one branch per year
commit: 519e35d37da4221abe56a4ea21775cc29c5fbf39
parent: d54a4164b3a23faf48d61643c7007164d1b2a9b9
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Tue,  3 Dec 2019 12:14:44 +0100

day2: Dump current code

Diffstat:

Aday2.go65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ago.mod3+++
2 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/day2.go b/day2.go @@ -0,0 +1,65 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +func line_positions(line string) [][2]int { + line_split := strings.Split(line, ",") + fmt.Printf("%9s\n", line_split) + position := [2]int{0, 0} // (x,y) + positions := make([][2]int, len(line_split)) + + for i, move := range line_split { + direction := strings.Split(move, "")[0] + amount, err := strconv.Atoi(move[1:]) + if err != nil { + fmt.Printf("Error: %s", err) + } + + switch direction { + case "U": + position[0] += amount + case "D": + position[0] -= amount + case "L": + position[1] += amount + case "R": + position[1] -= amount + } + + positions[i] = position + + // fmt.Printf("%s%02d(% 4d,% 4d)\n", direction, amount, position[0], position[1]) + } + + fmt.Printf("%3d\n", positions) + + return positions +} + +func Intersection(a, b [][2]int) (c [][2]int) { + m := make(map[[2]int]bool) + + for _, item := range a { + m[item] = true + } + + for _, item := range b { + if _, ok := m[item]; ok { + c = append(c, item) + } + } + return +} + +func main() { + //line1 := line_positions("R75,D30,R83,U83,L12,D49,R71,U7,L72") + //line2 := line_positions("U62,R66,U55,R34,D71,R55,D58,R83") + line1 := line_positions("R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51") + line2 := line_positions("U98,R91,D20,R16,D67,R40,U7,R15,U6,R7") + + fmt.Printf("%3d\n", Intersection(line1, line2)) +} diff --git a/go.mod b/go.mod @@ -0,0 +1,3 @@ +module hacktivis.me/git/adventofcode + +go 1.12