Skip to content

Instantly share code, notes, and snippets.

@Nv7-GitHub
Created December 5, 2021 07:39
Show Gist options
  • Save Nv7-GitHub/b213fb1fbf5b764d6a2f98182efaaa4e to your computer and use it in GitHub Desktop.
Save Nv7-GitHub/b213fb1fbf5b764d6a2f98182efaaa4e to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 5
package main
import (
_ "embed"
"fmt"
"strconv"
"strings"
)
//go:embed input.txt
var input string
func getInts(pos []string) Pos {
v1, err := strconv.Atoi(pos[0])
if err != nil {
panic(err)
}
v2, err := strconv.Atoi(pos[1])
if err != nil {
panic(err)
}
return Pos{v1, v2}
}
type Pos struct {
X int
Y int
}
type Field struct {
Values [][]int
}
func (f *Field) AddLine(pos1, pos2 Pos) {
// Horizontal line
if pos1.Y == pos2.Y {
if pos1.X > pos2.X {
buff := pos1.X
pos1.X = pos2.X
pos2.X = buff
}
for x := pos1.X; x <= pos2.X; x++ {
f.Values[pos1.Y][x]++
}
}
// Vertical line
if pos1.X == pos2.X {
if pos1.Y > pos2.Y {
buff := pos2.Y
pos2.Y = pos1.Y
pos1.Y = buff
}
for y := pos1.Y; y <= pos2.Y; y++ {
f.Values[y][pos1.X]++
}
}
}
func (f *Field) NumOverlapping() int {
out := 0
for _, line := range f.Values {
for _, v := range line {
if v > 1 {
out++
}
}
}
return out
}
func main() {
lines := strings.Split(input, "\n")
data := make([][2]Pos, 0)
for _, line := range lines {
vals := strings.Split(line, " -> ")
pos1 := getInts(strings.Split(vals[0], ","))
pos2 := getInts(strings.Split(vals[1], ","))
data = append(data, [2]Pos{pos1, pos2})
}
// Calculate size
width := 0
height := 0
for _, line := range data {
pos1 := line[0]
pos2 := line[1]
if pos1.X > width {
width = pos1.X
}
if pos1.Y > height {
height = pos1.Y
}
if pos2.X > width {
width = pos2.X
}
if pos2.Y > height {
height = pos2.Y
}
}
vals := make([][]int, height+1)
for i := range vals {
vals[i] = make([]int, width+1)
}
field := &Field{vals}
for _, line := range data {
field.AddLine(line[0], line[1])
}
fmt.Println(field.NumOverlapping())
}
package main
import (
_ "embed"
"fmt"
"math"
"strconv"
"strings"
)
//go:embed input.txt
var input string
type Direction int
const (
UpRight Direction = iota
DownRight
DownLeft
UpLeft
)
func getInts(pos []string) Pos {
v1, err := strconv.Atoi(pos[0])
if err != nil {
panic(err)
}
v2, err := strconv.Atoi(pos[1])
if err != nil {
panic(err)
}
return Pos{v1, v2}
}
type Pos struct {
X int
Y int
}
type Field struct {
Values [][]int
}
func (f *Field) AddLine(pos1, pos2 Pos) {
// Horizontal line
if pos1.Y == pos2.Y {
if pos1.X > pos2.X {
buff := pos1.X
pos1.X = pos2.X
pos2.X = buff
}
for x := pos1.X; x <= pos2.X; x++ {
f.Values[pos1.Y][x]++
}
} else if pos1.X == pos2.X { // Vertical
if pos1.Y > pos2.Y {
buff := pos2.Y
pos2.Y = pos1.Y
pos1.Y = buff
}
for y := pos1.Y; y <= pos2.Y; y++ {
f.Values[y][pos1.X]++
}
} else if pos1.X != pos2.X && pos1.Y != pos2.Y { // Diagonal
// Figure out direction
dir := UpRight
if pos1.X < pos2.X {
if pos1.Y < pos2.Y {
dir = UpRight
} else {
dir = DownRight
}
} else {
if pos1.Y < pos2.Y {
dir = UpLeft
} else {
dir = DownLeft
}
}
// Put in vals
x := pos1.X
y := pos1.Y
length := int(math.Abs(float64(pos1.X-pos2.X))) + 1
for i := 0; i < length; i++ {
f.Values[y][x]++
switch dir {
case UpRight:
x++
y++
case DownRight:
x++
y--
case DownLeft:
x--
y--
case UpLeft:
x--
y++
}
}
}
}
func (f *Field) NumOverlapping() int {
out := 0
for _, line := range f.Values {
for _, v := range line {
if v > 1 {
out++
}
}
}
return out
}
func main() {
lines := strings.Split(input, "\n")
data := make([][2]Pos, 0)
for _, line := range lines {
vals := strings.Split(line, " -> ")
pos1 := getInts(strings.Split(vals[0], ","))
pos2 := getInts(strings.Split(vals[1], ","))
data = append(data, [2]Pos{pos1, pos2})
}
// Calculate size
width := 0
height := 0
for _, line := range data {
pos1 := line[0]
pos2 := line[1]
if pos1.X > width {
width = pos1.X
}
if pos1.Y > height {
height = pos1.Y
}
if pos2.X > width {
width = pos2.X
}
if pos2.Y > height {
height = pos2.Y
}
}
vals := make([][]int, height+1)
for i := range vals {
vals[i] = make([]int, width+1)
}
field := &Field{vals}
for _, line := range data {
field.AddLine(line[0], line[1])
}
fmt.Println(field.NumOverlapping())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment