~cdv/aoc-2018-rs

927c0ac7fdf7847f1b640d3802d55d8c33d39222 — Christopher Vittal 2 years ago bb809c1
rustfmt
2 files changed, 71 insertions(+), 50 deletions(-)

M src/bin/day12.rs
M src/bin/day13.rs
M src/bin/day12.rs => src/bin/day12.rs +28 -18
@@ 42,17 42,21 @@ fn rule(m: &[u8]) -> u8 {

fn last(l: &[u8]) -> [u8; 4] {
    let n = l.len();
    [rule(&[l[n - 4], l[n - 3], l[n - 2], l[n - 1], b'.']),
     rule(&[l[n - 3], l[n - 2], l[n - 1], b'.', b'.']),
     rule(&[l[n - 2], l[n - 1], b'.', b'.', b'.']),
     rule(&[l[n - 1], b'.', b'.', b'.', b'.'])]
    [
        rule(&[l[n - 4], l[n - 3], l[n - 2], l[n - 1], b'.']),
        rule(&[l[n - 3], l[n - 2], l[n - 1], b'.', b'.']),
        rule(&[l[n - 2], l[n - 1], b'.', b'.', b'.']),
        rule(&[l[n - 1], b'.', b'.', b'.', b'.']),
    ]
}

fn generation(line: Vec<u8>, zero: isize) -> (Vec<u8>, isize) {
    let first = [rule(&[b'.', b'.', b'.', b'.', line[0]]),
                 rule(&[b'.', b'.', b'.', line[0], line[1]]),
                 rule(&[b'.', b'.', line[0], line[1], line[2]]),
                 rule(&[b'.', line[0], line[1], line[2], line[3]])];
    let first = [
        rule(&[b'.', b'.', b'.', b'.', line[0]]),
        rule(&[b'.', b'.', b'.', line[0], line[1]]),
        rule(&[b'.', b'.', line[0], line[1], line[2]]),
        rule(&[b'.', line[0], line[1], line[2], line[3]]),
    ];
    let first = if let Some(p) = first.iter().position(|&b| b == b'#') {
        &first[p..]
    } else {


@@ 62,20 66,26 @@ fn generation(line: Vec<u8>, zero: isize) -> (Vec<u8>, isize) {
    let next = line.windows(5).map(rule);
    let last = last(&line);
    let last = if let Some(p) = last.iter().rposition(|&b| b == b'#') {
        &last[0..p+1]
        &last[0..p + 1]
    } else {
        &[]
    };
    (first.iter().cloned().chain(next).chain(last.iter().cloned()).collect::<Vec<_>>(),
     zero + first.len() as isize - 2)
    (
        first
            .iter()
            .cloned()
            .chain(next)
            .chain(last.iter().cloned())
            .collect::<Vec<_>>(),
        zero + first.len() as isize - 2,
    )
}

fn plant_sum(line: &[u8], zero: isize) -> isize {
    line.iter().enumerate().map(|(i, &e)| if e == b'#' {
        (i as isize - zero)
    } else {
        0
    }).sum()
    line.iter()
        .enumerate()
        .map(|(i, &e)| if e == b'#' { (i as isize - zero) } else { 0 })
        .sum()
}

fn main() {


@@ 101,7 111,7 @@ fn main() {
        zero = gen.1;
        let prev = sum;
        sum = plant_sum(&line, zero);
        if diff == sum-prev {
        if diff == sum - prev {
            heuristic += 1;
        } else {
            heuristic = 0;


@@ 111,6 121,6 @@ fn main() {
        }
        diff = sum - prev;
        end_gen += 1;
    };
    }
    println!("  2: {}", sum + (50_000_000_000 - end_gen) * diff);
}

M src/bin/day13.rs => src/bin/day13.rs +43 -32
@@ 85,25 85,32 @@ impl AddAssign<Turn> for Dir {
fn main() {
    let (board, mut carts): (Vec<Vec<Point>>, Vec<Cart>) = aoc::file::to_lines(INPUT)
        .enumerate()
        .map(|(i, l)| l.unwrap().bytes().enumerate().map(|(j, b)| {
            match b {
                b'|' => (Point::Vert, None),
                b'-' => (Point::Horiz, None),
                b'\\' => (Point::TurnSl, None),
                b'/' => (Point::TurnFw, None),
                b'+' => (Point::Inter, None),
                b'^' => (Point::Vert, Some(Cart::new(Dir::Up, i, j))),
                b'v' => (Point::Vert, Some(Cart::new(Dir::Dn, i, j))),
                b'>' => (Point::Horiz, Some(Cart::new(Dir::Rt, i, j))),
                b'<' => (Point::Horiz, Some(Cart::new(Dir::Lf, i, j))),
                _ => (Point::None, None),
            }
        }).unzip())
        .fold((vec![], vec![]), |mut a, (pts, cts): (Vec<Point>, Vec<Option<Cart>>)| {
            a.0.push(pts);
            a.1.extend(cts.into_iter().filter_map(|c| c));
            a
        });
        .map(|(i, l)| {
            l.unwrap()
                .bytes()
                .enumerate()
                .map(|(j, b)| match b {
                    b'|' => (Point::Vert, None),
                    b'-' => (Point::Horiz, None),
                    b'\\' => (Point::TurnSl, None),
                    b'/' => (Point::TurnFw, None),
                    b'+' => (Point::Inter, None),
                    b'^' => (Point::Vert, Some(Cart::new(Dir::Up, i, j))),
                    b'v' => (Point::Vert, Some(Cart::new(Dir::Dn, i, j))),
                    b'>' => (Point::Horiz, Some(Cart::new(Dir::Rt, i, j))),
                    b'<' => (Point::Horiz, Some(Cart::new(Dir::Lf, i, j))),
                    _ => (Point::None, None),
                })
                .unzip()
        })
        .fold(
            (vec![], vec![]),
            |mut a, (pts, cts): (Vec<Point>, Vec<Option<Cart>>)| {
                a.0.push(pts);
                a.1.extend(cts.into_iter().filter_map(|c| c));
                a
            },
        );
    let mut p1 = true;
    while carts.len() > 1 {
        carts.sort_by(|c1, c2| c2.xy().cmp(&c1.xy()));


@@ 128,19 135,23 @@ fn main() {
            } else {
                let (x, y) = cart.xy();
                match board[x][y] {
                    Point::Vert | Point::Horiz => {},
                    Point::TurnSl => cart.dir = match cart.dir {
                        Dir::Up => Dir::Lf,
                        Dir::Rt => Dir::Dn,
                        Dir::Lf => Dir::Up,
                        Dir::Dn => Dir::Rt,
                    },
                    Point::TurnFw => cart.dir = match cart.dir {
                        Dir::Up => Dir::Rt,
                        Dir::Rt => Dir::Up,
                        Dir::Lf => Dir::Dn,
                        Dir::Dn => Dir::Lf,
                    },
                    Point::Vert | Point::Horiz => {}
                    Point::TurnSl => {
                        cart.dir = match cart.dir {
                            Dir::Up => Dir::Lf,
                            Dir::Rt => Dir::Dn,
                            Dir::Lf => Dir::Up,
                            Dir::Dn => Dir::Rt,
                        }
                    }
                    Point::TurnFw => {
                        cart.dir = match cart.dir {
                            Dir::Up => Dir::Rt,
                            Dir::Rt => Dir::Up,
                            Dir::Lf => Dir::Dn,
                            Dir::Dn => Dir::Lf,
                        }
                    }
                    Point::Inter => {
                        cart.dir += cart.next;
                        cart.next = match cart.next {