tidy up
This commit is contained in:
+7
-6
@@ -2,13 +2,14 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const bitSize = 40
|
||||
const bitSize = 12
|
||||
|
||||
func bitComplement(num uint64) uint64 {
|
||||
return ^num & (1<<bitSize - 1)
|
||||
@@ -56,7 +57,7 @@ func partTwo(s []string) (int64, int64) {
|
||||
}
|
||||
|
||||
func solve() {
|
||||
file, err := os.Open("bigboi.txt")
|
||||
file, err := os.Open("input.txt")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@@ -81,10 +82,10 @@ func solve() {
|
||||
total++
|
||||
}
|
||||
|
||||
partOne(&counts, &total)
|
||||
//fmt.Println(int64(g) * int64(e))
|
||||
partTwo(s)
|
||||
//fmt.Println(o * c)
|
||||
gamma, epsilon := partOne(&counts, &total)
|
||||
fmt.Println(int64(gamma) * int64(epsilon))
|
||||
o2, co2 := partTwo(s)
|
||||
fmt.Println(o2 * co2)
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
+21
-9
@@ -28,13 +28,12 @@ func createLine(s []string) *Line {
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *Line) Walk() []Point {
|
||||
var p []Point
|
||||
|
||||
func (l *Line) Walk() (p []Point, isDiagonal bool) {
|
||||
xstart, xend := minMax(l.start.x, l.end.x)
|
||||
ystart, yend := minMax(l.start.y, l.end.y)
|
||||
// diagonal line
|
||||
if xend-xstart == yend-ystart {
|
||||
isDiagonal = true
|
||||
for x, y := l.start.x, l.start.y; ; {
|
||||
p = append(p, Point{x: x, y: y})
|
||||
if x == l.end.x {
|
||||
@@ -62,7 +61,7 @@ func (l *Line) Walk() []Point {
|
||||
p = append(p, Point{x: l.start.x, y: y})
|
||||
}
|
||||
}
|
||||
return p
|
||||
return
|
||||
}
|
||||
|
||||
func minMax(a int, b int) (min int, max int) {
|
||||
@@ -87,23 +86,36 @@ func main() {
|
||||
r := regexp.MustCompile("[0-9]+")
|
||||
|
||||
floorMap := make(map[Point]int)
|
||||
var overlapCount int
|
||||
diags := make([]Point, 0)
|
||||
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
coords := r.FindAllString(text, -1)
|
||||
line := createLine(coords)
|
||||
points := line.Walk()
|
||||
points, isDiagonal := line.Walk()
|
||||
|
||||
if isDiagonal {
|
||||
diags = append(diags, points...)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, p := range points {
|
||||
if floorMap[p] == 1 {
|
||||
overlapCount++
|
||||
}
|
||||
floorMap[p]++
|
||||
}
|
||||
}
|
||||
|
||||
var overlapCount int
|
||||
for _, v := range floorMap {
|
||||
if v > 1 {
|
||||
fmt.Println("Part one: ", overlapCount)
|
||||
|
||||
for _, p := range diags {
|
||||
if floorMap[p] == 1 {
|
||||
overlapCount++
|
||||
}
|
||||
floorMap[p]++
|
||||
}
|
||||
fmt.Println(overlapCount)
|
||||
|
||||
fmt.Println("Part two: ", overlapCount)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user