Compare commits

...
2 Commits
Author SHA1 Message Date
Maya Herrscher 369e4c1e63 Kind of inefficient solution for day 9 (pt 2) 2024-12-09 09:13:32 +01:00
Maya Herrscher 90756ec10c Day 8 2024-12-09 07:12:16 +01:00
10 changed files with 379 additions and 0 deletions

No files matched your search

Binary file not shown.
+125
View File
@@ -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 269.
--- 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 949.
Both parts of this puzzle are complete! They provide two gold stars: **
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import sys
from itertools import combinations
from copy import deepcopy
if __name__ == '__main__':
mp = [list(line.strip('\n')) for line in open(sys.argv[1])]
antinodes = deepcopy(mp)
ah = deepcopy(mp)
antennas = {}
for j, y in enumerate(mp):
for i, x in enumerate(y):
if x != '.':
if x in antennas: antennas[x].append((i,j))
else: antennas[x] = [(i,j)]
ah[j][i] = '#'
for freq, ants in antennas.items():
for a1, a2 in combinations(ants, 2):
dx, dy = a2[0] - a1[0], a2[1] - a1[1]
x1, y1 = a1[0] - dx, a1[1] - dy
x2, y2 = a2[0] + dx, a2[1] + dy
if (0 <= x1 < len(mp[0])) and (0 <= y1 < len(mp)):
antinodes[y1][x1] = '#'
if (0 <= x2 < len(mp[0])) and (0 <= y2 < len(mp)):
antinodes[y2][x2] = '#'
while (0 <= x1 < len(mp[0])) and (0 <= y1 < len(mp)):
ah[y1][x1] = '#'
x1, y1 = x1-dx, y1-dy
while (0 <= x2 < len(mp[0])) and (0 <= y2 < len(mp)):
ah[y2][x2] = '#'
x2, y2 = x2+dx, y2+dy
# challenge 1
res1 = sum([a.count('#') for a in antinodes])
print(f"challenge 1:\n{res1}\n")
# challenge 2
res2 = sum([a.count('#') for a in ah])
print(f"challenge 2:\n{res2}")
+12
View File
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
+50
View File
@@ -0,0 +1,50 @@
...............................6.B..........P.....
n..............M..................................
....n.....sM7.............................6.....p.
......................Mr......................P...
.......n.......................................p.E
.......................6...................p......
r..............................C........B....P....
.....................d........6......B4....P......
.........................7....................4...
.n......................R..............4..........
.....N........S.................K.C..........4....
...........N..r.....................B....K........
..................................................
......N.......x.............7.......K.....2....E..
...................r..H........R..................
.....................s....p........C...........2..
....3.......................M.....................
........k....................H....5...............
.....x....N................d.5..y................J
m.....................d7...................J......
.......exk........................................
.......x.............5.......R....................
..........eY......................................
...S.3..............................O.E...J.......
.......8...H....k...............J.................
......S.e.........C.H.....................X.....y.
................j..........y.........2............
...........e.........k............................
......YS...3..............5..........K...XR.......
...m..............j.s..........c..................
.........................j........................
...............j..................................
.....m................................2...........
.........Y......................................b.
..................................................
.......................h...........b..............
............m......D..............d...............
........o......D..................................
...................................O..............
..................................................
......8...........................................
........D.Y..o...................1................
.....................................b..9.........
........................h..0......................
.....o......................h..0........b1........
.........8.............X..........................
..........o..........c..........1...........O.....
....8....................y0...c...................
..............D.......c..................9..0.....
............................1..........O..9.......
Binary file not shown.
+77
View File
@@ -0,0 +1,77 @@
--- Day 9: Disk Fragmenter ---
Another push of the button leaves you in the familiar hallways of some friendly amphipods! Good thing you each somehow got your own personal mini submarine. The Historians jet away in search of the Chief, mostly by driving directly into walls.
While The Historians quickly figure out how to pilot these things, you notice an amphipod in the corner struggling with his computer. He's trying to make more contiguous free space by compacting all of the files, but his program isn't working; you offer to help.
He shows you the disk map (your puzzle input) he's already generated. For example:
2333133121414131402
The disk map uses a dense format to represent the layout of files and free space on the disk. The digits alternate between indicating the length of a file and the length of free space.
So, a disk map like 12345 would represent a one-block file, two blocks of free space, a three-block file, four blocks of free space, and then a five-block file. A disk map like 90909 would represent three nine-block files in a row (with no free space between them).
Each file on disk also has an ID number based on the order of the files as they appear before they are rearranged, starting with ID 0. So, the disk map 12345 has three files: a one-block file with ID 0, a three-block file with ID 1, and a five-block file with ID 2. Using one character for each block where digits are the file ID and . is free space, the disk map 12345 represents these individual blocks:
0..111....22222
The first example above, 2333133121414131402, represents these individual blocks:
00...111...2...333.44.5555.6666.777.888899
The amphipod would like to move file blocks one at a time from the end of the disk to the leftmost free space block (until there are no gaps remaining between file blocks). For the disk map 12345, the process looks like this:
0..111....22222
02.111....2222.
022111....222..
0221112...22...
02211122..2....
022111222......
The first example requires a few more steps:
00...111...2...333.44.5555.6666.777.888899
009..111...2...333.44.5555.6666.777.88889.
0099.111...2...333.44.5555.6666.777.8888..
00998111...2...333.44.5555.6666.777.888...
009981118..2...333.44.5555.6666.777.88....
0099811188.2...333.44.5555.6666.777.8.....
009981118882...333.44.5555.6666.777.......
0099811188827..333.44.5555.6666.77........
00998111888277.333.44.5555.6666.7.........
009981118882777333.44.5555.6666...........
009981118882777333644.5555.666............
00998111888277733364465555.66.............
0099811188827773336446555566..............
The final step of this file-compacting process is to update the filesystem checksum. To calculate the checksum, add up the result of multiplying each of these blocks' position with the file ID number it contains. The leftmost block is in position 0. If a block contains free space, skip it instead.
Continuing the first example, the first few blocks' position multiplied by its file ID number are 0 * 0 = 0, 1 * 0 = 0, 2 * 9 = 18, 3 * 9 = 27, 4 * 8 = 32, and so on. In this example, the checksum is the sum of these, 1928.
Compact the amphipod's hard drive using the process he requested. What is the resulting filesystem checksum? (Be careful copy/pasting the input for this puzzle; it is a single, very long line.)
Your puzzle answer was 6288599492129.
--- Part Two ---
Upon completion, two things immediately become clear. First, the disk definitely has a lot more contiguous free space, just like the amphipod hoped. Second, the computer is running much more slowly! Maybe introducing all of that file system fragmentation was a bad idea?
The eager amphipod already has a new plan: rather than move individual blocks, he'd like to try compacting the files on his disk by moving whole files instead.
This time, attempt to move whole files to the leftmost span of free space blocks that could fit the file. Attempt to move each file exactly once in order of decreasing file ID number starting with the file with the highest file ID number. If there is no span of free space to the left of a file that is large enough to fit the file, the file does not move.
The first example from above now proceeds differently:
00...111...2...333.44.5555.6666.777.888899
0099.111...2...333.44.5555.6666.777.8888..
0099.1117772...333.44.5555.6666.....8888..
0099.111777244.333....5555.6666.....8888..
00992111777.44.333....5555.6666.....8888..
The process of updating the filesystem checksum is the same; now, this example's checksum would be 2858.
Start over, now compacting the amphipod's hard drive using this new method instead. What is the resulting filesystem checksum?
Your puzzle answer was 6321896265143.
Both parts of this puzzle are complete! They provide two gold stars: **
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env python3
import sys
from copy import deepcopy
def compress(mem):
blocks = deepcopy(mem)
r = len(blocks) - 1
for p, b in enumerate(blocks):
if b == None:
while blocks[r] == None: r -= 1
# if you check this before moving it obviously does not work
if p >= r: break
blocks[p] = blocks[r]
blocks[r] = None
return blocks
def block_compress(free, full, size):
memory = [None] * size
for id, pos in reversed(full.items()):
p, b = pos
for i in sorted(free.keys()):
if i >= p: break
if free[i] < b: continue
else: p = i
if p != pos[0]:
free[p+b] = free[p] - b
free.pop(p)
if (pos[0]+b) in free:
free[pos[0]] = b + free[pos[0]+b]
free.pop(pos[0]+b)
else: free[pos[0]] = b
for j in range (p, p+b): memory[j] = id
return memory
def checksum(blocks):
cs = 0
for p, b in enumerate(blocks):
if b == None: continue
cs += p * b
return cs
if __name__ == '__main__':
blocks = [int(n) for n in list(open(sys.argv[1]).read().strip('\n'))]
memory = [None] * sum(blocks)
p, id = 0, 0
free, full = {}, {}
for i, b in enumerate(blocks):
if i % 2 == 0 and b > 0:
for j in range (p, p+b): memory[j] = id
full[id] = (p, b)
id += 1
else: free[p] = b
p += b
# challenge 1
res1 = checksum(compress(memory))
print(f"challenge 1:\n{res1}\n")
# challenge 2
res2 = checksum(block_compress(free, full, sum(blocks)))
print(f"challenge 2:\n{res2}")
+1
View File
@@ -0,0 +1 @@
2333133121414131402
+1
View File
@@ -0,0 +1 @@
6337613674436833695582382518294574934721895775456926659050519789764499137091974516817593123529825470413735998773313821249746165140476827548333979879759812148140743921984858367387865689863575979767978384602135617653231357745451442716162787988827217792125086367824652239497919721686553336289590824034825884491882728622562067753798821229222238807634124749916752577072651378955553992845307918307276391238273773149858396433832536796653365434709657851887396187578778147166906832666361736921666530215593938020386798211757213535118020656880772626853567534823537521847840542462424239748076442664335159933397986757683414553370933017533841909225785682535581657913534751914590354573479543352482677664279490115563578845916997385280799060446627827972734570759842272566609483169585119611369745574079877574611920292074449221344989775049422092807055209772581092501625426515216037533372713253769756161167528724961425674338433856921993913658367042577497436930301681312780243028924226184437743980832960764956152123711024813868188023445957797073319557834842443457408060431584672765711965523592339064155945688420399677786021375036865224718236192794168244218132331879653238514185879711274422704721314129454939677194798258512282384257688549556871296166858544234847196990249480549228468620477732643536689838393877978653367792853453997762592631241338636138577555428914232648823466514071894652137678142034849239193626153640623580414314608933385485612012888459733812386760439427452815938894615323396842132211755866147610694370823973397748963732281746466628818129365432979699941761269528742324281144556379381879602911724569887421245076318843809377436853841020844590787517655729552955292366331147517060174571596182569363324916905792827650803161491453136021278077732064374448109498165846527088615498892332771430958179814356621939482129587840825717107138658080991769344985444860919820564023672497821693314755599976209047988027535412418173944146379780263754526214261017834795745288115763115420595437959121698079541886909440959071743761324933549055785895219883434371956470166162332913626974228698704867909040649016617460867032767038334727116689244913263370265020373034542849904912599647836283459162238825918961705866907222166252213569835943736588468231414457382040649035476988352594163315317533816572151422835818584798813953851735242330898787799654338389819473342688805893283467278648262129884651697935311570182570622882847035353690402079255041929980106820933987229229797544269317587273137530758375497634699083283818553718955173431710764611483099989260285267944525422740201440336499923419596970234642786695265316332262177054801980634357789526362333385795223688334671859856597460299593595342948524369087665258874015146296241079817536554214141611547952197222448591679959466790128097793753463968675487268040477815563380229164213672752215208397193598924987982747832046393487916058272183247276523097304784635757381389468220157974675179325452839698486360399879695439645913991513175369124373993030541020107278592224606843141533658010212677209043325490342134728924457069965191109858378318612965799852635497747794734595236056575848591924766220818282475324396727508923606980769458725299525452612238764560262657775582924569776483862349887068324449156297506644312010698479354773245797667563279586421294657685887842714452735721406358806171998745643329816772286796704744766044588635885245655459774311291948625491629489845688426449351717832871305285577753164547112747414863694575184763986239102380914887326641428026877699225992392096482370418830381415159027914953351339474421536726145911393866697198669419803537365630324763404171851150754014952414412764421984874654951033761733339938562539501446678133468715754993394758543046223523398728955356737781406931429746223633763241354625939226463718409470443784758784373146313839928768614814211356253038934558927790904988453344509018747586929116183883508082777047599959667039225085656443324675746726742070671570757088224324721174549372663665357073604929544480355015119614695352475587896476298629578751175152953418694340237315682094281425544939961496671928705760203087589687943595183450878840612944545990131498239234836114902680973922971833131647941447156647709468389679862137111593607085798425435736388659127388489737118418537272479942534595188821602597196231331570739573219060339186363023917360728735156330123729551057656522303564363389388580584548939620728350317592847949877699604391352991731027339689607733987355581433793686246693841298838454573798145112168184911056903380103939467378844737779330194582116721272190126779182733559651836358413812266428668253391218604697749045971951622863863588909757411613156843452329293637464367447412392465939681589644859070551682234527516285233194458768183747577595703213148321859615511124931012101096951580915748936348216536567577647345618573868040994546908948496431144262585963718473204983249712109511962363539684909657287652765145553168177329822059904713775018257475153084721036958345931122317816828966716917929036331813946978532772968156626894887060457595911112698Line truncated