From f3f88f47d1d6a2fffee6419796648cfccad923af Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sun, 8 Dec 2024 09:12:53 +0100 Subject: [PATCH] Started work on 2024/08 P1 --- 2015/10/10.md | 45 +++++++++++++ 2015/10/solution.py | 40 +++++++++++ 2024/08/8.md | 111 +++++++++++++++++++++++++++++++ 2024/08/solution.py | 68 +++++++++++++++++++ 2024/08/test2 | 12 ++++ README.md | 4 +- __pycache__/fred.cpython-311.pyc | Bin 23367 -> 23989 bytes fred.py | 18 +++++ 8 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 2015/10/10.md create mode 100644 2015/10/solution.py create mode 100644 2024/08/8.md create mode 100644 2024/08/solution.py create mode 100644 2024/08/test2 diff --git a/2015/10/10.md b/2015/10/10.md new file mode 100644 index 0000000..6c6a0d2 --- /dev/null +++ b/2015/10/10.md @@ -0,0 +1,45 @@ +## \-\-- Day 10: Elves Look, Elves Say \-\-- + +Today, the Elves are playing a game called +[look-and-say](https://en.wikipedia.org/wiki/Look-and-say_sequence). +They take turns making sequences by reading aloud the previous sequence +and using that reading as the next sequence. For example, `211` is read +as \"one two, two ones\", which becomes `1221` (`1` `2`, `2` `1`s). + +Look-and-say sequences are generated iteratively, using the previous +value as input for the next step. For each step, take the previous +value, and replace each run of digits (like `111`) with the number of +digits (`3`) followed by the digit itself (`1`). + +For example: + +- `1` becomes `11` (`1` copy of digit `1`). +- `11` becomes `21` (`2` copies of digit `1`). +- `21` becomes `1211` (one `2` followed by one `1`). +- `1211` becomes `111221` (one `1`, one `2`, and two `1`s). +- `111221` becomes `312211` (three `1`s, two `2`s, and one `1`). + +Starting with the digits in your puzzle input, apply this process 40 +times. What is *the length of the result*? + +Your puzzle answer was `360154`. + +## \-\-- Part Two \-\-- {#part2} + +Neat, right? You might also enjoy hearing [John Conway talking about +this sequence](https://www.youtube.com/watch?v=ea7lJkEhytA) (that\'s +Conway of *Conway\'s Game of Life* fame). + +Now, starting again with the digits in your puzzle input, apply this +process *50* times. What is *the length of the new result*? + +Your puzzle answer was `5103798`. + +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2015) and +try another puzzle. + +Your puzzle input was `1113122113`{.puzzle-input}. + diff --git a/2015/10/solution.py b/2015/10/solution.py new file mode 100644 index 0000000..2e89a5b --- /dev/null +++ b/2015/10/solution.py @@ -0,0 +1,40 @@ +#!/bin/python3 +import sys,time,re +from pprint import pprint +from itertools import groupby +sys.path.insert(0, '../../') +from fred import list2int,get_re,nprint,lprint,loadFile +start_time = time.time() + +input_f = 'input' + +part = 1 +######################################### +# # +# Part 1 # +# # +######################################### + +if part == 1: + instructions = loadFile(input_f)[0] + print(instructions) + + for i in range(0,50): + numbers = [(len(list(group)),key) for key, group in groupby(instructions)] + result = '' + for n in numbers: + result += str(n[0])+str(n[1]) + #print(instructions,'->',result) + instructions = result + #input() + print(len(instructions)) + +######################################### +# # +# Part 2 # +# # +######################################### +if part == 2: + exit() + +print("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file diff --git a/2024/08/8.md b/2024/08/8.md new file mode 100644 index 0000000..3e473eb --- /dev/null +++ b/2024/08/8.md @@ -0,0 +1,111 @@ +## \-\-- Day 8: Resonant Collinearity \-\-- + +You find yourselves on the [roof](/2016/day/25) 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]{title="They could have imitated delicious chocolate, but the mediocre chocolate is WAY easier to imitate."} +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?* + +To begin, [get your puzzle input](8/input). + +Answer: + diff --git a/2024/08/solution.py b/2024/08/solution.py new file mode 100644 index 0000000..98f93b6 --- /dev/null +++ b/2024/08/solution.py @@ -0,0 +1,68 @@ +#!/bin/python3 +import sys,time,re +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int,get_re,nprint,lprint,grid_valid,loadFile,subTuples,addTuples,toGrid,get_value_in_direction +start_time = time.time() + +input_f = 'test' + +part = 1 +######################################### +# # +# Part 1 # +# # +######################################### + +def findAntinodes(grid:list,pos:set,current_node:set): + print('Found',current_node,'at',pos) + antinodes = [] + for row in range(pos[0],len(grid)): + for col in range(0,len(grid[1])): + print(row,col) + + if get_value_in_direction(grid,(row,col)) == current_node and (row,col) != pos: + print('Matched with',(row,col)) + + s = subTuples((row,col-1),pos) + a = addTuples(s,(row,col+1)) + if grid_valid(s[0],s[1],grid): + print('Antinode at',s) + antinodes.append(s) + if grid_valid(a[0],a[1],grid): + print('Antinode at',a) + antinodes.append(a) + + return antinodes + +if part == 1: + grid = toGrid(input_f) + + nprint(grid) + + size = (len(grid),len(grid[0])) + antinodes = [] + for row in range(0,size[0]): + for col in range(0,size[1]): + current_node = get_value_in_direction(grid,(row,col)) + if current_node != '.': + antinodes += findAntinodes(grid,(row,col),current_node) + print(antinodes) + for idx, i in enumerate(grid): + for jdx, j in enumerate(i): + if (idx, jdx) in antinodes: + print('#', end=' ') # Print sign + else: + print(grid[idx][jdx], end=' ') # Regular grid element + print() + + +######################################### +# # +# Part 2 # +# # +######################################### +if part == 2: + exit() + +print("--- %s seconds ---" % (time.time() - start_time)) \ No newline at end of file diff --git a/2024/08/test2 b/2024/08/test2 new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/2024/08/test2 @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/README.md b/README.md index bfff89b..bbd3280 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ |_.~._@'.. ~ ~ *| | _| |_ ..\_\_ ..'* | 5 ** | ||| @@ '''...| |... .' '.'''../..| 6 ** |#~~~@@@@ @ | |/\ ''. | | -/ :| 7 ** - + ## 2023 . * ~~~~ /\ ' . 14 @@ -103,7 +103,7 @@ //| ## 2015 - + >>O>@>>>o>>>*>>*>>>@<>@<<<@>*<*<<>o<*<<<*<>O>>>*>*<<< 8 ** >@>o>O<<>>*>>*<*>>*>@<< 6 ** diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index 1c60b63e2e60216d9c95e57b36ba169111afa788..97aa3fc92cf3f7b1b20c02a9564ba4413c65dfd8 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(gL5${25=@CN6Kk{r(i<0& z*A?cpme>BI+&P*bd)q^u3@6_fjSNJ^K-TnSv@$N)WF7ANLg}2L4;?}%sR(U<*KWov z?*z*iVqhx`(u< zkZuxJ2hOk_>u_4Dy>qUw4JMjq|0CCNacyvymo^Q1gZd8wRh|;YkdJK^IFS{^OGD2X zJ!F-&QRE92tDYnQ8F2-Y{?aACrOUau{3Y)Zdo$pZ)K7C{ZK)o59Txuk%xabqzTtb> zf_Pwfzb4SI!}?(O{Tl4?Nn<0sAWj(*GUIm~@G4sW)o8DyAdVVgc2V3nqO(OYHPY=$ zqiAcT@)VT86emY2+Iy&+wtBaIUc=6bUv?Dy=TLx`5e%8&n9M2z=f%SK=)>#D_#8n6 z!B(d@w`;&G6c3v43dz(IAd@*xtczd}!B(+Bs{82%iULlHP_~_&6&tgw*t=pT+s5{Z zBkhOQbi3PWc=;pt&pEzOtUAg%M3_$JqLWF1kuB!I30`AML2 zFKcQ$_Y{kHd9Hy@DbT?JnF}2jXeXc^m+cGGR6x}RDh^OU@Fsygt8`WTTAMBu`GLH- c0Y1ecb7)xnpP6`L=LS|I?41kN{6w1l2^0QK3jhEB diff --git a/fred.py b/fred.py index 1a99389..ff9ab81 100644 --- a/fred.py +++ b/fred.py @@ -122,6 +122,24 @@ def addTuples(x: tuple, y: tuple): raise TypeError("Both inputs must be tuples.") return (x[0] + y[0], x[1] + y[1]) +def subTuples(x: tuple, y: tuple): + """ + Substracts two tuples element-wise. + + Args: + x (tuple): The first tuple. + y (tuple): The second tuple. + + Returns: + tuple: A tuple with the sum of corresponding elements of x and y. + + Raises: + TypeError: If x or y are not tuples. + """ + if not isinstance(x, tuple) or not isinstance(y, tuple): + raise TypeError("Both inputs must be tuples.") + return (x[0] - y[0], x[1] - y[1]) + def findDupes(input: list): """ Finds duplicate elements in a list and returns their values.