26
11

I wanted to show how the maze follows from individual letters but it was way too large

27
12
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 11: Cosmic Expansion

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 9 mins

28
24
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 10: Pipe Maze

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 40 mins

29
60
30
21
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 9: Mirage Maintenance

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 5 mins

31
15
32
22

This map was really opaque until yEd helped explain the problem...

33
6

I had 6 parallel paths, and for all of them, the goal node was found at the point where the directions string repeated. So I actually only had to search each of the paths for the point where I was on the goal position and the first direction. This worked on the input data, but not the example. See the assert in the code, and note that the statement println!("Found non-repeat at {node} step {step} (num dirs {num_directions})"); is never executed.

At the end there's also the first attempt of brute-forcing the solution which seems like it would have taken nearly forever.

use std::collections::HashMap;

use regex::Regex;

use crate::input::Source;

pub fn execute(source: Source) -> impl std::fmt::Debug {
    let lines = source.lines();
    let ([directions], [empty, nodes @ ..]) = lines.split_at(1) else {
        panic!("Wrong format");
    };
    assert!(empty.is_empty());

    let node_re = Regex::new(r"(...) = \((...), (...)\)").unwrap();
    let nodes: HashMap = nodes
        .iter()
        .map(|line| {
            let caps = node_re.captures(line).unwrap();
            let name = caps[1].to_string();
            let left = caps[2].to_string();
            let right = caps[3].to_string();
            (name, (left, right))
        })
        .collect();

    let start_nodes: Vec = nodes.keys().filter(|n| n.ends_with('A')).cloned().collect();
    let repeat_lengths: Vec = start_nodes
        .iter()
        .map(|node| {
            let (repeat_length, goal_steps) = find_repeats(node, directions, &nodes);
            assert!(goal_steps.is_empty()); // not a given, but that's what the input looks like
            dbg!(node, repeat_length, goal_steps);
            repeat_length
        })
        .collect();

    repeat_lengths.iter().cloned().reduce(num::integer::lcm)
}

/// (repeat length, steps to goal node within repeat)
fn find_repeats(
    starting_node: &str,
    directions: &str,
    nodes: &HashMap,
) -> (usize, Vec) {
    let mut node = starting_node.to_string();
    let num_directions = directions.chars().count();
    let mut goals = Vec::new();
    for (step, dir) in directions.chars().cycle().enumerate() {
        if node.ends_with('Z') {
            if step % num_directions == 0 {
                println!("Found repeat at {node} step {step} (num dirs {num_directions})");
                return (step, goals);
            }
            println!("Found non-repeat at {node} step {step} (num dirs {num_directions})");
            goals.push(step);
        }
        let (left, right) = &nodes[&node];
        node = match dir {
            'L' => left.clone(),
            'R' => right.clone(),
            other => panic!("Unknown dir {other}"),
        }
    }
    unreachable!()
}

// too slow
#[allow(dead_code)]
fn find_parallel(
    start_nodes: &[String],
    directions: &str,
    nodes: &HashMap,
) -> usize {
    let mut current_nodes = start_nodes.to_vec();
    for (step, dir) in directions.chars().cycle().enumerate() {
        if current_nodes.iter().all(|n| n.ends_with('Z')) {
            return step;
        }
        for node in current_nodes.iter_mut() {
            let (left, right) = nodes[node].clone();
            *node = match dir {
                'L' => left,
                'R' => right,
                other => panic!("Unknown dir {other}"),
            }
        }
    }
    unreachable!()
}
34
21

Day 8: Haunted Wasteland

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

35
0

The instructions for part 2 are missing info about how to handle the Four Of A Kind and a joker case. Wasted quite some time because I assumed that the remaining joker would remain unused, but turns out it turns your deck into Five Of A Kind.

Did everybody else just expect this?

36
18
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 7: Camel Cards

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there's at least 100 2 star entries on the global leaderboard

🔓 Thread has been unlocked after around 20 mins

37
17

Survey by u/jeroenheijmans Closes Dec 22

38
34
Noticing pattern... (programming.dev)

Credits to u/Felix_Tholomyes over on r/adventofcode

39
132
40
6

I messed around in desmos for like 20 mins trying to find an algebraic solution to day 6 and made this. I feel as if this is the best way to solve the problem by graphing f(x) = (x-max_time/2)^2. Finding its y-intercept, finding its vertex (max_time/2) and then finding the minimum x needed to get the max distance by solving for max_distance = f(x).

fn main() {
    let mut raw_input = include_str!("input.txt")
        .lines()
        .map(|f| {

        f.split_ascii_whitespace()
        .skip(1)
        .collect::<String>()
        .parse::<usize>()
        .unwrap()
        
        //for part 1, first parse then collect to a Vec<&str>

    }) ;

    let max_time = raw_input.next().unwrap();
    let max_distance = raw_input.next().unwrap();

    print!("{}", determine_range(max_time, max_distance));

    //let max_times = raw_input.next().unwrap();
    //let max_distances = raw_input.next().unwrap();

    // let races = (0..max_times.len()).map(|i| {
    //     determine_range(max_times[i], max_distances[i])
    // });

    // let total = races.reduce(|acc,x| x*acc).unwrap();
}

fn determine_range(max_time: usize, max_dist: usize) -> f64 {
    let vertex = max_time as f64/2.0;
    let min_y = vertex * vertex - max_dist as f64;
    let min_x = vertex - min_y.sqrt()+ 0.001;

    let mut res = 2.0 * ( (vertex-0.001).floor() - min_x.ceil() + 1.0);
    if vertex.fract() == 0.0 {
        res+=1.0;
    }
    res
}
41
23
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 6: Wait for It


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ

42
23
43
34
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 5: If You Give a Seed a Fertilizer


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Unlocked after 27 mins (current record for time, hard one today)

44
47
45
30

Am I in danger?

46
12
submitted 8 months ago* (last edited 8 months ago) by ooterness@lemmy.world to c/advent_of_code@programming.dev

If you're writing Advent of Code solutions in Rust, then I've written a crate that can fetch the user input data directly from the main website.

Long story short, you provide it a login token copied from your browser cookies, and it can fetch the input data by year and day. Inputs are cached locally, so it'll only download it once for a given problem. This was heavily inspired by the PyPi advent-of-code-data package.

Unlike other AoC-centric Rust crates, that's all it does. The other crates I've seen all want the code structured in a specific way to add timing benchmarks, unit testing, and other features. I wanted something lightweight where you just call a function to get the input; no more and no less.

To use the crate:

  • Follow the AoCD instructions to set the AOC_SESSION environment variable.
    This key is used for authentication and should not be shared with anyone.
  • Add the aocfetch crate to your Cargo.toml [dependencies] section:
    aocfetch = { git = "https://github.com/ooterness/AdventOfCode.git" }
  • Import the crate and call aocfetch::get_data(year, day) to fetch your input data.

An example:

use aocfetch;

fn main() {
    let input = aocfetch::get_data(2023, 1).unwrap();
    println!("My input data: {}", input);
    println!("Part 1 solution: 42");    // TODO
    println!("Part 2 solution: 42");    // TODO
}

If this goes well I will submit it to crates.io, but I wanted to open this up for beta-testing first.

47
24
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 4: Scratchcards


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Unlocked after 8 mins

48
2

I know I'm not the only one finding day 3 frustrating.

My current code for day 3 is here. I find the locations of all of the special symbols, check around them to get the numbers, and dedup the list of numbers I find. To make things easier to debug, I generated some supplementary text files. One shows the input I used, but with all of the numbers I used replaced with 'x's: the output doesn't suggest any problems with my code that are obvious. The other has each number that went into the sum on its own line. I checked a few of these numbers and they seemed correct, but there are too many to check by hand.

What could I be doing wrong? I've officially run out of ideas.

49
94

Thanks Homer.

50
20
submitted 8 months ago* (last edited 8 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

Day 3: Gear Ratios


Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ or pastebin (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒This post will be unlocked when there is a decent amount of submissions on the leaderboard to avoid cheating for top spots

🔓 Edit: Post has been unlocked after 11 minutes

view more: ‹ prev next ›

Advent Of Code

736 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS