name = "advent_of_code_2021"
version = "0.1.0"
authors = ["Greg Burri <greg.burri@gmail.com>"]
-edition = "2018"
+edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
"forward" => Movement::Forward(distance),
"down" => Movement::Down(distance),
"up" => Movement::Up(distance),
- _ => Movement::Forward(0)
+ unknown_command => panic!("Unknown command: {}", unknown_command),
}
})
.collect()
--- /dev/null
+use std::iter::Iterator;
+
+type Diagnostic = Vec<Vec<char>>;
+
+pub fn parse(diagnostic: &str) -> Diagnostic {
+ diagnostic
+ .split('\n')
+ .map(|line| { line.trim().chars().collect() })
+ .collect()
+}
+
+pub fn gamma_and_epsilon_rates(diagnostic: &Diagnostic) -> (u16, u16) {
+ let l = diagnostic[0].len();
+ dbg!(l);
+ let mut gamma = 0u16;
+ for i in 0..l {
+ if diagnostic.iter().fold(0, |n, current| { n + if current[i] == '1' { 1 } else { 0 } }) > diagnostic.len() / 2 {
+ gamma |= 1;
+ }
+
+ if i < l - 1 {
+ gamma <<= 1;
+ }
+ }
+
+ (gamma, !gamma & 0xFFFFu16 >> (16 - l))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn part1() {
+ let diagnostic =
+ parse(
+ "00100
+ 11110
+ 10110
+ 10111
+ 10101
+ 01111
+ 00111
+ 11100
+ 10000
+ 11001
+ 00010
+ 01010"
+ );
+ let (gamma, epsilon) = gamma_and_epsilon_rates(&diagnostic);
+ assert_eq!(gamma, 22);
+ assert_eq!(epsilon, 9);
+ }
+}
\ No newline at end of file
mod common;
mod day01;
mod day02;
+mod day03;
fn day01() -> String {
let report = common::read_list_of_numbers("data/day01.input", "\n");
format!("part1: {}, part2: {}", final_position.horizontal * final_position.aim, final_position.horizontal * final_position.depth)
}
+fn day03() -> String {
+ let diagnostic = day03::parse(&fs::read_to_string("data/day03.input").unwrap());
+ let (gamma, epsilon) = day03::gamma_and_epsilon_rates(&diagnostic);
+ format!("part1: {}, part2: {}", gamma as u32 * epsilon as u32, "")
+}
+
fn format_micros(t: u128) -> String {
if t < 10_000 {
format!("{} μs", t)
let days: Vec<fn() -> String> = vec!(
day01,
day02,
+ day03,
);
let args: Vec<String> = env::args().skip(1).collect();