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
|
use nom::{
branch::alt,
bytes::complete::tag,
character::complete::{i64 as nom_i64, line_ending, space1},
combinator::{map, value},
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_2.txt")?;
let route = parse_route(&input).unwrap().1;
let mut position = Position::default();
for instruction in &route {
position.advance(&instruction);
}
dbg!(position.horizontal.0 * position.depth.0);
Ok(())
}
#[derive(Debug)]
struct Route(Vec<Instruction>);
impl<'a> IntoIterator for &'a Route {
type Item = &'a Instruction;
type IntoIter = std::slice::Iter<'a, Instruction>;
fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
self.0.iter()
}
}
#[derive(Debug)]
struct Instruction {
direction: Direction,
distance: Distance,
}
#[derive(Debug, Clone)]
enum Direction {
Forward,
Up,
Down,
}
#[derive(
Default,
Debug,
Clone,
Copy,
derive_more::Add,
derive_more::AddAssign,
derive_more::Sub,
derive_more::SubAssign,
)]
struct Distance(i64);
#[derive(Default, Debug)]
struct Position {
horizontal: Distance,
depth: Distance,
}
impl Position {
fn advance(&mut self, instruction: &Instruction) {
match instruction.direction {
Direction::Forward => self.horizontal += instruction.distance,
Direction::Down => self.depth += instruction.distance,
Direction::Up => self.depth -= instruction.distance,
}
}
}
fn parse_route(input: &str) -> IResult<&str, Route> {
map(separated_list1(line_ending, parse_instruction), Route)(input)
}
fn parse_instruction(input: &str) -> IResult<&str, Instruction> {
map(
tuple((parse_direction, space1, parse_distance)),
|(direction, _, distance)| Instruction {
direction,
distance,
},
)(input)
}
fn parse_direction(input: &str) -> IResult<&str, Direction> {
alt((
value(Direction::Forward, tag("forward")),
value(Direction::Up, tag("up")),
value(Direction::Down, tag("down")),
))(input)
}
fn parse_distance(input: &str) -> IResult<&str, Distance> {
map(nom_i64, Distance)(input)
}
|