7 Incheckningar 2280dbe24b ... 01a1057b0b

Upphovsman SHA1 Meddelande Datum
  Piotr Czajkowski 01a1057b0b Added description 1 vecka sedan
  Piotr Czajkowski c9fcd199e6 Cleanup 1 vecka sedan
  Piotr Czajkowski 3dd2abe2f2 Solved part2 1 vecka sedan
  Piotr Czajkowski 61123b8f93 Close, but no bisquit 1 vecka sedan
  Piotr Czajkowski 549ea37780 Lost for part2 1 vecka sedan
  Piotr Czajkowski 283a81136c Solved part1 1 vecka sedan
  Piotr Czajkowski c9a8f73fef Able to read input 1 vecka sedan
3 ändrade filer med 290 tillägg och 0 borttagningar
  1. 115 0
      08/code.go
  2. 125 0
      08/description.txt
  3. 50 0
      08/input

+ 115 - 0
08/code.go

@@ -0,0 +1,115 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+)
+
+type Point struct {
+	x, y int
+}
+
+func (p *Point) key() string {
+	return fmt.Sprintf("%d_%d", p.y, p.x)
+}
+
+func readInput(file *os.File) (map[byte][]Point, [][]byte) {
+	scanner := bufio.NewScanner(file)
+	var matrix [][]byte
+	groups := make(map[byte][]Point)
+
+	var y int
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			break
+		}
+
+		matrix = append(matrix, []byte(line))
+		for x := range line {
+			if line[x] != '.' {
+				groups[line[x]] = append(groups[line[x]], Point{x: x, y: y})
+			}
+		}
+
+		y++
+	}
+
+	return groups, matrix
+}
+
+func checkAntinode(antinode Point, matrix [][]byte) bool {
+	if antinode.x < 0 || antinode.y < 0 || antinode.y >= len(matrix) || antinode.x >= len(matrix[0]) {
+		return false
+	}
+
+	return true
+}
+
+func getAntinodes(groups map[byte][]Point, matrix [][]byte, multi bool) map[string]Point {
+	antinodes := make(map[string]Point)
+
+	for _, items := range groups {
+		edge := len(items)
+		for i := range items {
+			for j := i + 1; j < edge; j++ {
+				deltaX := items[j].x - items[i].x
+				deltaY := items[j].y - items[i].y
+
+				firstAntinode := Point{x: items[i].x - deltaX, y: items[i].y - deltaY}
+				for checkAntinode(firstAntinode, matrix) {
+					antinodes[firstAntinode.key()] = firstAntinode
+					if multi {
+						firstAntinode = Point{x: firstAntinode.x - deltaX, y: firstAntinode.y - deltaY}
+					} else {
+						firstAntinode = Point{x: -1, y: -1}
+					}
+				}
+
+				secondAntinode := Point{x: items[j].x + deltaX, y: items[j].y + deltaY}
+				for checkAntinode(secondAntinode, matrix) {
+					antinodes[secondAntinode.key()] = secondAntinode
+					if multi {
+						secondAntinode = Point{x: secondAntinode.x + deltaX, y: secondAntinode.y + deltaY}
+					} else {
+						secondAntinode = Point{x: -1, y: -1}
+					}
+				}
+			}
+		}
+	}
+
+	return antinodes
+}
+
+func part2(groups map[byte][]Point, matrix [][]byte) int {
+	antinodes := getAntinodes(groups, matrix, true)
+
+	for _, items := range groups {
+		if len(items) > 1 {
+			for _, item := range items {
+				antinodes[item.key()] = item
+			}
+		}
+	}
+
+	return len(antinodes)
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	groups, matrix := readInput(file)
+	fmt.Println("Part1:", len(getAntinodes(groups, matrix, false)))
+	fmt.Println("Part2:", part2(groups, matrix))
+}

+ 125 - 0
08/description.txt

@@ -0,0 +1,125 @@
+--- Day 8: Resonant Collinearity ---
+
+You find yourselves on the roof of a top-secret Easter Bunny installation.
+
+While The Historians do their thing, you take a look at the familiar huge antenna. Much to your surprise, it seems to have been reconfigured to emit a signal that makes people 0.1% more likely to buy Easter Bunny brand Imitation Mediocre Chocolate as a Christmas gift! Unthinkable!
+
+Scanning across the city, you find that there are actually many such antennas. Each antenna is tuned to a specific frequency indicated by a single lowercase letter, uppercase letter, or digit. You create a map (your puzzle input) of these antennas. For example:
+
+............
+........0...
+.....0......
+.......0....
+....0.......
+......A.....
+............
+............
+........A...
+.........A..
+............
+............
+
+The signal only applies its nefarious effect at specific antinodes based on the resonant frequencies of the antennas. In particular, an antinode occurs at any point that is perfectly in line with two antennas of the same frequency - but only when one of the antennas is twice as far away as the other. This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
+
+So, for these two antennas with frequency a, they create the two antinodes marked with #:
+
+..........
+...#......
+..........
+....a.....
+..........
+.....a....
+..........
+......#...
+..........
+..........
+
+Adding a third antenna with the same frequency creates several more antinodes. It would ideally add four antinodes, but two are off the right side of the map, so instead it adds only two:
+
+..........
+...#......
+#.........
+....a.....
+........a.
+.....a....
+..#.......
+......#...
+..........
+..........
+
+Antennas with different frequencies don't create antinodes; A and a count as different frequencies. However, antinodes can occur at locations that contain antennas. In this diagram, the lone antenna with frequency capital A creates no antinodes but has a lowercase-a-frequency antinode at its location:
+
+..........
+...#......
+#.........
+....a.....
+........a.
+.....a....
+..#.......
+......A...
+..........
+..........
+
+The first example has antennas with two different frequencies, so the antinodes they create look like this, plus an antinode overlapping the topmost A-frequency antenna:
+
+......#....#
+...#....0...
+....#0....#.
+..#....0....
+....0....#..
+.#....A.....
+...#........
+#......#....
+........A...
+.........A..
+..........#.
+..........#.
+
+Because the topmost A-frequency antenna overlaps with a 0-frequency antinode, there are 14 total unique locations that contain an antinode within the bounds of the map.
+
+Calculate the impact of the signal. How many unique locations within the bounds of the map contain an antinode?
+
+Your puzzle answer was 341.
+--- Part Two ---
+
+Watching over your shoulder as you work, one of The Historians asks if you took the effects of resonant harmonics into your calculations.
+
+Whoops!
+
+After updating your model, it turns out that an antinode occurs at any grid position exactly in line with at least two antennas of the same frequency, regardless of distance. This means that some of the new antinodes will occur at the position of each antenna (unless that antenna is the only one of its frequency).
+
+So, these three T-frequency antennas now create many antinodes:
+
+T....#....
+...T......
+.T....#...
+.........#
+..#.......
+..........
+...#......
+..........
+....#.....
+..........
+
+In fact, the three T-frequency antennas are all exactly in line with two antennas, so they are all also antinodes! This brings the total number of antinodes in the above example to 9.
+
+The original example now has 34 antinodes, including the antinodes that appear on every antenna:
+
+##....#....#
+.#.#....0...
+..#.#0....#.
+..##...0....
+....0....#..
+.#...#A....#
+...#..#.....
+#....#.#....
+..#.....A...
+....#....A..
+.#........#.
+...#......##
+
+Calculate the impact of the signal using this updated model. How many unique locations within the bounds of the map contain an antinode?
+
+Your puzzle answer was 1134.
+
+Both parts of this puzzle are complete! They provide two gold stars: **

+ 50 - 0
08/input

@@ -0,0 +1,50 @@
+.......................V.........e...O............
+..........q.pj8...............................u...
+...................8..............................
+.............8.....6.................J....l....u..
+........................6................J..Z..B..
+......e.........E...........................O.J...
+......Jq..........................5...............
+...............E...........e.Q..5.f...............
+..............................Q..A.....f..B.....O.
+....V...................j.....Af..................
+............8......n..............l...f....Z7.....
+...............n..........4........A........BD....
+...........j...................Q..z.......R....l..
+N.........6....q.....3....n.........D...........Z.
+.............a.6..3.F........D..I.................
+.............03.................Q.......h...2.....
+......................A.u.......................m.
+.V........F......L.............5..........z.R....Z
+.......N....q.................n.......L.E.........
+..................M.........y.....................
+......N............................m.L..y........R
+.o....................L...........I...7..R........
+......o..........9..............2.......D.........
+..od.............y...........................I....
+d........3.....M...........E.............I........
+......X.W....................p.2.....7...z....s...
+V......o........M.....9.................G......7..
+.................M.....................h..0....m..
+.......d.......F......p.........s.h........z......
+..r..........Y.i................9............s....
+.....W..a.Y..........y.............p..............
+.....g.......r........w...........................
+....r.....b...............g........x.s.....h......
+....a.....d.......................................
+.....................S.......w.............1......
+..Y...............................H...............
+...b...........Y........................e..t...0.v
+..........i..........w.........9....T........v....
+.................U...........2....................
+.........S........t......T........................
+....................U..................Gt.........
+....U...S..........................P.....1.B......
+.r...X............w.......P.....x.j...............
+...W......x..b........g........F.....a............
+S.i.................................1.......H.....
+.......U......b......x.....X..........G.1.........
+...i....X....................P..4........H........
+.................................H................
+......W...................T4...g................v.
+..........................v........GP..4.....t....