From 68d3e12001f655296ffc6b2f9a7de6268ceae1d8 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Wed, 4 Dec 2024 20:53:21 +0100 Subject: [PATCH] Solved 2024/04 P1 + P2 --- 2024/03/notes.txt | 17 +++++ 2024/03/solution.py | 7 +-- 2024/03/test.py | 16 ----- 2024/04/4.md | 104 +++++++++++++++++++++++++++++++ 2024/04/solution.py | 66 ++++++++++++++++++++ README.md | 3 + __pycache__/fred.cpython-311.pyc | Bin 6885 -> 8434 bytes fred.py | 46 +++++++++++--- 8 files changed, 229 insertions(+), 30 deletions(-) create mode 100644 2024/03/notes.txt delete mode 100644 2024/03/test.py create mode 100644 2024/04/4.md create mode 100644 2024/04/solution.py diff --git a/2024/03/notes.txt b/2024/03/notes.txt new file mode 100644 index 0000000..4dafc8c --- /dev/null +++ b/2024/03/notes.txt @@ -0,0 +1,17 @@ + # # Second attempt + + # instructions = "" + + # with open(input_f) as file: + # for line in file: + # instructions += line + # print(instructions) + + +# #(do\(\).*?mul\((\d+),{1}(\d+)\)) + + + +# (?<=do\(\))([^d]*(mul\(\d+,\d+\))[^d]*)+(?=don't\(\)) + +# (?<=do\(\))[^m]*(mul\((\d+),{1}(\d+)\)*)*[^d]*(?=don't\(\)) diff --git a/2024/03/solution.py b/2024/03/solution.py index fa5f153..c6e25a8 100644 --- a/2024/03/solution.py +++ b/2024/03/solution.py @@ -43,9 +43,6 @@ if part == 2: if do: m = get_re(r"mul\((\d+),{1}(\d+)\)",match) result += (int(m.group(1))*int(m.group(2))) -print(result) - + print(result) # Not really happy with my code. I would prefer to find all mul(x,y) between do() and don't(), -# instead of finding all the mul/do/don't and then turning calculation on/off. - -#(do\(\).*?mul\((\d+),{1}(\d+)\)) \ No newline at end of file +# instead of finding all the mul/do/don't and then turning calculation on/off. \ No newline at end of file diff --git a/2024/03/test.py b/2024/03/test.py deleted file mode 100644 index 55eae9e..0000000 --- a/2024/03/test.py +++ /dev/null @@ -1,16 +0,0 @@ -from re import findall - -total1 = total2 = 0 -enabled = True -data = open('input').read() - -for a, b, do, dont in findall(r"mul\((\d+),(\d+)\)|(do\(\))|(don't\(\))", data): - if do or dont: - enabled = bool(do) - else: - x = int(a) * int(b) - total1 += x - total2 += x * enabled - -print(total1, total2) - diff --git a/2024/04/4.md b/2024/04/4.md new file mode 100644 index 0000000..fd871f7 --- /dev/null +++ b/2024/04/4.md @@ -0,0 +1,104 @@ +## \-\-- Day 4: Ceres Search \-\-- + +\"Looks like the Chief\'s not here. Next!\" One of The Historians pulls +out a device and pushes the only button on it. After a brief flash, you +recognize the interior of the [Ceres monitoring station](/2019/day/10)! + +As the search for the Chief continues, a small Elf who lives on the +station tugs on your shirt; she\'d like to know if you could help her +with her *word search* (your puzzle input). She only has to find one +word: `XMAS`. + +This word search allows words to be horizontal, vertical, diagonal, +written backwards, or even overlapping other words. It\'s a little +unusual, though, as you don\'t merely need to find one instance of +`XMAS` - you need to find *all of them*. Here are a few ways `XMAS` +might appear, where irrelevant characters have been replaced with `.`: + + ..X... + .SAMX. + .A..A. + XMAS.S + .X.... + +The actual word search will be full of letters instead. For example: + + MMMSXXMASM + MSAMXMSMSA + AMXSXMAAMM + MSAMASMSMX + XMASAMXAMM + XXAMMXXAMA + SMSMSASXSS + SAXAMASAAA + MAMMMXMMMM + MXMXAXMASX + +In this word search, `XMAS` occurs a total of `18` times; here\'s the +same word search again, but where letters not involved in any `XMAS` +have been replaced with `.`: + + ....XXMAS. + .SAMXMS... + ...S..A... + ..A.A.MS.X + XMASAMX.MM + X.....XA.A + S.S.S.S.SS + .A.A.A.A.A + ..M.M.M.MM + .X.X.XMASX + +Take a look at the little Elf\'s word search. *How many times does +`XMAS` appear?* + +Your puzzle answer was `2378`. + +## \-\-- Part Two \-\-- {#part2} + +The Elf looks quizzically at you. Did you misunderstand the assignment? + +Looking for the instructions, you flip over the word search to find that +this isn\'t actually an `XMAS` puzzle; it\'s an +`X-MAS` +puzzle in which you\'re supposed to find two `MAS` in the shape of an +`X`. One way to achieve that is like this: + + M.S + .A. + M.S + +Irrelevant characters have again been replaced with `.` in the above +diagram. Within the `X`, each `MAS` can be written forwards or +backwards. + +Here\'s the same example from before, but this time all of the `X-MAS`es +have been kept instead: + + .M.S...... + ..A..MSMS. + .M.S.MAA.. + ..A.ASMSM. + .M.S.M.... + .......... + S.S.S.S.S. + .A.A.A.A.. + M.M.M.M.M. + .......... + +In this example, an `X-MAS` appears `9` times. + +Flip the word search from the instructions back over to the word search +side and try again. *How many times does an `X-MAS` appear?* + +Your puzzle answer was `1796`. + +Both parts of this puzzle are complete! They provide two gold stars: +\*\* + +At this point, you should [return to your Advent calendar](/2024) and +try another puzzle. + +If you still want to see it, you can [get your puzzle +input](4/input). + diff --git a/2024/04/solution.py b/2024/04/solution.py new file mode 100644 index 0000000..dee3383 --- /dev/null +++ b/2024/04/solution.py @@ -0,0 +1,66 @@ +#!/bin/python3 +import sys,re +from itertools import cycle +from pprint import pprint +sys.path.insert(0, '../../') +from fred import list2int,get_re,nprint,lprint, toGrid,grid_valid,addTuples,get_value_in_direction + +input_f = 'input' +part = 2 + +grid = toGrid(input_f) + +directions = { + 'up': (-1, 0), + 'down': (1, 0), + 'left': (0, -1), + 'right': (0, 1), + 'up-left': (-1, -1), + 'up-right': (-1, 1), + 'down-left': (1, -1), + 'down-right': (1, 1) +} + +count = 0 + +######################################### +# # +# Part 1 # +# # +######################################### + +if part == 1: + directions = { + 'up': (-1, 0), + 'down': (1, 0), + 'left': (0, -1), + 'right': (0, 1), + 'up-left': (-1, -1), + 'up-right': (-1, 1), + 'down-left': (1, -1), + 'down-right': (1, 1) + } + for r, rows in enumerate(grid): + for c, _ in enumerate(rows): + if get_value_in_direction(grid,(r,c)) == 'X': + for d in directions: + if get_value_in_direction(grid,(r,c),d,4,'str') == 'XMAS': + count += 1 + print(count) + + +######################################### +# # +# Part 2 # +# # +######################################### +if part == 2: + # Kinda ugly code, wish it would be nicer. + for r, rows in enumerate(grid): + for c, _ in enumerate(rows): + tmp = '' + if get_value_in_direction(grid,(r,c)) == 'A': + if (get_value_in_direction(grid,(r,c),'up-left') == 'M' and get_value_in_direction(grid,(r,c),'down-right') == 'S') or (get_value_in_direction(grid,(r,c),'up-left') == 'S' and get_value_in_direction(grid,(r,c),'down-right') == 'M'): + if (get_value_in_direction(grid,(r,c),'up-right') == 'M' and get_value_in_direction(grid,(r,c),'down-left') == 'S') or (get_value_in_direction(grid,(r,c),'up-right') == 'S' and get_value_in_direction(grid,(r,c),'down-left') == 'M'): + count += 1 + print(count) \ No newline at end of file diff --git a/README.md b/README.md index a3df945..b56766e 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,9 @@ .--'~ ~ ~| .-' * \ / '-. 1 ** .--'~ ,* ~ | | >o< \_\_\|_/__/ | 2 ** .---': ~ '(~), ~| | >@>O< o-_/.()__------| 3 ** + |@..@'. ~ " ' ~ | |>O>o<@< \____ .'| 4 ** + + ## 2023 . * ~~~~ /\ ' . 14 diff --git a/__pycache__/fred.cpython-311.pyc b/__pycache__/fred.cpython-311.pyc index 97648b558b802da2c9cef828584b45c3b0916a7f..067a3b1b15ecb9fe5032da7ee9e691f687b86528 100644 GIT binary patch delta 2738 zcmaJ@ZEO_B8Q$63z3;s*-}!9Qz#3x1QODq7i|`fI!G;hgZh*8>tx$S+*5>Nt?wH*( zxa{TJ1Vv|R(IbPBqjDnW4~0fiLuu4Vt*WZO`lr%?{KGmW!haH}sw!EH`lqU@zVGb$ ztC2dp`^@__Gw;kh@9car@Q=aJFEq_BLD3$(GI=q2E7VrL3EaC9l_<$dV@^%R2yLbE zbuuPXm*Jv{;hIpWdR-Y)sM}B{WaL+9v!Zpfdp#Mq$?n^0|MS);302Af9&caYN zd}&HwEf%td$y%iWNkXC71u!cu2enFieyAF)_T7%povNm5%hj!&o=)Vh$;};mYJJwwwRb*QxFUaf;V{&q2Ge{~ZEj z-VN+b?ndjKsPnKO()$tm_>;gh>QNwjcr>^KJ%1T2cJ`tf5i4Ti`Z#OBJlF!^S#E2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