Commit graph

126 commits

Author SHA1 Message Date
178d96494a Solve 2023:17 p1-2 "Clumsy Crucible"
Revenge, blast from the past etc!

Had to learn Dijkstra's for this one. Was as stuck
as one can be on AOC. This stopped my progress
2023, and kept me busy at least 20-30h this year
(2024) as well. I never came past part 1, but got
part 2 in minutes when I hit home.

Turns out the initial queue was to blame, after
studying Dijkstras, reading hints on the subreddit
and tutorials in blogs. I was off-by-1 in costs,
since I misplaced a read from the grid.

I also struggled a really, really long time with
a bug where I resetted the steps to aggresively.

What helped me to figure it out was to create
simpler test case grids and step-debug them.

Example 1:

241
321

should give a least heat loss of 6 in pt1.

Example 2:

11199
12199
99199
99131
99111

should give a least heat loss of 9 in pt2.
2025-01-05 00:10:26 +01:00
fb468c2199 Solve 2023:16 "The Floor Will Be Lava" 2025-01-05 00:10:26 +01:00
a90269f7f9 Solve 2023:15 "Lens Library"
WALLOFTEXT for part 2, took me 90 minutes to find
this important text:

> Each step begins with a sequence of letters that
> indicate the label of the lens on which the step
> operates. The result of running the HASH algorithm
> on the label indicates the correct box for that
> step.

It also clarifies how part 2 and part 1 relates.
2025-01-05 00:10:26 +01:00
c832e30dcf Solve 2023:14 "Parabolic Reflector Dish" 2025-01-05 00:10:26 +01:00
3de54ce0e9 Solve 2023:13 "Point of Incidence" 2025-01-05 00:10:26 +01:00
4fa1b1e14c Solve 2023:12 "Hot Springs" 2025-01-05 00:10:26 +01:00
9ca8607f8b Solve 2023:11 "Cosmic Expansion" 2025-01-05 00:10:26 +01:00
a1bf11a5ed Fix flake8 errors for 2023:1-10 2025-01-05 00:10:26 +01:00
b681e5cdb7 Solve 2023:10 "Pipe Maze"
Got completely stuck on part 2. Tried some polygon
area calculations, but none provided the correct
answer, most likely due to the unorthodox polygon
points.

I also tried _shoelace method_ without any luck.
Had not heard about that one earlier, it was a good
learning experience even though I vould not use it
here.

By the subreddit, several people had had luck
using the ray method.

> Part 2 using one of my favorite facts from
> graphics engineering: lets say you have an
> enclosed shape, and you want to color every
> pixel inside of it. How do you know if a given
> pixel is inside the shape or not? Well, it
> turns out: if you shoot a ray in any direction
> from the pixel and it crosses the boundary an
> _odd number_ of times, it's _inside_. if it crosses
> an even number of times, it's outside. Works
> for all enclosed shapes, even self-intersecting
> and non-convex ones.
2025-01-05 00:10:26 +01:00
741f7b89d8 Refactor 2023:05
Increasing speed from 66mins to 4mins. Caching the
location value in the code to keep things at highest
speed.

From the subreddit, the algorithm looks like this.

1. Start att location 0
2. Traverse the whole process backwards, by
   reversing process steps and flipping dest/src
   positions.
3. Output is not a location, instead it's a seed.
4. if seed is in any seed range, use seed to get
   location as in step 1.
5. If not, increase location by 1 and repeat 2-4.
2025-01-05 00:10:26 +01:00
08288964bd Remove parse_input helper 2025-01-05 00:10:26 +01:00
96c4c1383a Solve 2023:09 "Mirage Maintenance" 2025-01-05 00:10:26 +01:00
540fa253da Solve 2023:08 "Haunted Wasteland"
Part 2 would have taken 10-15 hours with brute force.

After I figured out the puzzle input had circular
A-Z paths, it was plain as day that LCM was the
solution to the problem.

https://en.wikipedia.org/wiki/Least_common_multiple
2025-01-05 00:10:26 +01:00
fa3416cf40 Solve 2023:07 "Camel Cards" 2025-01-05 00:10:26 +01:00
a1a69d7114 Solve 2023:06 "Wait for it" 2025-01-05 00:10:26 +01:00
Anders Englöf Ytterström
ad05c8fab4 Solve 2023:05 "If You Give A Seed A Fertilizer"
Part 2 takes 66 minutes to run. There is some smart
things to realize here.
2025-01-05 00:10:26 +01:00
Anders Englöf Ytterström
cbd4bf50e1 Solve 2023:04 "Scratchcards"
On a train that according to swedish tradition
was late. Not a good environment to focus.

Got stuck 2 times:

- Initial code asumed the | was always after the 5th
  number, because of the example. Puzzle input had
  it at pos 10. Classic AoC mistake.
- I had a hard time trying to understand the score
  count, I insisted there was meant to be a +1 at
  some point.

  > That means card 1 is worth 8 points (1 for
  > the first match, then doubled three times for
  > each of the three matches after the first)

I should instead have just looked at the numbers.
2025-01-05 00:10:26 +01:00
7290f28ef7 Solve 2023:03 "Gear Ratios"
Overslept, took about 55 mins.
2025-01-05 00:10:26 +01:00
6001a6bb59 Solve 2023:02 "Cube Conundrum"
Very intermediate pythonic solution,
regex would have made the code more compact.

But since 2023:01 decreased the regex courage,
This code will do.
2025-01-05 00:10:26 +01:00
aae14797ea Solve 2023:01 "Trebuchet?!"
Turns out re methods are non-overlapping. And in
true AoC manners, no provided test cases had
overlaps.

Luckily for me, some of the last lines in the input
contained the string "oneight", so I was able to
find it out quite fast.

Revisions:

1) Reverse strings to find last digit
2) Use isdigit() and skip regex.
3) Use regexp with positive look-ahead.
2025-01-05 00:10:26 +01:00
661f18dca4 Prep Advent of Code 2023 2025-01-05 00:10:26 +01:00
bad86889d6 Revert "2023 solutions (#6)"
This reverts commit b6e048e4a7.
2025-01-05 00:09:28 +01:00
fcbeb4bfdd Solve 2024:24 pt2 "Code Chronicle"
To solve this, it was easier to visualize the
graph and spot errors. This code is kept for
reference.

To learn the mechanics, These subreddit threads
are helpful:

- https://www.reddit.com/r/adventofcode/comments/1hla5ql/2024_day_24_part_2_a_guide_on_the_idea_behind_the/
- https://www.reddit.com/r/adventofcode/comments/1hneuf0/2024_day_24_part_2_finally_solved_it/
2025-01-05 00:06:18 +01:00
d7d5312786 Solve 2024:21 pt2 "Keypad Conundrum"
Turns out the initial trial of using
Manhattan distance was required to
solve part 2, since the BFS
implementation is impossible to
scale up from 2 robots to 25.

Recursion and memoization makes the
execution time acceptable.

Line 53 makes all the difference here.
The order of `<|v|^|>` matters when
constructing a sequence. Many
hours was spent trying to find the correct
priority. For the example input, especially
456A and 379A were volatile.
2025-01-05 00:06:18 +01:00
3a9a7d4994 Solve 2024:21 p1 "Keypad Conundrum"
Early versions of the code used Manhattan distance
instead of BFS to find the movements. Turns out this
was covering about 80% of all cases.
2025-01-05 00:06:18 +01:00
72574e753f Solve 2024:24 pt1 "Crossed Wires"
No idea how to solve pt2, so it will rest for now.
2025-01-05 00:06:18 +01:00
1b7b30e296 Solve 2024:25 pt 1 "Code Chronicle"
Merry XMAS!
2025-01-05 00:06:18 +01:00
2c2379788d Solve 2024:23 p1-2 "LAN Party"
Part 1 uses the excellent itertools.combinations().

Part 2 introduces Bron-Kerbosch algorithm to the
util library!
2025-01-05 00:06:18 +01:00
096c3d3e5d Solve 2024:22 p1-2 "Monkey Market"
Part 1 was relatively easy, 2 was harder.

> So, by asking the monkey to sell the *first time*
> each buyer's prices go down 2, then up 1, then
> down 1, then up 3, you would get 23 (7 + 7 + 9)
> bananas!

Emphazis mine. Earlier versions of the code missed
this fact, and added all occourences of the sequence.
providing a answer that is too high.
2025-01-05 00:06:18 +01:00
0500ac41ab Solve 2024:20 p1-2 "Race Condition"
BFS to get the path from start to end.

pt 1 took some time to wrap around, and initial
versions of the code used defaultdict() to keep
counts of all cheats to check if the example
puzzle input gace the right counts for the cheats.

But since only 100 or more was requested, a counter
do the job in the final version of the code.

For pt 2, a permutation is used.
2025-01-05 00:06:18 +01:00
14520a4625 Solve 2024:17 p1-2 "Chronospatial Computer"
Slow burner.

First part was a straightforward implementation
of a runner, the code is extracted to `org_version()`.

Part 2 was way more tricky. Like many others, trying
to understand the program felt necessary. This
intepretation is this:

    """Python version of puzzle input program."""
    a = <unknown>
    b, c = 0, 0
    while a:
        b = (a % 8) ^ 1
        c = a // (2**b)
        b = (b ^ 5) ^ c
        a = a // (2**3)
        print(b % 8)

Some clarification here:

- the value is octagonal. 1-8 is the key.
- The value of reg a is _high_. It is not meant to
be brute forced.

The idea, which is not originally mine, is to go
from right to left. This code can be used to try
out some patterns:

    while True:
        python_version(input("Provide A:"))

Here, it was apparent a=4 gives the last digit of
my puzzle input. a=37 (4 * 8 + 3) gives the last
2 digits. a=222 (37 * 8 + 6) gives the last 3
digits, and so on.

Knowing the program could be reconstructed like
this, the first code halted at wrong values. Turns
out some steps give more than 1 possible scenario.

The code was therefore in need of a queue.
2025-01-05 00:06:18 +01:00
ded5c4f28c Solve 2024:19 p1-2 "Linen Layout"
Initial tries to use a while loop instead of
recursion gave a classic AoC situation: test cases
worked, but not actual AoC puzzle input.

Turns out I only considererd removing the longest
pattern form the design, rather than consider all
possible removals.

Some Python goodies in here:

- `"abcdefgh".startswith("abc")` instead of regexp.
- removeprefix() is nice, and in some cases more
readable. `"abcdefgh".removeprefix("abc")` vs
`"abcdefgh[3:]"
- To speed things up for pt 2, functools is used
which requires a dict to be hashed as a tuple.

If one wish to solve this without recursion, a
BFS solution is most likely the way to go.

def ispossible(design, patterns):
    Q = [design]
    possible = 0
    while Q:
        remaining = Q.pop(0)
        if not remaining:
            possible += 1
            continue
        for pattern in patterns[remaining[0]]:
            if remaining.startswith(pattern):
                Q.append(remaining.removeprefix(pattern))
    return possible
2025-01-05 00:06:18 +01:00
9dc3f16766 Solve 2024:18 p1-2 "RAM Run"
BFS baby.

FIRST_BLOCKED was found manually using code from pt 1
to perform a binary search.
2025-01-05 00:06:18 +01:00
f5c3ee938a Refactor 2024:15
Solution is still 100 lines, but now it is based
on a singular BFS oriented box push algorithm.
2025-01-05 00:06:18 +01:00
7688ddb763 Solve 2024:15 p2 "Warehouse Woes"
Glad this one is over. 150 lines of pure mayhem.
2025-01-05 00:06:18 +01:00
7c2b4c835a Solve 2024:16 pt2 "Reindeer Maze"
Part 1 was easily solved by Dijkstras (using heapq).
Tricky part was to find the way to serialize the
queue items for the heappush and heappop to behave
as required.

Part 2 was incredible hard to figure out, and I
could not do it by myself. From the subreddit, it
is hinted that a traditional set of visited nodes
to determine algorithm exit would not work.

After some laboration and readin some hints, I
managed to find a solution where the code keeps
tracks using a defaultdict.
2025-01-05 00:06:18 +01:00
fbe6994e3a Solve 2024:16 pt1 "Reindeer Maze" 2025-01-05 00:06:18 +01:00
2af9dc566a Solve 2024:15 p1 "Warehouse Woes" 2025-01-05 00:06:18 +01:00
e6307795a4 Solve 2024:14 p1-2 "Restroom Redoubt"
After working overtime to solve day 12, this brief
task was a much welcome change.

For pt 1, the code uses integer division and
modulus to update all positions. The tricky part
was to determine which quadrant got the middle
column and row (since dimensions was odd numbers,
and either left or right quandrants is +1 longer),
which was straightforward to verify by the test
cases.

For pt 2, the remains of the code used to visually
identify the tree is left under the `find_easter_egg`
toggle.

Basically, the code prints the grid with
all robots marked as a `#`, and free space as `.`.`

This wording of the puzzle is important:

> very rarely, _most of the robots_ should arrange
> themselves into a picture of a Christmas tree.

"most of the robots" means the tree will be quite
visible, which also means it is safe to asume a
significant cluster of "#" would appear over time.

By keeping track of the counter (seconds the robots
has traveled), it was evident that the cluster of `#`
occoured th first time at i=64 and every 103th
time forward.

In other words, `i % 103 == 64` will most likely
give a tree. The print statement is therefore
limited to those i's, and at `i == easter_egg_appearance`
the tree is visible.

So, to be extra clear: 64, 103 and pt2 answer is
unique to my puzzle input. If this code is used
with any other puzzle input, these numbers will
most likely vary.

For the fun, the code also contains the `display_easter_egg`
flag to actually print the tree. This is provided
to indicate how big the actual tree is: 33*36 chars.

Also, the `sints` utility function was added to
extract all signed ints from a string.
2025-01-05 00:06:18 +01:00
9a7a9c878b Solve 2024:12 p2 "Garden Groups"
Funny that original 2023 day 5 also was a PITA
to figure out.

Pt 1 was solved using BFS to flood-fill. After
trying some different methods for pt 2, including:

- wallcrawling,
- side couting,
- corner counting

I never produced code to get past the test cases.

- Wall crawling are hard due to overlapping
regions.
- corner couting and side counting are both hard,
but will act as equally good solutions (since side
count equals corner count).
- Concave corners are hard, convex corners are
easy.

The final code is based on the posts on the
solutions megathread. Changes:

- Keep all areas in a set, defining a region.
- find all convex and concave corners in each
region.

A new helper got introduced: Di, storing all
diagonal neighbors for grid traversing.

Convex corners:

..  R.  .R  ..
R.  ..  ..  .R

Concave corners:

RR  .R  R.  RR
.R  RR  RR  R.
2025-01-05 00:06:18 +01:00
1e807b5daf Solve 2024:13 p1-2 "Claw Contraption"
Initial version of the code tried to solve pt 1
using BFS, which took way too long even for the
test data.

After some fiddling with algebra with pen and paper,
I realized this 2 formulaes (using first example):

94 * b1 + 22 * b2 == 840
34 * b1 + 67 * b2 == 540

*b1 = button A presses
*b2 = button B presses

... could be rewritten to this single expression:

(94 + 34) * b1 + (22 + 67) * b2 = 840 * 540

I failed to remember the algebra for solving x than
y though, that I had to learn from the subreddit.
In the code, this is the ratio part.

Also, this solution using fractions is SICK.
https://www.reddit.com/r/adventofcode/comments/1hd4wda/comment/m1tz3nf/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button
2025-01-05 00:06:18 +01:00
19aee1363a Solve 2024:12 p1 "Garden Groups"
Loooong time overdue. First drafts of the code
kept track of every id separately in a defaultdict.

This was not sustainable since the code examples
had some recurring ids, and after managing to
support the test cases the code was eay off for
the real puzzle input.

In the end, the whole code was deleted and replaced
by a BFS solution that expands the current regions
until it finds no more areas.

Got the trick to count all perimiters in an else clause
from a leaderboard placed youtuber.
2025-01-05 00:06:18 +01:00
0eb961d31c Solve 2024:11 p1-2 "Plutonian Pebbles"
First part was solved using a list to store stones,
since I misinterpreted this paragraph:

> No matter how the stones change, their order is
> preserved, and they stay on their perfectly
> straight line.

But since the puzzle question do not require the
stones in the correct order, a list (or an OrderedDict)
is not required.

The pt 1 solution code got killed for eating all
RAM, so valuable minutes was lost trying to create
a generator instead. It failed. When searching for
clues, I ran across multiple people instead
utilizing dicts to count occourences. "but what of
the order?", I asked, and realized only the counts
was relevant.
2025-01-05 00:06:18 +01:00
0a812572db Solve 2024:10 p1-2 "Hoof It"
A bug in a draft version of the code accidentically
gave the answer to part 2. That's a first! :D
2025-01-05 00:06:18 +01:00
1b6502df7a Solve 2024:9 p1-2 "Disk Fragmenter"
Hard one. Comparing to earlier years, the
difficulty level seems higher than usual. I blame
the LLM hipsters.

This one I asumed, but missed when I tried to
find it:
> "If a block contains free space, skip it instead."

Lucky me.
2025-01-05 00:06:18 +01:00
dc44be89bf Solve 2024:8 p1-2 "Resonant Collinearity"
As many have said, the puzzle text this day
was a bit challenging to parse and understand.

Especially this part of pt 2 was easy to miss:

> In fact, the three T-frequency *antennas* are all
> exactly in line with two antennas,
> so *they are all also antinodes*!

not including the antennas in pt 2 gave an slightly
too low answer.
2025-01-05 00:06:18 +01:00
81fe14a407 Solve 2024:7 p1-2 "Bridge Repair"
Got thrown of big time at pt 1 since there is a
duplicate test value in my puzzle input. The code
initially asumed that all test values should be
distinct. Biased from seeing "test" in the
termology, most likely.

Earlier editions of the code also tried to create
all combinations using binary strings, but it fell
short on pt 2 when a third operation was introduced.

From some inspiration in the solutions mega
thread on Reddit, the final code is queue-based
instead. Apparently, I also learned this kind of
problem is usually well suited for DFS search, and
apparently the final code very much is _in deed_
a DFS.
2025-01-05 00:06:18 +01:00
7b2b101b8b Solve 2024:6 p1-2 "Guard Gallivant"
Pt 1 was easy, Pt 2 was pure horror.

Earlier drafts of the code tried to be way too
smart. At one point, I just came to the conclusion
to place a new obstacle (#) on the grid and just
rerun the thing to look for loops.

2 things:

- the visited positions from pt 1 can be used as a
subset for positions to consider for the extra
"#".
- The track of loops can be optimized to look at
bounces on "#"s instead of each individual y,x pos,
given that the direction is remembered.

pt 2 is familiar, the last time a puzzle required
look detection the puzzle used lazer beams and
reflectors. Not sure what Event or day it was.
2025-01-05 00:06:18 +01:00
44f6aa0f53 Solve 2024:5 p1-2 "Print Queue"
In p2, it took some tries to find a correct way
to rearrange incorrect pagesets. The code ended
up just rearranging first found incorrect order
and readding the updated pageset to queue.
2025-01-05 00:06:18 +01:00
f751b3b8d5 Solve 2024:4 p1-2 "Ceres Search"
Not gonna lie, This is not my gig skillwise. I had
much fun though!

A matrix would most likely make the code more
readable.

I relied massively on test cases here, so I share
them here instead of in the source code:

assert 1 == solve("""
    X.....
    .M....
    ..A...
    ...S..
    ..X...
    """.strip())[0]

    assert 2 == solve("""
    XS....
    .MA...
    ..AM..
    ...SX.
    ..X...
    """.strip())[0]

    assert 2 == solve("""
    ..S......
    ...A.....
    ....M....
    .....X...
    ..X...M..
    .......A.
    ........S
    """.strip())[0]

    assert 4 == solve("""
    X.SS
    M.AA
    A.MM
    S.XX
    ...M
    ...A
    ...S
    """.strip())[0]

    assert 4 == solve("""
    ...X...
    ...M...
    .X.A...
    XMASAMX
    .A.....
    .S.....
    """.strip())[0]

    assert 1 == solve("""
    ....X
    ...M.
    ..A..
    .S...
    """.strip())[0]

    assert 2 == solve("""
    ...SX
    ..AM.
    .MA..
    XS...
    """.strip())[0]

    assert 2 == solve("""
    ......X
    .....M.
    ....A..
    ...S...
    ..A....
    .M.....
    X......
    """.strip())[0]

    assert 1 == solve("""
    M.S
    .A.
    M.S
    """.strip())[1]

    assert 1 == solve("""
    M.M
    .A.
    S.S
    """.strip())[1]

    assert 1 == solve("""
    S.M
    .A.
    S.M
    """.strip())[1]

    assert 1 == solve("""
    S.S
    .A.
    M.M
    """.strip())[1]

    assert 1 == solve("""
    S.S
    .A.
    M.M
    """.strip())[1]

    assert 1 == solve("""
    .A.
    M.M
    .A.
    S.S
    """.strip())[1]

    assert 1 == solve("""
    M.M.
    .A.A
    S.S.
    """.strip())[1]

    assert 1 == solve("""
    M.M
    .A.
    S.S
    .A.
    """.strip())[1]

    assert 1 == solve("""
    .M.M
    A.A.
    .S.S
    """.strip())[1]
2025-01-05 00:06:18 +01:00