MWTAS2BIIR4LYXIVGASOR52XQXKCUCTQNB4MO4WSSTBI4BSG7HKAC
S5HFKJ5P5YDRR6VIMLZJSX34BJJUPVQSRFFMA22WU2NZTTAZM2EQC
RSO3CMWGWGW73HBTR5LZJR5N4KFWDE7WVHN4RAFDQNOYMHSS2KTAC
W6AJ2K3UVW6IX3H42I6C2VYM2QCK2YV2EMNVP73F426LAEOD2AWAC
6S7OD5A7AFBHTY55VATYR74O3TGMIOR7Y4ITUI6RKGLDMUWY2JNQC
CV3UDKDLBEI6DRKMGGUB6RPVTAURPVVTYUAUQXEHEFN5LG3A6FJQC
OCUJJOPFOAAVUXKRNYTFNNTOYL2TZR2QG2CYSC57GGK2V6UL5IIQC
AUANWDXPFRRFSWULJTVTWGSYY3EYIWHBSQQT7XU3UCP54UVR3ZBQC
7C23M57WZAP2YL5VXIGWQ3Z7MT2PTEUG7ADBZWJ7WHIJXOZ2BCNQC
B527MN663VOX7PY6BCLAAU62WDPTI4JTVLNUP2UR74S4KJYKT4HQC
Some(w) => if w == "no" {
match words.next() {
Some("other") => Ok(()),
_ => Err(RuleParseError::Other),
}?;
match words.next() {
Some("bags.") => Ok(()),
_ => Err(RuleParseError::Other),
}?;
return Ok(Rule {outer: outer, inner: inner});
} else {
w.parse().or_else(|e| Err(RuleParseError::Numeric(e)))
},
Some(w) => {
if w == "no" {
match words.next() {
Some("other") => Ok(()),
_ => Err(RuleParseError::Other),
}?;
match words.next() {
Some("bags.") => Ok(()),
_ => Err(RuleParseError::Other),
}?;
return Ok(Rule {
outer: outer,
inner: inner,
});
} else {
w.parse().or_else(|e| Err(RuleParseError::Numeric(e)))
}
}
let mut counted: HashMap<String,usize> = HashMap::new();
let tree: HashMap<_,_> = rules.iter().map(|rule| (rule.outer.clone(), rule.inner.clone())).collect();
let mut counted: HashMap<String, usize> = HashMap::new();
let tree: HashMap<_, _> = rules
.iter()
.map(|rule| (rule.outer.clone(), rule.inner.clone()))
.collect();
None => if tree
.get(&c)
.iter()
.flat_map(|v| v.iter()).all(|(r,_)|
counted.contains_key(r)
) {
counted.insert(c.clone(), tree
.get(&c)
.iter()
.flat_map(|v| v.iter())
.map(|(r,n)|
(1 + counted
.get(r)
.map(|x| *x)
.unwrap_or(0)
) * usize::from(*n)
).sum()
None => {
if tree
.get(&c)
.iter()
.flat_map(|v| v.iter())
.all(|(r, _)| counted.contains_key(r))
{
counted.insert(
c.clone(),
tree.get(&c)
.iter()
.flat_map(|v| v.iter())
.map(|(r, n)| {
(1 + counted.get(r).map(|x| *x).unwrap_or(0)) * usize::from(*n)
})
.sum(),
println!("{}", match args[2].as_ref() {
"1" => count_colors_containing(rules, vec![String::from("shiny gold")]),
"2" => count_bags_in(rules, String::from("shiny gold")),
_ => panic!("No such problem"),
});
println!(
"{}",
match args[2].as_ref() {
"1" => count_colors_containing(rules, vec![String::from("shiny gold")]),
"2" => count_bags_in(rules, String::from("shiny gold")),
_ => panic!("No such problem"),
}
);
std::io::BufReader::new(std::fs::File::open(&args[1]).unwrap()).lines()
.map(Result::unwrap)
).map(match args[2].as_ref() {
"any" => any_checked,
"all" => all_checked,
_ => panic!("No such aggregator"),
})
.map(|c| c.len())
.sum();
std::io::BufReader::new(std::fs::File::open(&args[1]).unwrap())
.lines()
.map(Result::unwrap),
)
.map(match args[2].as_ref() {
"any" => any_checked,
"all" => all_checked,
_ => panic!("No such aggregator"),
})
.map(|c| c.len())
.sum();
println!("{}", match args[2].as_ref() {
"1" => max_seat(&mut seats),
"2" => my_seat(&mut seats),
_ => panic!("unknown problem"),
});
println!(
"{}",
match args[2].as_ref() {
"1" => max_seat(&mut seats),
"2" => my_seat(&mut seats),
_ => panic!("unknown problem"),
}
);
const REQUIRED: &[(&str, fn(String) -> bool)] =
&[
("byr", check_byr),
("iyr", check_iyr),
("eyr", check_eyr),
("hgt", check_hgt),
("hcl", check_hcl),
("ecl", check_ecl),
("pid", check_pid)
];
const REQUIRED: &[(&str, fn(String) -> bool)] = &[
("byr", check_byr),
("iyr", check_iyr),
("eyr", check_eyr),
("hgt", check_hgt),
("hcl", check_hcl),
("ecl", check_ecl),
("pid", check_pid),
];
impl<I: Iterator<Item=HashMap<Q,R>>, Q: Clone + std::cmp::Eq + std::hash::Hash, R: Clone> Iterator for DelimByEmpty<I> {
type Item = HashMap<Q,R>;
impl<I: Iterator<Item = HashMap<Q, R>>, Q: Clone + std::cmp::Eq + std::hash::Hash, R: Clone>
Iterator for DelimByEmpty<I>
{
type Item = HashMap<Q, R>;
.map(extract_keys)
).map(|d| validate_line(d, version))
.map(|t| match t {
Document::Invalid => 0,
_ => 1,
})
.sum();
println!("{:}",result);
.map(extract_keys),
)
.map(|d| validate_line(d, version))
.map(|t| match t {
Document::Invalid => 0,
_ => 1,
})
.sum();
println!("{:}", result);
Direction::C(c,d) => match c {
Cardinal::N => { self.waypoint_y -= d as i32; },
Cardinal::S => { self.waypoint_y += d as i32; },
Cardinal::E => { self.waypoint_x += d as i32; },
Cardinal::W => { self.waypoint_x -= d as i32; },
Direction::C(c, d) => match c {
Cardinal::N => {
self.waypoint_y -= d as i32;
}
Cardinal::S => {
self.waypoint_y += d as i32;
}
Cardinal::E => {
self.waypoint_x += d as i32;
}
Cardinal::W => {
self.waypoint_x -= d as i32;
}
Direction::C(c,d) => match c {
Cardinal::N => { self.y -= d as i32; },
Cardinal::S => { self.y += d as i32; },
Cardinal::E => { self.x += d as i32; },
Cardinal::W => { self.x -= d as i32; },
Direction::C(c, d) => match c {
Cardinal::N => {
self.y -= d as i32;
}
Cardinal::S => {
self.y += d as i32;
}
Cardinal::E => {
self.x += d as i32;
}
Cardinal::W => {
self.x -= d as i32;
}
fn neighbours(xc: usize, yc: usize) -> impl Iterator<Item=(isize,isize)> {
(-1 .. 2).flat_map(|y| (-1 .. 2).map(move |x| (x,y))).filter(|p| *p != (0,0))
.map(move |(x,y)| (x + xc as isize, y + yc as isize))
fn neighbours(xc: usize, yc: usize) -> impl Iterator<Item = (isize, isize)> {
(-1..2)
.flat_map(|y| (-1..2).map(move |x| (x, y)))
.filter(|p| *p != (0, 0))
.map(move |(x, y)| (x + xc as isize, y + yc as isize))
fn build_neighbourhoods_v1(grid: &Vec<Vec<LobbyPoint>>) -> Vec<Vec<Vec<(usize,usize)>>> {
grid.into_iter().enumerate().map(|(y,row)|
row.into_iter().enumerate().map(move |(x,lp)|
match lp {
LobbyPoint::Floor => vec![],
_ => neighbours(x,y).flat_map(|(xn,yn)|
usize::try_from(yn).ok().and_then(|yn|
grid.get(yn).and_then(|row|
usize::try_from(xn).ok().and_then(|xn|
row.get(xn).and_then(|lp|
match lp {
LobbyPoint::Floor => None,
_ => Some((xn,yn)),
}
)
)
)
).into_iter()
).collect(),
}
).collect()
).collect()
fn build_neighbourhoods_v1(grid: &Vec<Vec<LobbyPoint>>) -> Vec<Vec<Vec<(usize, usize)>>> {
grid.into_iter()
.enumerate()
.map(|(y, row)| {
row.into_iter()
.enumerate()
.map(move |(x, lp)| match lp {
LobbyPoint::Floor => vec![],
_ => neighbours(x, y)
.flat_map(|(xn, yn)| {
usize::try_from(yn)
.ok()
.and_then(|yn| {
grid.get(yn).and_then(|row| {
usize::try_from(xn).ok().and_then(|xn| {
row.get(xn).and_then(|lp| match lp {
LobbyPoint::Floor => None,
_ => Some((xn, yn)),
})
})
})
})
.into_iter()
})
.collect(),
})
.collect()
})
.collect()
grid.get(y).and_then(|row| row.get(x)).and_then(|lp| match lp {
LobbyPoint::Floor => seek_line(x, y, xd, yd, grid),
_ => Some((x,y))
})
grid.get(y)
.and_then(|row| row.get(x))
.and_then(|lp| match lp {
LobbyPoint::Floor => seek_line(x, y, xd, yd, grid),
_ => Some((x, y)),
})
fn neighbours_v2<'a>(xc: usize, yc: usize, grid: &'a Vec<Vec<LobbyPoint>>) -> impl Iterator<Item=(usize,usize)> + 'a {
(-1 .. 2).flat_map(move |y| (-1 .. 2).map(move |x| (x,y)))
.filter(|p| *p != (0,0))
.flat_map(move |(x,y)| seek_line(xc,yc,x,y,grid).into_iter())
fn neighbours_v2<'a>(
xc: usize,
yc: usize,
grid: &'a Vec<Vec<LobbyPoint>>,
) -> impl Iterator<Item = (usize, usize)> + 'a {
(-1..2)
.flat_map(move |y| (-1..2).map(move |x| (x, y)))
.filter(|p| *p != (0, 0))
.flat_map(move |(x, y)| seek_line(xc, yc, x, y, grid).into_iter())
fn build_neighbourhoods_v2(grid: &Vec<Vec<LobbyPoint>>) -> Vec<Vec<Vec<(usize,usize)>>> {
grid.into_iter().enumerate().map(|(y,row)|
row.into_iter().enumerate().map(move |(x,lp)|
match lp {
LobbyPoint::Floor => vec![],
_ => neighbours_v2(x,y,grid).collect(),
}
).collect()
).collect()
fn build_neighbourhoods_v2(grid: &Vec<Vec<LobbyPoint>>) -> Vec<Vec<Vec<(usize, usize)>>> {
grid.into_iter()
.enumerate()
.map(|(y, row)| {
row.into_iter()
.enumerate()
.map(move |(x, lp)| match lp {
LobbyPoint::Floor => vec![],
_ => neighbours_v2(x, y, grid).collect(),
})
.collect()
})
.collect()
fn time_step(grid: &Vec<Vec<LobbyPoint>>, neighbourhoods: &Vec<Vec<Vec<(usize,usize)>>>, threshold: u16) -> Vec<Vec<LobbyPoint>> {
grid.into_iter().zip(neighbourhoods.into_iter()).map(|(gr,nr)|
gr.into_iter().zip(nr.into_iter()).map(|(lp,n)| match lp {
LobbyPoint::Floor => LobbyPoint::Floor,
_ => {
let c: u16 = n.into_iter().map(|(x,y)| match grid[*y][*x] {
LobbyPoint::Full => 1,
_ => 0,
}).sum();
match lp {
LobbyPoint::Full => if c >= threshold {
LobbyPoint::Empty
} else {
LobbyPoint::Full
},
LobbyPoint::Empty => if c == 0 {
LobbyPoint::Full
} else {
LobbyPoint::Empty
},
_ => LobbyPoint::Floor,
}
},
}).collect()
).collect()
fn time_step(
grid: &Vec<Vec<LobbyPoint>>,
neighbourhoods: &Vec<Vec<Vec<(usize, usize)>>>,
threshold: u16,
) -> Vec<Vec<LobbyPoint>> {
grid.into_iter()
.zip(neighbourhoods.into_iter())
.map(|(gr, nr)| {
gr.into_iter()
.zip(nr.into_iter())
.map(|(lp, n)| match lp {
LobbyPoint::Floor => LobbyPoint::Floor,
_ => {
let c: u16 = n
.into_iter()
.map(|(x, y)| match grid[*y][*x] {
LobbyPoint::Full => 1,
_ => 0,
})
.sum();
match lp {
LobbyPoint::Full => {
if c >= threshold {
LobbyPoint::Empty
} else {
LobbyPoint::Full
}
}
LobbyPoint::Empty => {
if c == 0 {
LobbyPoint::Full
} else {
LobbyPoint::Empty
}
}
_ => LobbyPoint::Floor,
}
}
})
.collect()
})
.collect()
"1" => (build_neighbourhoods_v1(&grid),4),
"2" => (build_neighbourhoods_v2(&grid),5),
_ => panic!("Version number (1 or 2) expected")
"1" => (build_neighbourhoods_v1(&grid), 4),
"2" => (build_neighbourhoods_v2(&grid), 5),
_ => panic!("Version number (1 or 2) expected"),
};
println!("{}", grid.into_iter().flat_map(|row| row.into_iter().map(|pt| match pt {
LobbyPoint::Full => 1,
_ => 0,
})).sum::<usize>())
}
println!(
"{}",
grid.into_iter()
.flat_map(|row| row.into_iter().map(|pt| match pt {
LobbyPoint::Full => 1,
_ => 0,
}))
.sum::<usize>()
)