Ancestors

Toot

Written by modulux on 2024-12-17 at 16:13

I've been solving AoC these days but forgot to post my solutions.

Day 15 was not computationally difficult or requiring of large amounts of maths or complex algorithms, but it was the sort of problem full of detail where you can screw things up easily

Task 1.

fn main() {

let f = read_to_string("input.txt").unwrap();

let mut f = f.split("\n\n");

let tiles = f.next().unwrap();

let mut map = Vec::new();

let mut line = Vec::new();

for i in tiles.chars() {

    if i == '\n' {

        map.push(line);

        line = vec![];

    } else {

        line.push(i);

    }

}

map.push(line);

for i in f.next().unwrap().chars() {

    let rh = map

        .iter()

        .filter_map(|e| e.iter().position(|e2| *e2 == '@'))

        .next()

        .unwrap();

    let rv = map.iter().position(|e| e.contains(&'@')).unwrap();

    match i {

        'v' => {

            let wall = map[rv + 1..]

                .iter()

                .position(|e| e[rh] == '#')

                .map(|e| e + rv + 1)

                .unwrap();

            if let Some(hole) = map[rv + 1..wall]

                .iter()

                .position(|e| e[rh] == '.')

                .map(|e| e + rv + 1)

            {

                let t = map[rv + 1][rh];

                map[hole][rh] = t;

                map[rv][rh] = '.';

                map[rv + 1][rh] = '@';

            }

        }

        '>' => {

            let wall = map[rv][rh + 1..]

                .iter()

                .position(|e| *e == '#')

                .map(|e| e + rh + 1)

                .unwrap();

            if let Some(hole) = map[rv][rh + 1..wall]

                .iter()

                .position(|e| *e == '.')

                .map(|e| e + rh + 1)

            {

                let t = map[rv][rh + 1];

                map[rv][hole] = t;

                map[rv][rh] = '.';

                map[rv][rh + 1] = '@';

            }

        }

        '<' => {

            let wall = map[rv][..rh]

                .iter()

                .enumerate()

                .filter(|e| *e.1 == '#')

                .max_by_key(|k| k.0)

                .map(|e| e.0)

                .unwrap();

            if let Some(hole) = map[rv][wall..=rh - 1]

                .iter()

                .enumerate()

                .filter(|e| *e.1 == '.')

                .max_by_key(|k| k.0)

                .map(|k| k.0 + wall)

            {

                let t = map[rv][rh - 1];

                map[rv][hole] = t;

                map[rv][rh] = '.';

                map[rv][rh - 1] = '@';

            }

        }

        '^' => {

            let wall = map[..=rv - 1]

                .iter()

                .enumerate()

                .filter(|e| e.1[rh] == '#')

                .max_by_key(|k| k.0)

                .map(|e| e.0)

                .unwrap();

            if let Some(hole) = map[wall..=rv - 1]

                .iter()

                .enumerate()

                .filter(|e| e.1[rh] == '.')

                .max_by_key(|k| k.0)

                .map(|e| e.0 + wall)

            {

                let t = map[rv - 1][rh];

                map[hole][rh] = t;

                map[rv][rh] = '.';

                map[rv - 1][rh] = '@';

            }

        }

        _ => (),

    }

}

let mut result = 0;

for i in 0..map.len() {

    for j in 0..map[0].len() {

        if map[i][j] == 'O' {

            result += (100 * i) + j;

        }

    }

}

println!("{}", result);

}

As you can see, nothing too special. The only fun insight was finding the wall and the hole, and that you only need to move the robot, the box right by the robot, and put it in the hole.

[#]aoc24

=> More informations about this toot | More toots from modulux@node.isonomia.net

Descendants

Proxy Information
Original URL
gemini://mastogem.picasoft.net/thread/113669045993310830
Status Code
Success (20)
Meta
text/gemini
Capsule Response Time
243.529025 milliseconds
Gemini-to-HTML Time
1.75675 milliseconds

This content has been proxied by September (3851b).