Compare commits

...

1 Commits

Author SHA1 Message Date
77febf6a0c day4 2020-12-04 16:50:04 -08:00
6 changed files with 1526 additions and 163 deletions

1138
input/2020/day4.txt Normal file

File diff suppressed because it is too large Load Diff

39
src/day1.rs Normal file
View File

@ -0,0 +1,39 @@
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day1)]
pub fn d1_input(input: &str) -> Vec<u64> {
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect();
entries.sort();
entries
}
#[aoc(day1, part1)]
pub fn solve_d1_p1(input: &[u64]) -> u64 {
for entry in input {
let needed = 2020 - entry;
if let Ok(idx) = input.binary_search(&needed) {
return entry * input[idx];
}
}
panic!("not found");
}
#[aoc(day1, part2)]
pub fn solve_d1_p2(input: &[u64]) -> Option<u64> {
for i in 0..input.len() - 2 {
let entry1 = input[i];
for j in i + 1..input.len() - 1 {
let entry2 = input[j];
if entry1 + entry2 > 2020 {
break;
}
for k in j + 1..input.len() {
let entry3 = input[k];
if entry1 + entry2 + entry3 == 2020 {
return Some(entry1 * entry2 * entry3);
}
}
}
}
return None;
}

53
src/day2.rs Normal file
View File

@ -0,0 +1,53 @@
use aoc_runner_derive::aoc;
struct PasswdEntry<'a> {
lower_bound: usize,
upper_bound: usize,
policy_char: u8,
passwd: &'a [u8],
}
impl<'a> PasswdEntry<'a> {
fn parse(mut input: &'a str) -> Option<Self> {
let lb_idx = input.find('-')?;
let lower_bound: usize = (&input[..lb_idx]).parse().ok()?;
input = &input[lb_idx + 1..];
let ub_idx = input.find(' ')?;
let upper_bound: usize = (&input[..ub_idx]).parse().ok()?;
input = &input[ub_idx + 1..];
let policy_char = input.as_bytes()[0];
let passwd = &input[3..].as_bytes();
Some(PasswdEntry {
lower_bound,
upper_bound,
policy_char,
passwd,
})
}
}
#[aoc(day2, part1)]
pub fn solve_d2_p1(input: &str) -> usize {
fn line_is_valid(line: &str) -> bool {
let entry = PasswdEntry::parse(line).unwrap();
let count = entry
.passwd
.iter()
.filter(|&&b| b == entry.policy_char)
.count();
(count >= entry.lower_bound) && (count <= entry.upper_bound)
}
input.split('\n').filter(|x| line_is_valid(x)).count()
}
#[aoc(day2, part2)]
pub fn solve_d2_p2(input: &str) -> usize {
fn line_is_valid(line: &str) -> bool {
let entry = PasswdEntry::parse(line).unwrap();
(entry.passwd[entry.lower_bound - 1] == entry.policy_char)
^ (entry.passwd[entry.upper_bound - 1] == entry.policy_char)
}
input.split('\n').filter(|x| line_is_valid(x)).count()
}

101
src/day3.rs Normal file
View File

@ -0,0 +1,101 @@
use std::convert::TryFrom;
use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug)]
pub enum MapSquare {
Open,
Tree,
}
impl TryFrom<u8> for MapSquare {
type Error = u8;
fn try_from(b: u8) -> Result<MapSquare, u8> {
Ok(match b {
b'.' => MapSquare::Open,
b'#' => MapSquare::Tree,
unknown => return Err(unknown),
})
}
}
#[aoc_generator(day3)]
pub fn d3_input(input: &[u8]) -> Vec<Vec<MapSquare>> {
input
.split(|&b| b == b'\n')
.map(|line| {
line.iter()
.copied()
.map(|b| MapSquare::try_from(b).unwrap())
.collect()
})
.collect()
}
#[aoc(day3, part1)]
pub fn solve_d3_p1(input: &[Vec<MapSquare>]) -> usize {
input
.iter()
.skip(1)
.enumerate()
.map(|(steps_taken, grid_line)| {
let x_coord = (steps_taken + 1) * 3;
match grid_line.iter().cycle().nth(x_coord).unwrap() {
MapSquare::Open => 0,
MapSquare::Tree => 1,
}
})
.sum()
}
#[aoc(day3, part2)]
pub fn solve_d3_p2(input: &[Vec<MapSquare>]) -> usize {
#[derive(Debug, Clone, Copy)]
struct Step {
x_step: usize,
y_step: usize,
}
let steps = [
Step {
x_step: 1,
y_step: 1,
},
Step {
x_step: 3,
y_step: 1,
},
Step {
x_step: 5,
y_step: 1,
},
Step {
x_step: 7,
y_step: 1,
},
Step {
x_step: 1,
y_step: 2,
},
];
steps
.iter()
.copied()
.map(|Step { x_step, y_step }| -> usize {
input
.iter()
.skip(y_step)
.step_by(y_step)
.enumerate()
.map(|(num_steps_taken, grid_line)| {
let x_coord = (num_steps_taken + 1) * x_step;
match grid_line.iter().cycle().nth(x_coord).unwrap() {
MapSquare::Open => 0,
MapSquare::Tree => 1,
}
})
.sum()
})
.product()
}

190
src/day4.rs Normal file
View File

@ -0,0 +1,190 @@
use std::str::FromStr;
use aoc_runner_derive::aoc;
#[derive(Debug)]
struct BirthYear(u16);
impl FromStr for BirthYear {
type Err = ();
fn from_str(s: &str) -> Result<BirthYear, ()> {
let year = s.parse().map_err(|_| ())?;
if year < 1920 || year > 2002 {
return Err(());
}
Ok(BirthYear(year))
}
}
#[derive(Debug)]
struct IssueYear(u16);
impl FromStr for IssueYear {
type Err = ();
fn from_str(s: &str) -> Result<IssueYear, ()> {
let year = s.parse().map_err(|_| ())?;
if year < 2010 || year > 2020 {
return Err(());
}
Ok(IssueYear(year))
}
}
#[derive(Debug)]
struct ExpYear(u16);
impl FromStr for ExpYear {
type Err = ();
fn from_str(s: &str) -> Result<ExpYear, ()> {
let year = s.parse().map_err(|_| ())?;
if year < 2020 || year > 2030 {
return Err(());
}
Ok(ExpYear(year))
}
}
#[derive(Debug)]
struct HairColor(u32);
impl FromStr for HairColor {
type Err = ();
fn from_str(s: &str) -> Result<HairColor, ()> {
let hex = s.strip_prefix("#").ok_or(())?;
let hcl = u32::from_str_radix(hex, 16).map_err(|_| ())?;
if hcl > 0xffffff {
return Err(());
}
Ok(HairColor(hcl))
}
}
#[derive(Debug)]
enum Height {
Inches(u8),
Cm(u8),
}
impl FromStr for Height {
type Err = ();
fn from_str(s: &str) -> Result<Height, ()> {
if let Some(cm) = s.strip_suffix("cm") {
let cm: u8 = cm.parse().map_err(|_| ())?;
if cm < 150 || cm > 193 {
return Err(());
}
Ok(Height::Cm(cm))
} else if let Some(inches) = s.strip_suffix("in") {
let inches: u8 = inches.parse().map_err(|_| ())?;
if inches < 59 || inches > 76 {
return Err(());
}
Ok(Height::Inches(inches))
} else {
Err(())
}
}
}
#[derive(Debug)]
enum EyeColor {
Amber,
Blue,
Brown,
Gray,
Green,
Hazel,
Other,
}
impl FromStr for EyeColor {
type Err = ();
fn from_str(s: &str) -> Result<EyeColor, ()> {
Ok(match s {
"amb" => EyeColor::Amber,
"blu" => EyeColor::Blue,
"brn" => EyeColor::Brown,
"gry" => EyeColor::Gray,
"grn" => EyeColor::Green,
"hzl" => EyeColor::Hazel,
"oth" => EyeColor::Other,
_ => return Err(()),
})
}
}
#[derive(Debug)]
struct PassportId(u32);
impl FromStr for PassportId {
type Err = ();
fn from_str(s: &str) -> Result<PassportId, ()> {
if s.len() != 9 {
return Err(());
}
Ok(PassportId(s.parse().map_err(|_| ())?))
}
}
#[derive(Debug)]
pub struct Passport<'a> {
byr: BirthYear,
iyr: IssueYear,
eyr: ExpYear,
hgt: Height,
hcl: HairColor,
ecl: EyeColor,
pid: PassportId,
cid: Option<&'a str>,
}
impl<'a> Passport<'a> {
fn parse(input: &'a str) -> Option<Self> {
fn parse_kv(input: &str) -> Option<(&str, &str)> {
let mut iter = input.split(':');
let k = iter.next()?;
let v = iter.next()?;
if iter.next().is_some() {
return None;
}
Some((k, v))
}
let mut byr = None;
let mut iyr = None;
let mut eyr = None;
let mut hgt = None;
let mut hcl = None;
let mut ecl = None;
let mut pid = None;
let mut cid = None;
for field in input.split_ascii_whitespace() {
match parse_kv(field) {
Some(("byr", value)) => byr = Some(value.parse().ok()?),
Some(("iyr", value)) => iyr = Some(value.parse().ok()?),
Some(("eyr", value)) => eyr = Some(value.parse().ok()?),
Some(("hgt", value)) => hgt = Some(value.parse().ok()?),
Some(("hcl", value)) => hcl = Some(value.parse().ok()?),
Some(("ecl", value)) => ecl = Some(value.parse().ok()?),
Some(("pid", value)) => pid = Some(value.parse().ok()?),
Some(("cid", value)) => cid = Some(value),
_ => return None,
}
}
Some(Passport {
byr: byr?,
iyr: iyr?,
eyr: eyr?,
hgt: hgt?,
hcl: hcl?,
ecl: ecl?,
pid: pid?,
cid,
})
}
}
#[aoc(day4, part2)]
pub fn solve_d4_p2(input: &str) -> usize {
input.split("\n\n").filter_map(Passport::parse).count()
}

View File

@ -1,164 +1,6 @@
use std::convert::TryFrom;
pub mod day1;
pub mod day2;
pub mod day3;
pub mod day4;
use aoc_runner_derive::{aoc, aoc_generator, aoc_lib};
#[aoc_generator(day1)]
pub fn d1_input(input: &str) -> Vec<u64> {
let mut entries: Vec<u64> = input.split('\n').map(|x| x.parse().unwrap()).collect();
entries.sort();
entries
}
#[aoc(day1, part1)]
pub fn solve_d1_p1(input: &[u64]) -> u64 {
for entry in input {
let needed = 2020 - entry;
if let Ok(idx) = input.binary_search(&needed) {
return entry * input[idx];
}
}
panic!("not found");
}
#[aoc(day1, part2)]
pub fn solve_d1_p2(input: &[u64]) -> Option<u64> {
for i in 0..input.len() - 2 {
let entry1 = input[i];
for j in i + 1..input.len() - 1 {
let entry2 = input[j];
if entry1 + entry2 > 2020 {
break;
}
for k in j + 1..input.len() {
let entry3 = input[k];
if entry1 + entry2 + entry3 == 2020 {
return Some(entry1 * entry2 * entry3);
}
}
}
}
return None;
}
struct PasswdEntry<'a> {
lower_bound: usize,
upper_bound: usize,
policy_char: u8,
passwd: &'a [u8],
}
impl<'a> PasswdEntry<'a> {
fn parse(mut input: &'a str) -> Option<Self> {
let lb_idx = input.find('-')?;
let lower_bound: usize = (&input[..lb_idx]).parse().ok()?;
input = &input[lb_idx + 1..];
let ub_idx = input.find(' ')?;
let upper_bound: usize = (&input[..ub_idx]).parse().ok()?;
input = &input[ub_idx + 1..];
let policy_char = input.as_bytes()[0];
let passwd = &input[3..].as_bytes();
Some(PasswdEntry { lower_bound, upper_bound, policy_char, passwd })
}
}
#[aoc(day2, part1)]
pub fn solve_d2_p1(input: &str) -> usize {
fn line_is_valid(line: &str) -> bool {
let entry = PasswdEntry::parse(line).unwrap();
let count = entry.passwd.iter().filter(|&&b| b == entry.policy_char).count();
(count >= entry.lower_bound) && (count <= entry.upper_bound)
}
input.split('\n').filter(|x| line_is_valid(x)).count()
}
#[aoc(day2, part2)]
pub fn solve_d2_p2(input: &str) -> usize {
fn line_is_valid(line: &str) -> bool {
let entry = PasswdEntry::parse(line).unwrap();
(entry.passwd[entry.lower_bound - 1] == entry.policy_char)
^ (entry.passwd[entry.upper_bound - 1] == entry.policy_char)
}
input.split('\n').filter(|x| line_is_valid(x)).count()
}
#[derive(Debug)]
pub enum MapSquare {
Open,
Tree,
}
impl TryFrom<u8> for MapSquare {
type Error = u8;
fn try_from(b: u8) -> Result<MapSquare, u8> {
Ok(match b {
b'.' => MapSquare::Open,
b'#' => MapSquare::Tree,
unknown => return Err(unknown),
})
}
}
#[aoc_generator(day3)]
pub fn d3_input(input: &[u8]) -> Vec<Vec<MapSquare>> {
input
.split(|&b| b == b'\n')
.map(|line| line.iter().copied().map(|b| MapSquare::try_from(b).unwrap()).collect())
.collect()
}
#[aoc(day3, part1)]
pub fn solve_d3_p1(input: &[Vec<MapSquare>]) -> usize {
input
.iter()
.skip(1)
.enumerate()
.map(|(steps_taken, grid_line)| {
let x_coord = (steps_taken + 1) * 3;
match grid_line.iter().cycle().nth(x_coord).unwrap() {
MapSquare::Open => 0,
MapSquare::Tree => 1,
}
})
.sum()
}
#[aoc(day3, part2)]
pub fn solve_d3_p2(input: &[Vec<MapSquare>]) -> usize {
#[derive(Debug, Clone, Copy)]
struct Step {
x_step: usize,
y_step: usize,
}
let steps = [
Step { x_step: 1, y_step: 1 },
Step { x_step: 3, y_step: 1 },
Step { x_step: 5, y_step: 1 },
Step { x_step: 7, y_step: 1 },
Step { x_step: 1, y_step: 2 },
];
steps
.iter()
.copied()
.map(|Step { x_step, y_step }| -> usize {
input
.iter()
.skip(y_step)
.step_by(y_step)
.enumerate()
.map(|(num_steps_taken, grid_line)| {
let x_coord = (num_steps_taken + 1) * x_step;
match grid_line.iter().cycle().nth(x_coord).unwrap() {
MapSquare::Open => 0,
MapSquare::Tree => 1,
}
})
.sum()
})
.product()
}
aoc_lib! { year = 2020 }
aoc_runner_derive::aoc_lib! { year = 2020 }