summaryrefslogtreecommitdiff
path: root/2022/src/bin/day_5.rs
blob: f06012cf1e224d29d0eb3cb635ced57929668668 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use nom::{
    branch::alt,
    bytes::complete::tag,
    character::complete::{
        anychar, char as nom_char, line_ending, not_line_ending, u32 as nom_u32,
    },
    combinator::map,
    multi::separated_list1,
    sequence::tuple,
    IResult,
};
use std::fs;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let input = fs::read_to_string("inputs/day_5.txt")?;
    let state = CraneState::parser(&input).unwrap().1;

    let mut state_part_1 = state.clone();
    while !state_part_1.done() {
        state_part_1.process_next_instruction(false);
    }
    dbg!(state_part_1.read_top_row());

    let mut state_part_2 = state.clone();
    while !state_part_2.done() {
        state_part_2.process_next_instruction(true);
    }
    dbg!(state_part_2.read_top_row());

    Ok(())
}

#[derive(Debug, PartialEq, Eq, Clone)]
struct CraneState {
    towers: Vec<Tower>,
    instructions: Vec<Instruction>,
}

#[derive(Debug, Default, PartialEq, Eq, Clone)]
struct Tower {
    crates: Vec<char>,
}

#[derive(Debug, PartialEq, Eq, Clone)]
struct Instruction {
    number: usize,
    src: usize,
    dest: usize,
}

impl CraneState {
    fn parser(input: &str) -> IResult<&str, CraneState> {
        let single_crate = alt((
            map(tuple((tag("["), anychar, tag("]"))), |(_, c, _)| Some(c)),
            map(tag("   "), |_| None),
        ));
        let crate_row = separated_list1(nom_char(' '), single_crate);

        map(
            tuple((
                separated_list1(line_ending, crate_row),
                line_ending,
                not_line_ending,
                line_ending,
                line_ending,
                separated_list1(line_ending, Instruction::parser),
            )),
            |(crate_rows, _, _, _, _, mut instructions)| {
                let mut towers = Vec::new();
                for row in &crate_rows {
                    for (i, c) in row
                        .iter()
                        .enumerate()
                        .filter_map(|(i, c)| c.map(|some_c| (i, some_c)))
                    {
                        while i >= towers.len() {
                            towers.push(Tower::default());
                        }
                        towers[i].crates.push(c);
                    }
                }
                for tower in &mut towers {
                    tower.crates.reverse();
                }

                instructions.reverse();
                CraneState {
                    towers,
                    instructions,
                }
            },
        )(input)
    }

    fn process_next_instruction(&mut self, maintain_order: bool) {
        let Some(instruction) = self.instructions.pop() else {
            return
        };
        let mut to_move = Vec::new();
        for _ in 0..instruction.number {
            let Some(crate_to_move) = self.towers[instruction.src].crates.pop() else {
                panic!("Invalid puzzle input: failed to get crate");
            };
            to_move.push(crate_to_move);
        }
        if maintain_order {
            to_move.reverse();
        }
        for crate_to_move in to_move {
            self.towers[instruction.dest].crates.push(crate_to_move);
        }
    }

    fn done(&self) -> bool {
        self.instructions.len() == 0
    }

    fn read_top_row(&self) -> String {
        let mut res = String::new();
        for tower in &self.towers {
            if let Some(top) = tower.crates.last() {
                res.push(*top);
            }
        }
        res
    }
}

impl Instruction {
    fn parser(input: &str) -> IResult<&str, Instruction> {
        map(
            tuple((
                tag("move "),
                nom_u32,
                tag(" from "),
                nom_u32,
                tag(" to "),
                nom_u32,
            )),
            |(_, number, _, src, _, dest)| Instruction {
                number: number as usize,
                src: (src - 1) as usize,
                dest: (dest - 1) as usize,
            },
        )(input)
    }
}