1
0
Fork 0

Added Day 4 Part 1

This commit is contained in:
Ishan Jain 2020-12-04 11:08:59 +05:30
parent be2a7fcef3
commit 7ca65d40dd
No known key found for this signature in database
GPG Key ID: F261A0E73038D89D
3 changed files with 1179 additions and 377 deletions

1458
input

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,13 @@
..##.......
#...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#
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

View File

@ -1,58 +1,46 @@
use std::collections::HashMap;
use std::io::{stdin, Read};
#[derive(Debug, PartialEq, Eq)]
enum GridType {
Open,
Tree,
}
fn main() {
let mut input = String::new();
stdin().read_to_string(&mut input).unwrap();
let grid = input
.split('\n')
let passports: usize = input
.split("\n\n")
.filter(|x| !x.is_empty())
.into_iter()
.map(|x| x.replace('\n', " "))
.map(|x| {
x.as_bytes()
.into_iter()
.map(|y| match y {
b'#' => GridType::Tree,
b'.' => GridType::Open,
_ => unreachable!(),
let mut map = HashMap::new();
x.split(' ').filter(|x| !x.is_empty()).for_each(|y| {
let mut y = y.split(':');
let key = y.next().unwrap();
let value = y.next().unwrap();
map.insert(key.to_owned(), value.to_owned());
});
map
})
.collect::<Vec<GridType>>()
.filter(|map| {
validate(&map, "byr")
&& validate(&map, "iyr")
&& validate(&map, "eyr")
&& validate(&map, "hgt")
&& validate(&map, "hcl")
&& validate(&map, "ecl")
&& validate(&map, "pid")
})
.collect::<Vec<Vec<GridType>>>();
.count();
let m = grid.len();
let n = grid[0].len();
println!("m = {} n = {}", m, n);
let a = calc_trees(&grid, m, n, 3, 1);
let b = calc_trees(&grid, m, n, 1, 1);
let c = calc_trees(&grid, m, n, 5, 1);
let d = calc_trees(&grid, m, n, 7, 1);
let e = calc_trees(&grid, m, n, 1, 2);
println!("{} {} {} {} {}", a, b, c, d, e);
println!("{}", a * b * c * d * e);
println!("{:?}", passports);
}
fn calc_trees(grid: &[Vec<GridType>], m: usize, n: usize, slope_x: usize, slope_y: usize) -> u32 {
let mut sx = 0;
let mut sy = 0;
let mut treecount = if grid[0][0] == GridType::Open { 0 } else { 1 };
while sx < m - 1 {
sy = (sy + slope_x) % n;
sx += slope_y;
if grid[sx][sy] == GridType::Tree {
treecount += 1;
fn validate(map: &HashMap<String, String>, key: &str) -> bool {
if let Some(v) = map.get(key) {
!v.is_empty()
} else {
false
}
}
return treecount;
}