summary refs log tree commit diff homepage
path: root/2016/src/bin/day13.rs
blob: 060f571d101d527924ca310ca6b1ba3c0602c89b (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use std::collections::{HashMap, VecDeque};
use std::io::{self, Read};

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct Point {
    x: u32,
    y: u32,
}

const START: Point = Point { x: 1, y: 1 };

impl Point {
    fn north(self) -> Option<Point> {
        if self.y == 0 {
            None
        } else {
            Some(Point { x: self.x, y: self.y - 1 })
        }
    }

    fn east(self) -> Point {
        Point { x: self.x + 1, y: self.y }
    }

    fn south(self) -> Point {
        Point { x: self.x, y: self.y + 1 }
    }

    fn west(self) -> Option<Point> {
        if self.x == 0 {
            None
        } else {
            Some(Point { x: self.x - 1, y: self.y })
        }
    }

    fn neighbors(self) -> [Option<Point>; 4] {
        [self.north(), Some(self.east()), Some(self.south()), self.west()]
    }
}

#[derive(Clone, Copy)]
struct Layout {
    constant: u32,
}

impl Layout {
    fn is_open(&self, point: Point) -> bool {
        let Point { x, y } = point;
        let sum = x * x + 3 * x + 2 * x * y + y + y * y + self.constant;
        sum.count_ones() % 2 == 0
    }
}

fn solve1(goal: Point, constant: u32) -> u32 {
    let layout = Layout { constant: constant };

    let mut distance = HashMap::new();
    let mut frontier = VecDeque::new();
    frontier.push_back(START);
    distance.insert(START, 0);

    while let Some(current) = frontier.pop_front() {
        if current == goal { break; }
        let current_distance = *distance.get(&current).unwrap();

        let neighbors = current.neighbors();
        let iter = neighbors.iter()
            .filter_map(|&p| p)
            .filter(|&p| layout.is_open(p));

        for next in iter {
            if !distance.contains_key(&next) {
                frontier.push_back(next);
                distance.insert(next, current_distance + 1);
            }
        }
    }

    *distance.get(&goal).unwrap()
}

fn solve2(limit: u32, constant: u32) -> usize {
    let layout = Layout { constant: constant };

    let mut distance = HashMap::new();
    let mut frontier = VecDeque::new();
    frontier.push_back(START);
    distance.insert(START, 0);

    while let Some(current) = frontier.pop_front() {
        let current_distance = *distance.get(&current).unwrap();
        if current_distance == limit { continue; }

        let neighbors = current.neighbors();
        let iter = neighbors.iter()
            .filter_map(|&p| p)
            .filter(|&p| layout.is_open(p));

        for next in iter {
            if !distance.contains_key(&next) {
                frontier.push_back(next);
                distance.insert(next, current_distance + 1);
            }
        }
    }

    distance.len()
}

fn main() {
    let mut input = String::new();
    io::stdin().read_to_string(&mut input).unwrap();

    println!("Part 1: {}", solve1(Point { x: 31, y: 39 }, input.trim().parse().unwrap()));
    println!("Part 2: {}", solve2(50, input.trim().parse().unwrap()));
}

#[test]
fn part1() {
    assert_eq!(11, solve1(Point { x: 7, y: 4 }, 10));
}
s='logsubject'>Remove unneeded includes in ui.cJune McEnroe 2022-02-19Reimplement tab completeJune McEnroe 2022-02-19Handle errors from editFn, etc.June McEnroe 2022-02-19Reimplement text macrosJune McEnroe 2022-02-19Factor out input handling to input.cJune McEnroe 2022-02-19Factor out window management to window.cJune McEnroe 2022-02-19Enable -Wmissing-prototypesJune McEnroe In other words, warn when a function is missing static. I don't see why this isn't in -Wextra. 2022-02-19Fix edit.[ch] license notice additional permissionsJune McEnroe 2022-02-19Run line editing testsJune McEnroe I know, it feels wrong. 2022-02-18Implement new line editing "library"June McEnroe Losing tab complete and text macros, for now. This new implementation works on an instance of a struct and does not interact with the rest of catgirl, making it possible to copy into another project. Unlike existing line editing libraries, this one is entirely abstract and can be rendered externally. My goal with this library is to be able to implement vi mode. Since it operates on struct instances rather than globals, it might also be possible to give catgirl separate line editing buffers for each window, which would be a nice UX improvement. 2022-02-18Simplify cursor positioning in inputJune McEnroe Do some extra work by adding the portion before the cursor to the input window twice, but simplify the interaction with the split point. This fixes the awkward behaviour when moving the cursor across colour codes where the code would be partially interpreted up to the cursor. 2022-02-18Fix M-f orderingJune McEnroe 2022-02-12Move sandman build to scripts/MakefileJune McEnroe 2022-02-12Use compat_readpassphrase.c on LinuxJune McEnroe 2022-02-12Copy RPP defines from oconfigureJune McEnroe