Compare commits
6 commits
cab4e2974d
...
cc2bc308c3
Author | SHA1 | Date | |
---|---|---|---|
Tristan Daniël Maat | cc2bc308c3 | ||
Tristan Daniël Maat | 361285e23d | ||
Tristan Daniël Maat | ab79670185 | ||
Tristan Daniël Maat | 0c61772aa0 | ||
Tristan Daniël Maat | 1eed1d2f85 | ||
Tristan Daniël Maat | 5a2aec7e76 |
1
day-3/.gitignore
vendored
Normal file
1
day-3/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
78
day-3/Cargo.lock
generated
Normal file
78
day-3/Cargo.lock
generated
Normal file
|
@ -0,0 +1,78 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-3"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
10
day-3/Cargo.toml
Normal file
10
day-3/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day-3"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
323
day-3/input
Normal file
323
day-3/input
Normal file
|
@ -0,0 +1,323 @@
|
|||
...#...#..#....#..#...#..##..#.
|
||||
.#..#.....#.#............###...
|
||||
.#...###....#.............##..#
|
||||
...##...##....#.....##..#.##...
|
||||
.....###.#.###..##.#.##.......#
|
||||
#...##.....#..........#..#.#.#.
|
||||
......##.......##..#....#.#....
|
||||
....#.###.##..#.#..##.##....#.#
|
||||
.......#.......###.#.#.##.....#
|
||||
.........#.#....#..........#.#.
|
||||
.#...##.....##.........#..#....
|
||||
.##....#.#.#...##......#.......
|
||||
##.#.#..#....#....#....#...#.#.
|
||||
##....#.#..##......#....##...#.
|
||||
....#..#..##..#.###.......#.#..
|
||||
.....##....###...........#.#.##
|
||||
#.....##.........#....##......#
|
||||
........###.#..#....#....#.....
|
||||
...#.......#.##..#.###......#..
|
||||
...............#..#....#.##....
|
||||
..#..###..#.#..#.........##..#.
|
||||
####..#..####..................
|
||||
#...####...#.......#.#.#...#...
|
||||
......###.....#......#..#..#...
|
||||
#...#.....##.....#.#..##...#.#.
|
||||
#...........##.......#.........
|
||||
.#..#.........#.#..##....#.....
|
||||
........##...#................#
|
||||
........#.###.#.###.#.#.##..##.
|
||||
.#....##.....#...##.#..#.#.....
|
||||
..#..#.....###....##.#....#.#.#
|
||||
#......##.##...##..#.........#.
|
||||
#..#..#.....#.....#.........#..
|
||||
#....#.#...###.........#...#...
|
||||
.#.#.....##......#.#......#....
|
||||
..##......##...#.#.#.#.........
|
||||
..#......#.....##.###.#.#..#...
|
||||
....#..#.......#..#..#.....#...
|
||||
.#.#.....#...#..........#......
|
||||
#.#..#...........#.#.##.#...#.#
|
||||
..#.#....###...#...#.....#.#...
|
||||
....##.#.###....####.......#...
|
||||
.....##....#.......#..#..#....#
|
||||
...##..#.#.#.#......#......#...
|
||||
...##...#....#...#......###...#
|
||||
........#..#.#.....#.###.......
|
||||
..#..##.#....#.#.........#...#.
|
||||
.....#.####....#.##.........#..
|
||||
......#...#...#.....#......###.
|
||||
.##.....#....#..#.#....#.....#.
|
||||
...........#...#....##..#...#..
|
||||
.....#....#.....#...##..#...#.#
|
||||
.#...#.........#.......#...#..#
|
||||
...#..#...#........#......#....
|
||||
..#..#####.#.....#.#....#...#.#
|
||||
...#.......#.#....#...##..#..#.
|
||||
####..#.#.###.#.#..............
|
||||
.##........#...#.#....#..#.....
|
||||
..#..............#.#..##...#.##
|
||||
.###.#.....#.#.....##.#......##
|
||||
....###.....#...#...#.#..#.....
|
||||
....###.#.##.......#....#...#..
|
||||
#..#...#......##..#.....#.#...#
|
||||
....#.#.........#..............
|
||||
#.##.##...#..#.#.#.....#...#.##
|
||||
#...#...#......#...........##..
|
||||
#.#.#......#............#.#....
|
||||
.#.#..######...#.#.........#.##
|
||||
..#.#..#...#......#............
|
||||
....#.....#......##..#.....#...
|
||||
.##............#....##..#......
|
||||
.#.#.#...#.##.............###.#
|
||||
#.#...#...#.....#....#.#.#.....
|
||||
........#..#......##.##.#.....#
|
||||
.....#.....#.#####...#....#....
|
||||
.#...#......#.........#.#......
|
||||
...#...#..##.....##....#..#....
|
||||
....#....##..#.........#.......
|
||||
..#........##..#.#........#....
|
||||
...#...##...........#...#....#.
|
||||
.....##.........#..#....#..#.#.
|
||||
#..#....##..#...##.....#..##.#.
|
||||
..#.#.#.#...#...#.....#.#....#.
|
||||
.......#.###...#.#.......#.#...
|
||||
....#..#..#.###.#.....###..#.#.
|
||||
.#..##......#..#..#....#.####..
|
||||
..##...........#...#.........#.
|
||||
......#..#...#..........#......
|
||||
....#..........#......##...#...
|
||||
....#..#.##........#.#...##.#..
|
||||
#.##......#........##.#...#...#
|
||||
#..#....#.....###........##....
|
||||
...........##.....##..#....#.##
|
||||
..#....#..#..#......#.#.....#..
|
||||
#....#.##....#.....##.......#..
|
||||
.#.....#.#..............#.##..#
|
||||
.#..#..#...#...#....#.#.....#..
|
||||
...###...##.#...#..#........#..
|
||||
#...#.##.#.....#.#....#..#.....
|
||||
#.....###.#.......#.#..#.#..##.
|
||||
....#..#..##.......###.#...#...
|
||||
.#...####...............#.....#
|
||||
.#.##.#.....#.....#.#......##.#
|
||||
#...........#.##....###.##....#
|
||||
...............#..........#....
|
||||
.....#..#.##.###.#.............
|
||||
...##.............#.....#.#..#.
|
||||
....#.#...#.#..#..#..#....#....
|
||||
..#.......#..........#...#...#.
|
||||
...............#.#.#...###....#
|
||||
....#...#.##....#..##....#.....
|
||||
........#.#.##.........##.##.##
|
||||
#.....###.......#.#....#..#..##
|
||||
.#..#...#......#.#..##.......#.
|
||||
#.....#.#........#.##..#..#....
|
||||
.###..##.#.......#......###....
|
||||
.#...###.....#.....#....###...#
|
||||
........##.##......#.#....#...#
|
||||
.#....#..#.........#..##...##..
|
||||
.......#.......##.#..#..##.....
|
||||
#..##..##......#.#......#.##...
|
||||
..#..###..#...#....#..#...#....
|
||||
#.............#.####.........##
|
||||
..#..................#...#..#..
|
||||
..#......#........##.......#.#.
|
||||
.#.#.#.#..###.....#....#.#.....
|
||||
...#.##.###.......#....#.......
|
||||
................##...#.....#...
|
||||
..#.###.#...#.####....#..#..#..
|
||||
..#....###....##..#.#.........#
|
||||
.#..#.#.....#........#....##...
|
||||
.....#..#......#..#..##.#.#....
|
||||
.#..#.........##....##......#..
|
||||
.....#.#...#...#.#...#.#...#.#.
|
||||
..#..#...#...#...##.#..###.....
|
||||
..#..##......#..##.#...##......
|
||||
.......#..##....##.#......#..#.
|
||||
..#......#.#.....#.##....##....
|
||||
..#....#......#......##........
|
||||
....##.#.#....#.......#.##.....
|
||||
#.....#...###....#....#...#....
|
||||
............#.#..#...#...#..#..
|
||||
..##.............##....#.......
|
||||
.#.......#.##.#......#....##...
|
||||
...##............#....#..#...#.
|
||||
.##.####.....#.#..###.#....#.##
|
||||
....##.#........#..#...#.......
|
||||
...#...###.##...........##..#..
|
||||
..##..##....#...#..#..........#
|
||||
..#.........#.#...##..........#
|
||||
.......##....#.#...##.....#..#.
|
||||
.............#.....#.#.......#.
|
||||
#.......#..##..##...##.#.......
|
||||
..............#.....#.#..#...##
|
||||
........##..#.....#...#...#.#..
|
||||
###.#.................#........
|
||||
...#........#...#.#######..#..#
|
||||
...#.##...##.#.#..######...#...
|
||||
#.......#..#....#..#.##.....#..
|
||||
#..#....##....#.##.......#....#
|
||||
#...#..#.#.#...#..#.##..#......
|
||||
....#..##....#..#.#...........#
|
||||
.##..#.#.............###.......
|
||||
#....##......#..#..#.....###...
|
||||
..#..........#...###.#.........
|
||||
.####......#....#......#.#....#
|
||||
..#....#.#.#......#....#.......
|
||||
.....#.....#....#....#####....#
|
||||
.##..........#...#.###....#....
|
||||
....##.....##......#...#.#.....
|
||||
.#...#...#..#.#.#...#####......
|
||||
...#.##..####.##.##.......##...
|
||||
............#.......#..........
|
||||
.#..##.#..#####........#..#...#
|
||||
#......##..##..##.........##...
|
||||
....#....#.............#.#....#
|
||||
###..#.....#.....#.#...#..#.###
|
||||
#...#.......##......#....#.#.#.
|
||||
...#......#..#...#....#...###.#
|
||||
....#....##.......#....#......#
|
||||
............#......##.##.....#.
|
||||
...#.........#......#....##..##
|
||||
.....##....##...#..###...#..#..
|
||||
.......##.#..........#.##.##...
|
||||
....##...........#.#..#..#.##.#
|
||||
#...#..##.##.#....#....#.#.....
|
||||
...##.#.....#..#..#..###....##.
|
||||
#.##.#..#..#.#.............#...
|
||||
..#.#.............###.....#....
|
||||
...#..#....#..#.....#.#..#..#..
|
||||
...#.....##.#...........#..##.#
|
||||
.........#.#.##..#..#.#...#....
|
||||
...#..##..#...#...###.##.#..#..
|
||||
.#..##...##......##..##........
|
||||
......##....##.#.##.#.#........
|
||||
...#..................#.....#..
|
||||
.##................#.#..#..###.
|
||||
.##.##.....#................#..
|
||||
.....#.#..........#...#..#.#..#
|
||||
.............#......#..#.#..#..
|
||||
...#...##..#........#....#.....
|
||||
#......#........##.##...##.....
|
||||
##..#..##....#...#............#
|
||||
..##..##.##....##..##........#.
|
||||
...#....#.#.#.#....#.#...##....
|
||||
....#...##..##.#.##...#..#...#.
|
||||
#..#....##.#.....#.......#...##
|
||||
##.#....#.............#..#.....
|
||||
.##..#..#.#.....#.......#.#..#.
|
||||
.......#..#...##...#...###..#..
|
||||
..........#...#.#..##.....#...#
|
||||
..#....#...........#####....#..
|
||||
#....#..#.......##.............
|
||||
.........##..#####.......##....
|
||||
#..#..........#.....###...#..#.
|
||||
.#.#.#..#...#.......##...#####.
|
||||
.....#....#.###...#.......#....
|
||||
#.#.....##...###....###....#...
|
||||
.#.....#..#.#.#........#...#...
|
||||
.##.#.#.#......#....###....#...
|
||||
.#..##..####......###......#...
|
||||
......#.#.#.#.#...#...####.##..
|
||||
.#........##..#.....#....#....#
|
||||
.....###......##..#....#.......
|
||||
#.#.##...#.#......###..........
|
||||
........#.#...#..#......#....#.
|
||||
..##...##.........#.......#.#..
|
||||
..#.##....#...##.....#.###.....
|
||||
.........#..#.#....#....#.#.##.
|
||||
#.........#......#..#.......#..
|
||||
...#...##.......#.........#....
|
||||
............#......#...........
|
||||
##.....#.....#.#...#.....#.....
|
||||
..#.#...#..#...#.#...........#.
|
||||
#.#.#..#..#...##.#...#.#.....#.
|
||||
.#..###.#..##.#.....#.....#....
|
||||
##....##....#.......##..##.....
|
||||
.#..#...........###..........#.
|
||||
.#..#..#..........###..#.......
|
||||
#..###......#............##...#
|
||||
#......#........#..#..#..#.#...
|
||||
.......#.###...#.##............
|
||||
.##....#.......#.#...##.....#.#
|
||||
....#..#.#.......#.#...........
|
||||
##....#.###.#....#.#..##.#....#
|
||||
..#..#..#....#...#........##...
|
||||
...#...##....#..#.#...#..#.....
|
||||
......#..#......#....#.......#.
|
||||
#.#..............#...###...#..#
|
||||
...#....#..#..........#.#...#..
|
||||
#.....##..##.....#........#....
|
||||
.#...##..#.#..............#....
|
||||
##.#....#..##...#..#.####.#..#.
|
||||
.....#.......#.#.#.#..#.....###
|
||||
...#.##....#.#........##.......
|
||||
#...#.#...#.#..###..##.##...#.#
|
||||
###..............#.#.###.......
|
||||
...###..#.#..#....##...###.#...
|
||||
......##...........#...#..#...#
|
||||
.#..#.........##.......#..#...#
|
||||
.#.......###......##...#...#...
|
||||
.#......##...#........#.......#
|
||||
.#..#.....#.........#.#........
|
||||
#...#.#.....#...#..##.........#
|
||||
......##.#......##.#..##.#.....
|
||||
...............#.#..#....#....#
|
||||
#....#..#..#..#.#.....##...##..
|
||||
#.#......#.###......#..#...####
|
||||
.#.#..#...#...#.#..#.##.##.#.#.
|
||||
.....#.#...###...#.#.....##....
|
||||
...#..#.#..........##.#....#.#.
|
||||
...#..#.#.##.....###.##.#....#.
|
||||
..........#..###......#..#.#...
|
||||
###.....#..###..#...#..###.#...
|
||||
..#..#.....##.#.#..###.......#.
|
||||
....#....##........##..........
|
||||
.......#..........#...#......#.
|
||||
.#........#.#.#.#.#.......#....
|
||||
.#..#.......##..##....#.#...#..
|
||||
.#.#.#.......#..#..............
|
||||
#.#....#.#...#.#.#.....#.#...##
|
||||
.....#..........##..#.......#..
|
||||
.##......#.#....#.#.......#....
|
||||
..#.##....#.##.#...#...........
|
||||
...##......##..##.............#
|
||||
..........##.#.#..#..........#.
|
||||
.##....#..#..#.#....##.#...#.#.
|
||||
...........#....#.....#.#..#...
|
||||
.#.....#....##..#.........#....
|
||||
.....#.....#...#....#...#.###.#
|
||||
..#....#....#.....#...#......#.
|
||||
.....##..#.............#...#...
|
||||
........#..#.......#.#.......#.
|
||||
#...###..#.##.#...###...##..##.
|
||||
....##..#.......#...#.#........
|
||||
.#...#.#.##....####........#..#
|
||||
.#...#.#.####.##.#.............
|
||||
#..##...#....#...#.#.#.#.##..#.
|
||||
.#.......#........#.....###....
|
||||
#.#.....#....#..#....#..#....#.
|
||||
...#..#...#.....#.........##...
|
||||
.#....#......###...#....#.#.#..
|
||||
#.#........#......#...#....##..
|
||||
.....#..#......#..#..#......#..
|
||||
.#.....#..#.##.#.#.#...#......#
|
||||
##........#..#.#..#...#.####...
|
||||
..........##....#.#..#.#....#..
|
||||
#.##..#..#....#..#....##..#.#.#
|
||||
..#......#.......#...##..#.....
|
||||
##...#.........#......#......#.
|
||||
.#.....................#..#.##.
|
||||
.#.......#........#.#.#..##.#..
|
||||
..#..........#........#..##.#..
|
||||
.#...#...#.........##.#.#.#....
|
||||
....#....#.###.#....###....#.##
|
||||
....##......##........##.#.##..
|
||||
....#.#......#.##.#...#.##.....
|
||||
....#....#..#.#..###.#.#.......
|
||||
....#......#..#.#.......#..##..
|
||||
.....#..#.#.##.##..##.....#.#..
|
||||
...#....................##.....
|
||||
#.....#...##...#.#.............
|
||||
..#.#...#.#.#.....##..#....#...
|
91
day-3/src/main.rs
Normal file
91
day-3/src/main.rs
Normal file
|
@ -0,0 +1,91 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
let slope = parse_slope(&input)?;
|
||||
|
||||
// Part 1
|
||||
let encounters = count_encounters(&slope, (1, 3));
|
||||
println!("{}", encounters);
|
||||
|
||||
// Part 2
|
||||
let encounters: usize = [(1, 1), (1, 3), (1, 5), (1, 7), (2, 1)]
|
||||
.iter()
|
||||
.map(|angle| count_encounters(&slope, *angle))
|
||||
.product();
|
||||
println!("{}", encounters);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_slope(input: &str) -> Result<Vec<Vec<usize>>, String> {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.chars()
|
||||
.map(|c| match c {
|
||||
'.' => Ok(0),
|
||||
'#' => Ok(1),
|
||||
other => Err(format!("Invalid slope; contains character: {}", other)),
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_encounters(input: &Vec<Vec<usize>>, angle: (usize, usize)) -> usize {
|
||||
input
|
||||
.iter()
|
||||
.enumerate()
|
||||
.step_by(angle.0)
|
||||
.map(|(i, row)| row[(i * angle.1 / angle.0) % row.len()])
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let example = indoc!(
|
||||
"..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
.#...##..#.
|
||||
..#.##.....
|
||||
.#.#.#....#
|
||||
.#........#
|
||||
#.##...#...
|
||||
#...##....#
|
||||
.#..#...#.#"
|
||||
);
|
||||
|
||||
let parsed = parse_slope(example)?;
|
||||
assert_eq!(count_encounters(&parsed, (1, 3)), 7);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let example = indoc!(
|
||||
"..##.......
|
||||
#...#...#..
|
||||
.#....#..#.
|
||||
..#.#...#.#
|
||||
.#...##..#.
|
||||
..#.##.....
|
||||
.#.#.#....#
|
||||
.#........#
|
||||
#.##...#...
|
||||
#...##....#
|
||||
.#..#...#.#"
|
||||
);
|
||||
|
||||
let parsed = parse_slope(example)?;
|
||||
assert_eq!(count_encounters(&parsed, (2, 1)), 2);
|
||||
Ok(())
|
||||
}
|
||||
}
|
1
day-4/.gitignore
vendored
Normal file
1
day-4/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
78
day-4/Cargo.lock
generated
Normal file
78
day-4/Cargo.lock
generated
Normal file
|
@ -0,0 +1,78 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-4"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
10
day-4/Cargo.toml
Normal file
10
day-4/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day-4"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
1068
day-4/input
Normal file
1068
day-4/input
Normal file
File diff suppressed because it is too large
Load diff
122
day-4/src/main.rs
Normal file
122
day-4/src/main.rs
Normal file
|
@ -0,0 +1,122 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
let passports = parse_passports(&input)?;
|
||||
println!("{}", validate_passports(&passports));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_passports(input: &str) -> Result<Vec<HashMap<&str, &str>>, &str> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|passport| {
|
||||
passport
|
||||
.trim()
|
||||
.split(|c| c == ' ' || c == '\n')
|
||||
.map(|field| {
|
||||
let split = field
|
||||
.find(':')
|
||||
.ok_or("Invalid passport entry; no separator")?;
|
||||
let item = field.split_at(split);
|
||||
|
||||
let key = item.0;
|
||||
let value = item
|
||||
.1
|
||||
.strip_prefix(":")
|
||||
.expect("We've already checked the separator exists");
|
||||
|
||||
Ok((key, value))
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn validate_passports(passports: &Vec<HashMap<&str, &str>>) -> usize {
|
||||
let required_fields = ["byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"];
|
||||
let rules = [
|
||||
|year: &str| match year.parse() {
|
||||
Ok(1920..=2002) => true,
|
||||
_ => false,
|
||||
},
|
||||
|year: &str| match year.parse() {
|
||||
Ok(2010..=2020) => true,
|
||||
_ => false,
|
||||
},
|
||||
|year: &str| match year.parse() {
|
||||
Ok(2020..=2030) => true,
|
||||
_ => false,
|
||||
},
|
||||
|height: &str| match height.split_at(height.chars().count() - 2) {
|
||||
(height, "cm") => match height.parse() {
|
||||
Ok(150..=193) => true,
|
||||
_ => false,
|
||||
},
|
||||
(height, "in") => match height.parse() {
|
||||
Ok(59..=76) => true,
|
||||
_ => false,
|
||||
},
|
||||
_ => false,
|
||||
},
|
||||
|color: &str| match color.split_at(1) {
|
||||
("#", code) => code.chars().all(|a| char::is_ascii_hexdigit(&a)),
|
||||
_ => false,
|
||||
},
|
||||
|color: &str| {
|
||||
["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]
|
||||
.iter()
|
||||
.any(|c| c == &color)
|
||||
},
|
||||
|pid: &str| pid.chars().count() == 9 && pid.chars().all(char::is_numeric),
|
||||
];
|
||||
|
||||
passports
|
||||
.iter()
|
||||
.filter(|passport| {
|
||||
required_fields
|
||||
.iter()
|
||||
.zip(rules.iter())
|
||||
.all(|(field, rule)| {
|
||||
if let Some(value) = passport.get(field) {
|
||||
rule(value)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
})
|
||||
.count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), String> {
|
||||
let example = indoc!(
|
||||
"ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
|
||||
byr:1937 iyr:2017 cid:147 hgt:183cm
|
||||
|
||||
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
|
||||
hcl:#cfa07d byr:1929
|
||||
|
||||
hcl:#ae17e1 iyr:2013
|
||||
eyr:2024
|
||||
ecl:brn pid:760753108 byr:1931
|
||||
hgt:179cm
|
||||
|
||||
hcl:#cfa07d eyr:2025 pid:166559648
|
||||
iyr:2011 ecl:brn hgt:59in
|
||||
"
|
||||
);
|
||||
|
||||
let passports = parse_passports(example)?;
|
||||
assert_eq!(validate_passports(&passports), 2);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
1
day-5/.gitignore
vendored
Normal file
1
day-5/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
78
day-5/Cargo.lock
generated
Normal file
78
day-5/Cargo.lock
generated
Normal file
|
@ -0,0 +1,78 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-5"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
10
day-5/Cargo.toml
Normal file
10
day-5/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day-5"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
815
day-5/input
Normal file
815
day-5/input
Normal file
|
@ -0,0 +1,815 @@
|
|||
BFBBFFFLRR
|
||||
FFBFBBBLLL
|
||||
FBFBFBFLLL
|
||||
BBFFFBFLLR
|
||||
FBFFBBFLRR
|
||||
BBFBFFFLLL
|
||||
FFBBFFFRLL
|
||||
BFBFBBBRRR
|
||||
FBFFBFFRLR
|
||||
FBBBBBBLLL
|
||||
BBFBBBBLLL
|
||||
BFFFBBBLLL
|
||||
FFBFFFBRLR
|
||||
BFFFFBFLLR
|
||||
FFBBBFBLRL
|
||||
BFFBFBBRRL
|
||||
FBFBFBBRRR
|
||||
FBFFFBBRRL
|
||||
FBFBBBFRLR
|
||||
BFFFFFBRRL
|
||||
BBFFFBBLRR
|
||||
FFBBBBBRRR
|
||||
FFBBBBBLLL
|
||||
BBFBFFBRLR
|
||||
FBFFBFBRRR
|
||||
FFFBBBBRLL
|
||||
BBFBFFBLLR
|
||||
BFBFFFBRLL
|
||||
FFBFBFFLLR
|
||||
FBFBBFBRLL
|
||||
FBFFBBBLLL
|
||||
FBFBBBFRLL
|
||||
FBFBFBBRLL
|
||||
BFFFFBBRLL
|
||||
FFFBBFFLLR
|
||||
FFBFFFBLRL
|
||||
FFBFBBFLLL
|
||||
BFBBFBBLLL
|
||||
BFFFFBBRRL
|
||||
FFBFFFBRLL
|
||||
BFBBFFFRRR
|
||||
FFBBFFFLRL
|
||||
FFBFBFBRLL
|
||||
BFBFBBBLLL
|
||||
BBFBBFFLRL
|
||||
BFFFFBBLLR
|
||||
FFBFBBFRLR
|
||||
FFBBFBBLLR
|
||||
FBFFBBFLLL
|
||||
BFBFBFBLLL
|
||||
BFFBFBBLLR
|
||||
FBBBBFFRLR
|
||||
FBBBFBFRLL
|
||||
FBBBBFFLRR
|
||||
BFFBBBBLRL
|
||||
FBFFBBFLRL
|
||||
BBFFFBBLLR
|
||||
FBFBFFFRLL
|
||||
FBBBFBBRRL
|
||||
FFBBBFBLLR
|
||||
BFFBBBFLLR
|
||||
BFFFFBFRLR
|
||||
BBFFFFBLLR
|
||||
FBBFFFBRRL
|
||||
BFFFBFBRRR
|
||||
FBFBFFFRRR
|
||||
BBBFFFBLRL
|
||||
BFBFBFBLLR
|
||||
BFBBBBBLRL
|
||||
FFBBFBBRRR
|
||||
FBBFFFBRRR
|
||||
FFBFBFBRRR
|
||||
BBFFFFBRRL
|
||||
BFBBBBBLLL
|
||||
BFBFBFFRLL
|
||||
BFBFBFBRRL
|
||||
BFBFFFFLRL
|
||||
FFBBBBBLLR
|
||||
FBBFBFFRLL
|
||||
FBBFFBBLLL
|
||||
BFFFFFFLRL
|
||||
FFFBBFFLRL
|
||||
FFFBBFFRLL
|
||||
FFBFFFBLLL
|
||||
FBBFBFBLRL
|
||||
BBFFFFBRLR
|
||||
FBBFBBBRLR
|
||||
FFBBFFBLLL
|
||||
FFBFFFBRRR
|
||||
FBFBFBFRRL
|
||||
FFBBBFBRRL
|
||||
BBFFBFFLRR
|
||||
FBFFFFFRRR
|
||||
FBBFFFBRLL
|
||||
FFBBFBBRRL
|
||||
FFBBFFFLLR
|
||||
FBFBBFBLLL
|
||||
BFFFBBFLRR
|
||||
BFBFBFFLRR
|
||||
BFFFFBFRRR
|
||||
BBFBBFFRLL
|
||||
BFBBBBBLRR
|
||||
BFBBBBFLLL
|
||||
BBFBBBBLLR
|
||||
FFBBBFFRRR
|
||||
BBFFBFBLRR
|
||||
FFFBBBFRLR
|
||||
BFBFFFBLRR
|
||||
BFFFBBFLLL
|
||||
FBBBBFFLRL
|
||||
FBBFFFFRRL
|
||||
FFBFFBBRRL
|
||||
BFBBBBBRLR
|
||||
FBBFFBBLRL
|
||||
BBFBFBBRLL
|
||||
FBFFBFBRLR
|
||||
FBBBBFFLLR
|
||||
FBBFBBFRRL
|
||||
FBFBBBBRLL
|
||||
BFFBFBFRLL
|
||||
FFBFBBFRRL
|
||||
FBFFFBFRRL
|
||||
FBFFBBFRRL
|
||||
FFBFBBFRLL
|
||||
BBFBBBBLRL
|
||||
BFFFBBBRRL
|
||||
FFBFFFBLRR
|
||||
FFBBFBBLRL
|
||||
BFBFBBFRRR
|
||||
BFFFFFFRRL
|
||||
FBBBBBBRRR
|
||||
FBFBBFBLRR
|
||||
FBFFFFFRLR
|
||||
BFFBBBFLRR
|
||||
BFBBFFBLLL
|
||||
FBFFFFBLRL
|
||||
FFBBBBBLRL
|
||||
FFFBBBFRLL
|
||||
FFBBFBFRLL
|
||||
FBFBBBFLRL
|
||||
FFBBBFBRLL
|
||||
BBFBBFBLRL
|
||||
FFBFBFFRLR
|
||||
BBFBBBBLRR
|
||||
FFFBBBBRRR
|
||||
FBBBFBFLLL
|
||||
BFFFBFBRLR
|
||||
FBBFBBFRRR
|
||||
FBFFBBBRLR
|
||||
FBFBBFBRLR
|
||||
FBBBFBBLLL
|
||||
BBFBFBFLLL
|
||||
FFBBFBBRLL
|
||||
FBBFBBBLRR
|
||||
BFFFBBFRRL
|
||||
BFBFFBFLRR
|
||||
BBFBBBFLLL
|
||||
BFFFBBFRLR
|
||||
BFBBFBFLRL
|
||||
FBBBBBFRRR
|
||||
FBFFFBBRRR
|
||||
BBFFFFBLRR
|
||||
FBFBFBFLRL
|
||||
BBFFBBBRLL
|
||||
FFBBBFFLRR
|
||||
FFBBBBFLRR
|
||||
FBFBFBBRRL
|
||||
BBFBFFBRRL
|
||||
FBBBFFBLLL
|
||||
BFBFFFFLLR
|
||||
FFBFFBFLRR
|
||||
BFBBFFFLLR
|
||||
BBFBBFFLRR
|
||||
BFFFFFFLLR
|
||||
BBFFBFBRRL
|
||||
FBBBFBBRLL
|
||||
BFBBBFBLLR
|
||||
BFBBFFBRLR
|
||||
FBFBBBFRRR
|
||||
BFBBBBBRRR
|
||||
FFBBFBBRLR
|
||||
FBBFBBFRLR
|
||||
BFFBBFBRLR
|
||||
FBFFFFFLLL
|
||||
FBBFFBFRLL
|
||||
FBBBBFFRRL
|
||||
FBFBBBBLRL
|
||||
FFFBBFBLRR
|
||||
FFBFFBBLRR
|
||||
FFBBBBBLRR
|
||||
FBBFFBFRRR
|
||||
FBBBFBFRRR
|
||||
FBBFBBBLLR
|
||||
BFFFFBFLLL
|
||||
FFBFFFFRRL
|
||||
BFBFFBBLRL
|
||||
FFBFFFFRLR
|
||||
FBFBFBFRLL
|
||||
BBFBFBBRRL
|
||||
FFBFBBFLLR
|
||||
BFBBFFBLLR
|
||||
FBFFBFFLLL
|
||||
BFFBBFBRRR
|
||||
FBFFFBBRLL
|
||||
BBFBBFFRRR
|
||||
FBFBBFBLRL
|
||||
FBFBFBBLRL
|
||||
FBFFBBFRRR
|
||||
FBFFBBBLRL
|
||||
FFBFBBBRRR
|
||||
BFBBBFFLLR
|
||||
FFBBFFFRRL
|
||||
FFBBBBBRRL
|
||||
FFBFFBBRLL
|
||||
FFBBFBFLLR
|
||||
FBBBFBFLLR
|
||||
BFFFBFFRLL
|
||||
FFBBBFBLRR
|
||||
BFFBBBFRRR
|
||||
BFBBFBBRRR
|
||||
BBFBFFFRRR
|
||||
FBBFFFBLRL
|
||||
BBFBBBFLLR
|
||||
BBFFFBBLRL
|
||||
FBBFFFFLRR
|
||||
BFFBFFFRRL
|
||||
FFBFBBBRLL
|
||||
BBFBBFBRLL
|
||||
FBFFFBBLRR
|
||||
BFBFBBFRLL
|
||||
FBFBBBBRRL
|
||||
FFFBBBFLLR
|
||||
FBFFFFBRRR
|
||||
FBBBBBBRLR
|
||||
BBFBFBFRRR
|
||||
BFBFFBFRLL
|
||||
BFBBBFFLLL
|
||||
BFBBBFBLLL
|
||||
FBFFBFBLRL
|
||||
FFBFFFFLLR
|
||||
FBBBBBFLLL
|
||||
BBFFBFBLLR
|
||||
BBFBBBFLRR
|
||||
BFFFFFFRRR
|
||||
BFFBFFBRRR
|
||||
BFFBBFFLLL
|
||||
FFFBBBFLLL
|
||||
BFFBBFFRRR
|
||||
BBFBBBBRRL
|
||||
FBFBBFFLRR
|
||||
BFBFFBBLLR
|
||||
BFBFFBBRRL
|
||||
BFBBFFFLLL
|
||||
BFBFFBBRRR
|
||||
BFBBFBFRRL
|
||||
FBFBFFFLLR
|
||||
BFBFBFFLLR
|
||||
BFBBBBFRRL
|
||||
BFFFBFFLRR
|
||||
FBBFBBBRRL
|
||||
BBFFFBBRLL
|
||||
FFBFBBBLRR
|
||||
FFBBFFBRLL
|
||||
FBBBFBFRRL
|
||||
FFBFFFFLRL
|
||||
BFFFBBBRLL
|
||||
BFBBBFFRRR
|
||||
FFBFFBBRRR
|
||||
BFBFFFBLLR
|
||||
FBFFFBFLLR
|
||||
FBFBBFFRRL
|
||||
FFBFFBBLLL
|
||||
BBFBBBFLRL
|
||||
BFBFBBFRRL
|
||||
BFFFBFFLLL
|
||||
FBBBFFFRLL
|
||||
BBBFFFFLLR
|
||||
BFFBFFBLRL
|
||||
BBFFBBBLLL
|
||||
BFFBFFBLLR
|
||||
BBFFBBBLRL
|
||||
FBBFFBBLLR
|
||||
FFBFBBFRRR
|
||||
FBFBBFBRRR
|
||||
BFBFFBFRLR
|
||||
BFBBBFBRLL
|
||||
FFBBBBFLRL
|
||||
BFFBFBFLLR
|
||||
BFBBFFFRLL
|
||||
BBFBFBBLRR
|
||||
FBBFBFFLLL
|
||||
FBBFFFFLRL
|
||||
FBBBBFBRRL
|
||||
FFBBBFFRLL
|
||||
FBBFBFFRRL
|
||||
FBBBFBFRLR
|
||||
FBBBFFBLRR
|
||||
BBFFBBBLRR
|
||||
FBBFFBFLLL
|
||||
FBFBBFFRLL
|
||||
FFBFBFFRLL
|
||||
BFBFBBBRRL
|
||||
FBFBBBFRRL
|
||||
FFBFBBBLLR
|
||||
BFFBFFFRLR
|
||||
FFBFBFFRRL
|
||||
BBFFFFFLRL
|
||||
FBFFBFFLRL
|
||||
BBFFFBFLLL
|
||||
BFFBBFBLRR
|
||||
BFBBFBFLLR
|
||||
FBFBFBFLRR
|
||||
BBFFBBFRLL
|
||||
BBFBFBFRLL
|
||||
BBFFFBFRLR
|
||||
BFFBFBFRRL
|
||||
BFFBFBBLRL
|
||||
FFFBBBFLRL
|
||||
FBBBFFFRRL
|
||||
FFBFBBFLRR
|
||||
FBBFBFBRLR
|
||||
FFFBBFFRRR
|
||||
FBBBBBBLRL
|
||||
BBFFBBFRRR
|
||||
FFBFFBFLLL
|
||||
BFFFBBFLRL
|
||||
FBFBBBFLRR
|
||||
BBFFBFBLLL
|
||||
FBFFBBFRLR
|
||||
BFFBFBBLLL
|
||||
BFBBFFBLRR
|
||||
FFBBBFFLLR
|
||||
FBFBFFFRRL
|
||||
BFBBFBBRLL
|
||||
BBFBBFBLRR
|
||||
FBBFFFFLLR
|
||||
BFFFBBFRRR
|
||||
BBFFFBFRRR
|
||||
BBFBBFFLLL
|
||||
BBFFBFBRLL
|
||||
BBBFFFFRLR
|
||||
BFFFFBFRLL
|
||||
FFBBBFFRRL
|
||||
BFFBBBFLRL
|
||||
BBFBBBFRLL
|
||||
FBFFFFBRLL
|
||||
BFBFFBFLRL
|
||||
FFBFFBBLRL
|
||||
FBBBBBBLRR
|
||||
BBFFFBFRLL
|
||||
BFBFFBBRLR
|
||||
BBFBFFBLLL
|
||||
BFFFBFFRRR
|
||||
FFBFFBFLRL
|
||||
FBBFBFFLRR
|
||||
BBFBFFBRRR
|
||||
BBFBFFBLRL
|
||||
FBBBBFBLRR
|
||||
BBFFBBBRRR
|
||||
BBFFBFFLLR
|
||||
FBBBFFBRLL
|
||||
BFBFFBBLLL
|
||||
FBBFBBBLLL
|
||||
FFBBFBBLRR
|
||||
FBBBBFFLLL
|
||||
BFBFBBFLRL
|
||||
BFBBBFBRRL
|
||||
BFBBBBFRLL
|
||||
FFBBFFFRLR
|
||||
BBFBBBFRLR
|
||||
BFBBFBBRLR
|
||||
FFFBBBBLLL
|
||||
BFFFFFBLRL
|
||||
FBFBFFBRLL
|
||||
FBBFBBBRLL
|
||||
FBBFBBFRLL
|
||||
BFBFBBFLLR
|
||||
FFBBFBFLRL
|
||||
FBBBFFBRRL
|
||||
FFBFFBFRRR
|
||||
BBFFFFBRLL
|
||||
FBBFFBFRRL
|
||||
BBFFFFFRLR
|
||||
FBFBFFBLRR
|
||||
BFFBBFBRLL
|
||||
BBFFFFFLRR
|
||||
BBFFBBBRRL
|
||||
BBFFBFFRLR
|
||||
FFBBFFBLLR
|
||||
FBBFFFBLRR
|
||||
BFFBBBBRLL
|
||||
FBFBFBBLRR
|
||||
FBBFBFBRLL
|
||||
FBBBFFFRRR
|
||||
FBBFFBBRLL
|
||||
FFBFFFFRLL
|
||||
FFBBBFBLLL
|
||||
FBFFBBBLRR
|
||||
FFBBFFBRRR
|
||||
FFFBBFBRRR
|
||||
FBFBFFBRRR
|
||||
FFBFFBFRLR
|
||||
BFFFFFFLLL
|
||||
FFBFFBFRRL
|
||||
BFBFBFFLLL
|
||||
BFFBBBBRRR
|
||||
BFFBBFBRRL
|
||||
BBFFBBBRLR
|
||||
BBFFBFFRRR
|
||||
BBFBFBFRLR
|
||||
FFBBFBFLRR
|
||||
FBFFBFBLRR
|
||||
BBFBBBBRLL
|
||||
BBFFFFFLLL
|
||||
FBFFBFFLRR
|
||||
BBBFFFBRRL
|
||||
BFFBFFBRRL
|
||||
BFBBBFBRRR
|
||||
FFFBBFBRLL
|
||||
BFFBBFBLLR
|
||||
BFBBBBFRLR
|
||||
BFFFFBFLRL
|
||||
FBBFBFBRRR
|
||||
BFFBFBBLRR
|
||||
BBFBBFBLLL
|
||||
FBFBFFBRLR
|
||||
FFBBBBFRLL
|
||||
BFBFFBBRLL
|
||||
BBFBBBBRLR
|
||||
FFFBBBBLRL
|
||||
FFFBBFBLLR
|
||||
BFBBFBFLRR
|
||||
FFFBBFBLRL
|
||||
FBBBBBFLLR
|
||||
FBFFBFBRLL
|
||||
BFBFBBBRLL
|
||||
BFBBBBFRRR
|
||||
FBBFBFBRRL
|
||||
BBFFBBFRLR
|
||||
FFFBBBBRLR
|
||||
BFBFBBBLRR
|
||||
FBBBBFBRLR
|
||||
BBFBFBBLRL
|
||||
FFBBBFFRLR
|
||||
BFBBBFFLRR
|
||||
FBFBBBBLRR
|
||||
FBBFFFBRLR
|
||||
BFFFFFBLRR
|
||||
BFFBBBBRLR
|
||||
FBBBFFFRLR
|
||||
BBBFFFFLRR
|
||||
BFBFBFBRRR
|
||||
FFBFFBFRLL
|
||||
BBFBFBFLRR
|
||||
BFBBFBBLLR
|
||||
FFBBBBBRLL
|
||||
FFFBBBBLRR
|
||||
FBBFBBFLRL
|
||||
BFFFBFBLLL
|
||||
BFBFFFFRLL
|
||||
FBFFFBFLRL
|
||||
BBFFFFFRRR
|
||||
FBBFBBFLLL
|
||||
FBBFBFFRLR
|
||||
FFFBBFFLRR
|
||||
BFBBFFFRRL
|
||||
FBFFFBBRLR
|
||||
BBFBFFFRLL
|
||||
FBBFFBBRRL
|
||||
BBBFFFFRLL
|
||||
BFBBBBBRLL
|
||||
FFFBBFFRRL
|
||||
FBBFFFBLLR
|
||||
BFFFBFFLLR
|
||||
FFBFFBFLLR
|
||||
FBBFFBBRRR
|
||||
BFFFFFBRLL
|
||||
BFFFBFFRRL
|
||||
BBFBBBFRRR
|
||||
BFFBFBFLRR
|
||||
FBFBBFFRLR
|
||||
BBFFFBFLRR
|
||||
BBFBFBFLLR
|
||||
BFFBBBFLLL
|
||||
FBFBFBBLLR
|
||||
FBBBBBBRLL
|
||||
FBFBFFFLRL
|
||||
FFBBFFBRLR
|
||||
BBFBBFBRRL
|
||||
BBFBBFBRLR
|
||||
BFFBBFFLRR
|
||||
BFFBFFFLRR
|
||||
BFBFBFFRRL
|
||||
BBFBBBBRRR
|
||||
FBFFFFFLLR
|
||||
BBFFBBFLLR
|
||||
BFFFBBBRLR
|
||||
FBBBBFFRLL
|
||||
BBFBBFFRRL
|
||||
BBFFBFFRLL
|
||||
BFBFBFBLRL
|
||||
BFBFFFBLLL
|
||||
BBFBFFFRLR
|
||||
FBBBFFFLRL
|
||||
FFFBBBBLLR
|
||||
FFBFFFFLRR
|
||||
BBFBFBFRRL
|
||||
FBFFBBBRLL
|
||||
FBBBFBBLRL
|
||||
BBBFFFFLLL
|
||||
FFBFBBBRRL
|
||||
FBFBFFBLLR
|
||||
FBFFFFFLRL
|
||||
BFBFBBBLRL
|
||||
BFFBBFFRLR
|
||||
BFBFFBBLRR
|
||||
BFFBFBBRLR
|
||||
FBBFBFBLRR
|
||||
FFBBBFFLRL
|
||||
BBFFBBFLRR
|
||||
FBBBFBBRRR
|
||||
BFFFBBBLLR
|
||||
BFBFBBBRLR
|
||||
BFFBFBFLLL
|
||||
BBBFFFBRRR
|
||||
BBBFFFBRLR
|
||||
FBFBBFFLLL
|
||||
BFFBBFFLLR
|
||||
FFBBFFBLRR
|
||||
FBBBBBFRLR
|
||||
BFBBFFBRRL
|
||||
FBBBFFFLRR
|
||||
FBFFFFFRLL
|
||||
FFBFFFBLLR
|
||||
BFFBFFFRLL
|
||||
BFBFBFBRLL
|
||||
BFFFFBBLRR
|
||||
FBBFFBBLRR
|
||||
BFBBBFBLRL
|
||||
BBFBFBBLLR
|
||||
BFBBBFBLRR
|
||||
FBFFFBFLLL
|
||||
BFBBBBBLLR
|
||||
BFBFFFBRLR
|
||||
BFBFBBFLLL
|
||||
BBBFFFFRRR
|
||||
FBBFFBBRLR
|
||||
BBBFFFFLRL
|
||||
FBBFBBBRRR
|
||||
FFBBFBFRRL
|
||||
BFFFBBBRRR
|
||||
FFBFBFFRRR
|
||||
BBFBFFFLRR
|
||||
FFBFBBBLRL
|
||||
BFBFBFFRLR
|
||||
FFBBFFFRRR
|
||||
FBFFBFBLLL
|
||||
FBFFBFBLLR
|
||||
BBFBBFFLLR
|
||||
BFBBBFFRLR
|
||||
BFFFFBBLLL
|
||||
BFFBFFBRLL
|
||||
FBBFFFFRLL
|
||||
FFBFBFBLRR
|
||||
FBBBBFBLLL
|
||||
BFBFBFBRLR
|
||||
FBBFBBBLRL
|
||||
BFBFFBFRRL
|
||||
FBBFBFBLLL
|
||||
BFFFBFBLRL
|
||||
BBFFBFFLLL
|
||||
BBFFFBFLRL
|
||||
FFFBBBFRRL
|
||||
FBBFFFBLLL
|
||||
BFFFFFBLLR
|
||||
FBBBBFBRLL
|
||||
BFFFFBBRRR
|
||||
BFFFFBBLRL
|
||||
FBBFFBFRLR
|
||||
BBFFFFBRRR
|
||||
FBFFFFBLLL
|
||||
FFBFBFBRLR
|
||||
BFFFBBBLRL
|
||||
BFFFFFFRLL
|
||||
BFBFFBFLLR
|
||||
BBFFFBBRLR
|
||||
FBBBBFBLRL
|
||||
FBBBBFBRRR
|
||||
BFFBFFFLLL
|
||||
BFFBBBFRLL
|
||||
BFBFBBFRLR
|
||||
BBFFBFBRLR
|
||||
FBBBBBFLRR
|
||||
BFBBFBFRLR
|
||||
FFBBFBFRLR
|
||||
BFFFFFBRRR
|
||||
BFFBBFFLRL
|
||||
FBFFBFFRRL
|
||||
BFBBFFFLRL
|
||||
FBFFFBBLRL
|
||||
BFFFBFFRLR
|
||||
BFFFBFBRRL
|
||||
FBBBBBFLRL
|
||||
BBFFBBFLLL
|
||||
FFBBFBBLLL
|
||||
FFBFBFFLLL
|
||||
BBBFFFBLLL
|
||||
BFBFBBBLLR
|
||||
FBBFFBFLRL
|
||||
FBBBBFFRRR
|
||||
BFBBBBFLLR
|
||||
FFBFFFFRRR
|
||||
BBFFBBFLRL
|
||||
FBFBFBBRLR
|
||||
BFBBFFBLRL
|
||||
FBFBFFBLLL
|
||||
BBFFBFFLRL
|
||||
FBFBFFFLRR
|
||||
FFBFBFBLRL
|
||||
BFFBFBBRRR
|
||||
FFBFBBBRLR
|
||||
BFBBFFFRLR
|
||||
BFBFFFFRLR
|
||||
FFFBBFFLLL
|
||||
BFBFFFFLLL
|
||||
FFBFFBBRLR
|
||||
FFBFBFFLRR
|
||||
BFFBBFFRLL
|
||||
BBFFBBBLLR
|
||||
BFBBBBBRRL
|
||||
FBFBBBBRRR
|
||||
FBBFFFFRRR
|
||||
FFBBBFBRLR
|
||||
FFBFBFBLLL
|
||||
BFFBFFFLLR
|
||||
FBFBFBFRRR
|
||||
BFFBBBBLLL
|
||||
BFFFFBBRLR
|
||||
FBFBBFFLRL
|
||||
FFBFBFFLRL
|
||||
FBBBFFBLLR
|
||||
FBBFBFFLRL
|
||||
FFBFBBFLRL
|
||||
BBBFFFFRRL
|
||||
FFBBFBFLLL
|
||||
FFBBBBFRRL
|
||||
BBFFFFBLLL
|
||||
FFBBFBFRRR
|
||||
FBFFFFFLRR
|
||||
BFFBBBBRRL
|
||||
BFBBFBBRRL
|
||||
BBFBFBBRLR
|
||||
FFBFFFFLLL
|
||||
FBBFBFBLLR
|
||||
FBFBFFBLRL
|
||||
BBFBFBBRRR
|
||||
BFFBBFBLRL
|
||||
FFBBBFFLLL
|
||||
FBBBFFFLLL
|
||||
BFFFBFBLLR
|
||||
FFBBFFFLLL
|
||||
BFFBFFBLRR
|
||||
FFBBFFFLRR
|
||||
BFFBFBBRLL
|
||||
FFBFBFBLLR
|
||||
FBBBBBBLLR
|
||||
FBFFFFBRLR
|
||||
BFBBBFFRLL
|
||||
FFBBBBFRLR
|
||||
FFBFFBBLLR
|
||||
BBFFFFFLLR
|
||||
FBFFFBFRLR
|
||||
BFBFFFFRRL
|
||||
FBFFFBBLLL
|
||||
BBFFFBBLLL
|
||||
BFFBFBFLRL
|
||||
BBFFFBFRRL
|
||||
FBFFBBBRRR
|
||||
FBFFFBBLLR
|
||||
FFFBBFBRRL
|
||||
BBFBFFFLRL
|
||||
BFBFFFBRRL
|
||||
BFBBBFBRLR
|
||||
BFFFBBBLRR
|
||||
FBBFBFFLLR
|
||||
FBFBBBBRLR
|
||||
BBFBBBFRRL
|
||||
FFFBBFBRLR
|
||||
BFFBFFFLRL
|
||||
FBBBBBBRRL
|
||||
BFFBFFBLLL
|
||||
BFFBBFFRRL
|
||||
FBBFBBFLLR
|
||||
BFFBBBBLLR
|
||||
BFBBBFFLRL
|
||||
BFBFFBFRRR
|
||||
BBFFFBBRRL
|
||||
BFBFBFFLRL
|
||||
BFBBFFBRLL
|
||||
BFFFFFFLRR
|
||||
FFFBBBFLRR
|
||||
FBBFFFFLLL
|
||||
FFBBFFBRRL
|
||||
BFBFFBFLLL
|
||||
FBFBBBFLLR
|
||||
FFBBBBBRLR
|
||||
BBFBFFBRLL
|
||||
FBFBBFBRRL
|
||||
FBBFFBFLRR
|
||||
BFFFFFBRLR
|
||||
FBFBBFBLLR
|
||||
FBBBFFFLLR
|
||||
BFFFFBFRRL
|
||||
FBFFFFFRRL
|
||||
FBFBFFFRLR
|
||||
FBFBBBFLLL
|
||||
BBFBFFBLRR
|
||||
BFBBBFFRRL
|
||||
BFBBFBFRRR
|
||||
BBFFFFFRLL
|
||||
FBFBFBFRLR
|
||||
FBBBBBFRLL
|
||||
BBFFBFBLRL
|
||||
BBFFBBFRRL
|
||||
BFBFFFFLRR
|
||||
FBFFBBBRRL
|
||||
FFFBBFFRLR
|
||||
BBFFBFFRRL
|
||||
BBFBBFBLLR
|
||||
FBBFFBFLLR
|
||||
FBFFFFBLLR
|
||||
FFBFBFBRRL
|
||||
FBFFFFBLRR
|
||||
BFFBFFFRRR
|
||||
FFFBBBFRRR
|
||||
BFBFBFBLRR
|
||||
FBFBBBBLLR
|
||||
BBFFFFBLRL
|
||||
BFBBFBFLLL
|
||||
FBBBFFBLRL
|
||||
BBFBFFFRRL
|
||||
BBBFFFBRLL
|
||||
FBBFBBFLRR
|
||||
FBFBFFFLLL
|
||||
FBBFFFFRLR
|
||||
BFFBBBFRRL
|
||||
BBFFFBBRRR
|
||||
FBBBFFBRLR
|
||||
FFBFFFBRRL
|
||||
BFFFFFBLLL
|
||||
FBFFFBFRLL
|
||||
BBFFBFBRRR
|
||||
BFFFFBFLRR
|
||||
FFBBBBFLLR
|
||||
BFBFBBFLRR
|
||||
FBFBFBBLLL
|
||||
BFFFBFBLRR
|
||||
BBFBBFBRRR
|
||||
BFBBFBBLRL
|
||||
BBFBFBBLLL
|
||||
FBBBFBFLRL
|
||||
BFFFBBFRLL
|
||||
FBBBBBFRRL
|
||||
BFFBFFBRLR
|
||||
BFFBBBBLRR
|
||||
FFFBBFBLLL
|
||||
BFBBBBFLRL
|
||||
FBBBFBFLRR
|
||||
FBFFFBFLRR
|
||||
BFBFFFBRRR
|
||||
FBFFBFFLLR
|
||||
BFBFBFFRRR
|
||||
BFFBFBFRLR
|
||||
FBFFFFBRRL
|
||||
BFFFBFBRLL
|
||||
FBBBFBBLRR
|
||||
BFBBFFBRRR
|
||||
BFBFFFFRRR
|
||||
BFFBFBFRRR
|
||||
FFBBFFBLRL
|
||||
BFBBBBFLRR
|
||||
FBFFBBBLLR
|
||||
FBFBBFFLLR
|
||||
FBFFFBFRRR
|
||||
FBFBBBBLLL
|
||||
BFBBFBBLRR
|
||||
BFBFFFBLRL
|
||||
BFFBBFBLLL
|
||||
FBFFBBFLLR
|
||||
BBFBFFFLLR
|
||||
BBFFFFFRRL
|
||||
FBFBBFFRRR
|
||||
BFFFBFFLRL
|
||||
FBFBFBFLLR
|
||||
BFFFFFFRLR
|
||||
FFBBBBFRRR
|
||||
BBFBFBFLRL
|
||||
FBBBFFBRRR
|
||||
FBFFBFFRRR
|
||||
FBBBBFBLLR
|
||||
BBBFFFBLLR
|
||||
FBFFBFBRRL
|
||||
FBBBFBBRLR
|
||||
FBFFBBFRLL
|
||||
BBBFFFBLRR
|
||||
BFFFBBFLLR
|
||||
FBFFBFFRLL
|
||||
FFBBBBFLLL
|
||||
FFBBBFBRRR
|
||||
BBFBBFFRLR
|
||||
FBBBFBBLLR
|
||||
FBBFBFFRRR
|
||||
BFBBFBFRLL
|
||||
FFFBBBBRRL
|
||||
FBFBFFBRRL
|
114
day-5/src/main.rs
Normal file
114
day-5/src/main.rs
Normal file
|
@ -0,0 +1,114 @@
|
|||
use std::fs;
|
||||
|
||||
type SeatCode = [char; 10];
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
|
||||
// Part 1
|
||||
let max_id: Result<usize, String> = input
|
||||
.lines()
|
||||
.map(|code| {
|
||||
let (row, col) = find_seat(parse_seatcode(code)?);
|
||||
Ok(row * 8 + col)
|
||||
})
|
||||
.max()
|
||||
.ok_or("No seat codes set")?;
|
||||
|
||||
println!("{}", max_id?);
|
||||
|
||||
// Part 2
|
||||
let mut ids = input
|
||||
.lines()
|
||||
.map(|code| {
|
||||
let (row, col) = find_seat(parse_seatcode(code)?);
|
||||
Ok(row * 8 + col)
|
||||
})
|
||||
.collect::<Result<Vec<usize>, String>>()?;
|
||||
ids.sort();
|
||||
|
||||
let mut previous = ids[0];
|
||||
for id in &ids[1..] {
|
||||
if id - 1 != previous {
|
||||
println!("{}", id);
|
||||
break;
|
||||
} else {
|
||||
previous = *id;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_seatcode(input: &str) -> Result<SeatCode, String> {
|
||||
let mut output = ['0'; 10];
|
||||
|
||||
for (i, c) in input.chars().enumerate() {
|
||||
if i > 10 {
|
||||
Err(format!("Seat code too long: '{}'", input))?;
|
||||
}
|
||||
|
||||
if !['F', 'B', 'L', 'R'].iter().any(|valid| &c != valid) {
|
||||
Err(format!("Invalid character in seat code: '{}'", input))?;
|
||||
}
|
||||
|
||||
output[i] = c;
|
||||
}
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
|
||||
fn find_seat(code: SeatCode) -> (usize, usize) {
|
||||
let midpoint = |(start, end): (usize, usize)| (start + end) / 2;
|
||||
let binary_follow = |range: (usize, usize), direction: &char| match direction {
|
||||
'F' | 'L' => (range.0, midpoint(range)),
|
||||
'B' | 'R' => (midpoint(range) + 1, range.1),
|
||||
_ => unreachable!("We already checked all characters are valid"),
|
||||
};
|
||||
|
||||
let row = code[..7].iter().fold((0, 127), binary_follow).0;
|
||||
let col = code[7..].iter().fold((0, 7), binary_follow).0;
|
||||
|
||||
(row, col)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "FBFBBFFRLR";
|
||||
let seat = find_seat(parse_seatcode(input)?);
|
||||
|
||||
assert_eq!(seat, (44, 5));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "BFFFBBFRRR";
|
||||
let seat = find_seat(parse_seatcode(input)?);
|
||||
|
||||
assert_eq!(seat, (70, 7));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple3() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "FFFBBBFRRR";
|
||||
let seat = find_seat(parse_seatcode(input)?);
|
||||
|
||||
assert_eq!(seat, (14, 7));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple4() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = "BBFFBBFRLL";
|
||||
let seat = find_seat(parse_seatcode(input)?);
|
||||
|
||||
assert_eq!(seat, (102, 4));
|
||||
Ok(())
|
||||
}
|
||||
}
|
1
day-6/.gitignore
vendored
Normal file
1
day-6/.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target/
|
78
day-6/Cargo.lock
generated
Normal file
78
day-6/Cargo.lock
generated
Normal file
|
@ -0,0 +1,78 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
[[package]]
|
||||
name = "day-6"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"indoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "47741a8bc60fb26eb8d6e0238bbb26d8575ff623fdc97b1a2c00c050b9684ed8"
|
||||
dependencies = [
|
||||
"indoc-impl",
|
||||
"proc-macro-hack",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indoc-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce046d161f000fffde5f432a0d034d0341dc152643b2598ed5bfce44c4f3a8f0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unindent",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-hack"
|
||||
version = "0.5.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.53"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8833e20724c24de12bbaba5ad230ea61c3eafb05b881c7c9d3cfe8638b187e68"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
|
||||
|
||||
[[package]]
|
||||
name = "unindent"
|
||||
version = "0.1.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f14ee04d9415b52b3aeab06258a3f07093182b88ba0f9b8d203f211a7a7d41c7"
|
10
day-6/Cargo.toml
Normal file
10
day-6/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "day-6"
|
||||
version = "0.1.0"
|
||||
authors = ["Tristan Daniël Maat <tm@tlater.net>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
indoc = "0.3"
|
2087
day-6/input
Normal file
2087
day-6/input
Normal file
File diff suppressed because it is too large
Load diff
137
day-6/src/main.rs
Normal file
137
day-6/src/main.rs
Normal file
|
@ -0,0 +1,137 @@
|
|||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = fs::read_to_string("input")?;
|
||||
|
||||
// Part 1
|
||||
let groups = parse_groups(&input)?;
|
||||
println!("{}", count_answers(&groups));
|
||||
|
||||
// Part 2
|
||||
let groups = parse_group_individuals(&input)?;
|
||||
println!("{}", count_individual_answers(&groups));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_groups(input: &str) -> Result<Vec<HashSet<char>>, String> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
group
|
||||
.chars()
|
||||
.filter(|c| *c != '\n')
|
||||
.map(|c| {
|
||||
if c.is_alphabetic() {
|
||||
Ok(c)
|
||||
} else {
|
||||
Err(format!("Invalid answer: {}", c))
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_answers(groups: &Vec<HashSet<char>>) -> usize {
|
||||
groups.iter().map(|group| group.iter().count()).sum()
|
||||
}
|
||||
|
||||
fn parse_group_individuals(input: &str) -> Result<Vec<Vec<HashSet<char>>>, String> {
|
||||
input
|
||||
.split("\n\n")
|
||||
.map(|group| {
|
||||
group
|
||||
.lines()
|
||||
.map(|individual| {
|
||||
individual
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_alphabetic() {
|
||||
Ok(c)
|
||||
} else {
|
||||
Err(format!("Invalid answer: {}", c))
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn count_individual_answers(groups: &Vec<Vec<HashSet<char>>>) -> usize {
|
||||
groups
|
||||
.iter()
|
||||
.map(|group| {
|
||||
let mut iter = group.into_iter().cloned();
|
||||
let first = iter.next().expect("Must have at least one element");
|
||||
|
||||
iter.fold(first, |cumulative, entry| {
|
||||
cumulative.intersection(&entry).copied().collect()
|
||||
})
|
||||
.len()
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use indoc::indoc;
|
||||
|
||||
#[test]
|
||||
fn test_simple() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
||||
"
|
||||
);
|
||||
|
||||
let groups = parse_groups(input)?;
|
||||
let counts = count_answers(&groups);
|
||||
assert_eq!(counts, 11);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple2() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let input = indoc!(
|
||||
"abc
|
||||
|
||||
a
|
||||
b
|
||||
c
|
||||
|
||||
ab
|
||||
ac
|
||||
|
||||
a
|
||||
a
|
||||
a
|
||||
a
|
||||
|
||||
b
|
||||
"
|
||||
);
|
||||
|
||||
let groups = parse_group_individuals(input)?;
|
||||
let counts = count_individual_answers(&groups);
|
||||
assert_eq!(counts, 6);
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue