86 lines
1.4 KiB
Rust
86 lines
1.4 KiB
Rust
mod model;
|
|
mod parsing;
|
|
|
|
use aoc_runner_derive::{aoc, aoc_generator};
|
|
pub use model::{Fold, Grid, Input, Point};
|
|
use yap::IntoTokens;
|
|
|
|
#[aoc_generator(day13)]
|
|
pub fn parse_input(input: &str) -> Input {
|
|
parsing::parse_input(&mut input.into_tokens()).unwrap()
|
|
}
|
|
|
|
#[aoc(day13, part1)]
|
|
pub fn part1(input: &Input) -> usize {
|
|
let Input {
|
|
mut grid,
|
|
instructions,
|
|
} = input.clone();
|
|
grid.fold(instructions[0]);
|
|
grid.count()
|
|
}
|
|
|
|
#[aoc(day13, part2)]
|
|
pub fn part2(input: &Input) -> String {
|
|
let Input {
|
|
mut grid,
|
|
instructions,
|
|
} = input.clone();
|
|
|
|
for instruction in instructions {
|
|
grid.fold(instruction);
|
|
}
|
|
grid.to_string()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::Input;
|
|
|
|
const EXAMPLE_INPUT: &str = "6,10
|
|
0,14
|
|
9,10
|
|
0,3
|
|
10,4
|
|
4,11
|
|
6,0
|
|
6,12
|
|
4,1
|
|
0,13
|
|
10,12
|
|
3,4
|
|
3,0
|
|
8,4
|
|
1,10
|
|
2,14
|
|
8,10
|
|
9,0
|
|
|
|
fold along y=7
|
|
fold along x=5";
|
|
const RESULT_PART_1: usize = 17;
|
|
|
|
#[test]
|
|
fn part1_example() {
|
|
let result = super::part1(&super::parse_input(EXAMPLE_INPUT));
|
|
assert_eq!(result, RESULT_PART_1);
|
|
}
|
|
|
|
#[test]
|
|
fn fold_twice() {
|
|
let Input {
|
|
mut grid,
|
|
instructions,
|
|
} = super::parse_input(EXAMPLE_INPUT);
|
|
grid.fold(instructions[0]);
|
|
assert_eq!(grid.count(), 17);
|
|
grid.fold(instructions[1]);
|
|
assert_eq!(grid.count(), 16);
|
|
}
|
|
|
|
#[test]
|
|
fn parse_example() {
|
|
super::parse_input(EXAMPLE_INPUT);
|
|
}
|
|
}
|