advent2022

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

commit 31d2c30ef8d57ac6913a18cff3a07a5a8f193ea1
parent 466b1eb9bd144df6d5fa88b8c7cb1d761082131c
Author: bsandro <email@bsandro.tech>
Date:   Thu, 15 Dec 2022 07:23:18 +0000

Day 15 part 1

Diffstat:
Aday15/input.txt | 40++++++++++++++++++++++++++++++++++++++++
Aday15/main.go | 100+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aday15/sample.txt | 14++++++++++++++
3 files changed, 154 insertions(+), 0 deletions(-)

diff --git a/day15/input.txt b/day15/input.txt @@ -0,0 +1,40 @@ +Sensor at x=1112863, y=496787: closest beacon is at x=1020600, y=2000000 +Sensor at x=2980210, y=1712427: closest beacon is at x=2946825, y=1712605 +Sensor at x=2799204, y=1425283: closest beacon is at x=2946825, y=1712605 +Sensor at x=3999908, y=2754283: closest beacon is at x=4064129, y=2651511 +Sensor at x=760990, y=1455625: closest beacon is at x=1020600, y=2000000 +Sensor at x=3996490, y=3239979: closest beacon is at x=4064129, y=2651511 +Sensor at x=3347352, y=3603589: closest beacon is at x=3621840, y=3614596 +Sensor at x=2888433, y=2337157: closest beacon is at x=2946825, y=1712605 +Sensor at x=3423261, y=2191958: closest beacon is at x=3153728, y=1862250 +Sensor at x=1160237, y=3999960: closest beacon is at x=109153, y=3585462 +Sensor at x=693519, y=3701289: closest beacon is at x=109153, y=3585462 +Sensor at x=2615270, y=2824808: closest beacon is at x=2554122, y=2935074 +Sensor at x=3046971, y=1755494: closest beacon is at x=2946825, y=1712605 +Sensor at x=139591, y=1186912: closest beacon is at x=1020600, y=2000000 +Sensor at x=2309134, y=47090: closest beacon is at x=3211831, y=-792661 +Sensor at x=1849154, y=1377259: closest beacon is at x=2946825, y=1712605 +Sensor at x=2515971, y=2851853: closest beacon is at x=2554122, y=2935074 +Sensor at x=2524614, y=2738138: closest beacon is at x=2554122, y=2935074 +Sensor at x=3811778, y=1370280: closest beacon is at x=3153728, y=1862250 +Sensor at x=2615590, y=3819371: closest beacon is at x=2554122, y=2935074 +Sensor at x=3996286, y=3719213: closest beacon is at x=3621840, y=3614596 +Sensor at x=3963152, y=2368927: closest beacon is at x=4064129, y=2651511 +Sensor at x=3495504, y=3076982: closest beacon is at x=3621840, y=3614596 +Sensor at x=3725521, y=2560764: closest beacon is at x=4064129, y=2651511 +Sensor at x=952643, y=2385401: closest beacon is at x=1020600, y=2000000 +Sensor at x=3934384, y=2596106: closest beacon is at x=4064129, y=2651511 +Sensor at x=3060628, y=3082730: closest beacon is at x=2554122, y=2935074 +Sensor at x=3468382, y=3916817: closest beacon is at x=3621840, y=3614596 +Sensor at x=3300107, y=469364: closest beacon is at x=3211831, y=-792661 +Sensor at x=2306388, y=1932261: closest beacon is at x=2946825, y=1712605 +Sensor at x=1965, y=3514070: closest beacon is at x=109153, y=3585462 +Sensor at x=3081537, y=1841861: closest beacon is at x=3153728, y=1862250 +Sensor at x=2997643, y=1729779: closest beacon is at x=2946825, y=1712605 +Sensor at x=21714, y=3624181: closest beacon is at x=109153, y=3585462 +Sensor at x=1549467, y=3109269: closest beacon is at x=2554122, y=2935074 +Sensor at x=3722307, y=3839410: closest beacon is at x=3621840, y=3614596 +Sensor at x=3848580, y=3544878: closest beacon is at x=3621840, y=3614596 +Sensor at x=1189516, y=2153239: closest beacon is at x=1020600, y=2000000 +Sensor at x=468190, y=1889204: closest beacon is at x=1020600, y=2000000 +Sensor at x=270403, y=2762568: closest beacon is at x=109153, y=3585462 diff --git a/day15/main.go b/day15/main.go @@ -0,0 +1,100 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "math" + "strconv" +) + +type Pos interface { + GetX() int + GetY() int +} + +func Distance(p1 Pos, p2 Pos) int { + return int(math.Abs(float64(p1.GetX()-p2.GetX()))+math.Abs(float64(p1.GetY()-p2.GetY()))) +} + +type Sensor struct { + x, y, r int +} +func (s Sensor) GetX() int { return s.x } +func (s Sensor) GetY() int { return s.y } + +type Vec2 struct { + x, y int +} +func (v Vec2) GetX() int { return v.x } +func (v Vec2) GetY() int { return v.y } + +type Sensors []Sensor + +type Beacons []Vec2 +func (bs Beacons) Have(b1 Vec2) bool { + for _, b := range bs { + if b1.x == b.x && b1.y == b.y { + return true + } + } + return false +} + +func main() { + if len(os.Args) > 2 { + y, err:=strconv.Atoi(os.Args[2]) + if err!=nil{log.Fatal(err)} + day15(os.Args[1], y) + } else if len(os.Args) == 1 { + fmt.Printf("usage: %s inputfile.txt ROW_NUM\n", os.Args[0]) + } else { + fmt.Println("No input data") + } +} + +func day15(input_file string, target_y int) { + fmt.Printf("day 15 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 sensors Sensors + var beacons Beacons + for scanner.Scan() { + in := scanner.Text() + var s Sensor + var b Vec2 + num, err := fmt.Sscanf(in, "Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d", &s.x, &s.y, &b.x, &b.y) + if num != 4 || err != nil { + log.Fatal("Parsing error", err) + } + s.r = Distance(s, b) + sensors = append(sensors, s) + if !beacons.Have(b) { + beacons = append(beacons, b) + } + } + if err = scanner.Err(); err != nil { + log.Fatal(err) + } + + points := make(map[int]bool) + for _, s := range sensors { + if Distance(s, Vec2{x:s.x,y:target_y}) <= s.r { + for x:=s.x-s.r;x<=s.x+s.r;x++{ + p:=Vec2{x:x,y:target_y} + if Distance(s, p) <= s.r { + if !beacons.Have(p) { + points[x]=true + } + } + } + } + } + + fmt.Println("points:", len(points)) +} diff --git a/day15/sample.txt b/day15/sample.txt @@ -0,0 +1,14 @@ +Sensor at x=2, y=18: closest beacon is at x=-2, y=15 +Sensor at x=9, y=16: closest beacon is at x=10, y=16 +Sensor at x=13, y=2: closest beacon is at x=15, y=3 +Sensor at x=12, y=14: closest beacon is at x=10, y=16 +Sensor at x=10, y=20: closest beacon is at x=10, y=16 +Sensor at x=14, y=17: closest beacon is at x=10, y=16 +Sensor at x=8, y=7: closest beacon is at x=2, y=10 +Sensor at x=2, y=0: closest beacon is at x=2, y=10 +Sensor at x=0, y=11: closest beacon is at x=2, y=10 +Sensor at x=20, y=14: closest beacon is at x=25, y=17 +Sensor at x=17, y=20: closest beacon is at x=21, y=22 +Sensor at x=16, y=7: closest beacon is at x=15, y=3 +Sensor at x=14, y=3: closest beacon is at x=15, y=3 +Sensor at x=20, y=1: closest beacon is at x=15, y=3