Compare commits

...

2 Commits

Author SHA1 Message Date
Daniel S. 05c89e5f11 cargo fmt 2020-12-06 03:20:11 +01:00
Daniel S. 561a1453df Add length check for Day 4, hcl value 2020-12-06 03:20:03 +01:00
4 changed files with 95 additions and 90 deletions

View File

@ -1,6 +1,5 @@
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day1)] #[aoc_generator(day1)]
pub fn input_generator(input: &str) -> Vec<usize> { pub fn input_generator(input: &str) -> Vec<usize> {
input input

View File

@ -54,10 +54,10 @@ pub fn input_generator(input: &str) -> Vec<Rule> {
#[aoc(day2, part1)] #[aoc(day2, part1)]
pub fn solve_part1(input: &Vec<Rule>) -> usize { pub fn solve_part1(input: &Vec<Rule>) -> usize {
return input.iter().filter(|v| v.is_valid_part1()).count() return input.iter().filter(|v| v.is_valid_part1()).count();
} }
#[aoc(day2, part2)] #[aoc(day2, part2)]
pub fn solve_part2(input: &Vec<Rule>) -> usize { pub fn solve_part2(input: &Vec<Rule>) -> usize {
return input.iter().filter(|v| v.is_valid_part2()).count() return input.iter().filter(|v| v.is_valid_part2()).count();
} }

View File

@ -76,6 +76,9 @@ impl Creds {
} }
fn validate_hcl(val: &str) -> bool { fn validate_hcl(val: &str) -> bool {
if val.len() != 7 {
return false;
}
let mut ch = val.chars(); let mut ch = val.chars();
if ch.next() != Some('#') { if ch.next() != Some('#') {
return false; return false;

View File

@ -1,87 +1,90 @@
use aoc_runner_derive::{aoc, aoc_generator}; use aoc_runner_derive::{aoc, aoc_generator};
#[derive(Debug,Eq,PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Direction { pub enum Direction {
Front, Front,
Back, Back,
Left, Left,
Right Right,
} }
#[aoc_generator(day5)] #[aoc_generator(day5)]
pub fn input_generator(input: &str) -> Vec<Vec<Direction>> { pub fn input_generator(input: &str) -> Vec<Vec<Direction>> {
input.lines().map(|line| { input
line.chars().map(|c| { .lines()
match c { .map(|line| {
'L' => Direction::Left, line.chars()
'R' => Direction::Right, .map(|c| match c {
'F' => Direction::Front, 'L' => Direction::Left,
'B' => Direction::Back, 'R' => Direction::Right,
other => panic!("Invalid direction: {}",other) 'F' => Direction::Front,
} 'B' => Direction::Back,
}).collect() other => panic!("Invalid direction: {}", other),
}).collect() })
} .collect()
})
pub fn seat_to_pos(seat: &Vec<Direction>) -> (usize,usize) { .collect()
let mut col_range: (usize, usize) = (0,8); }
let mut row_range: (usize, usize) = (0,128);
for inst in seat { pub fn seat_to_pos(seat: &Vec<Direction>) -> (usize, usize) {
let delta_col = (col_range.1-col_range.0)/2; let mut col_range: (usize, usize) = (0, 8);
let delta_row = (row_range.1-row_range.0)/2; let mut row_range: (usize, usize) = (0, 128);
match inst { for inst in seat {
Direction::Back => { let delta_col = (col_range.1 - col_range.0) / 2;
row_range.0+=delta_row; let delta_row = (row_range.1 - row_range.0) / 2;
}, match inst {
Direction::Front => { Direction::Back => {
row_range.1-=delta_row; row_range.0 += delta_row;
}, }
Direction::Left => { Direction::Front => {
col_range.1-=delta_col; row_range.1 -= delta_row;
}, }
Direction::Right => { Direction::Left => {
col_range.0+=delta_col; col_range.1 -= delta_col;
}, }
} Direction::Right => {
} col_range.0 += delta_col;
if row_range.1-row_range.0 != 1 { }
panic!("Invalid!"); }
} }
if col_range.1-col_range.0 != 1 { if row_range.1 - row_range.0 != 1 {
panic!("Invalid!"); panic!("Invalid!");
} }
return (col_range.0,row_range.0); if col_range.1 - col_range.0 != 1 {
} panic!("Invalid!");
}
#[aoc(day5, part1)] return (col_range.0, row_range.0);
pub fn solve_part1(input: &Vec<Vec<Direction>>) -> usize { }
let mut max_seat_id: usize = 0;
for seat in input { #[aoc(day5, part1)]
let pos = seat_to_pos(seat); pub fn solve_part1(input: &Vec<Vec<Direction>>) -> usize {
let seat_id = pos.0+pos.1*8; let mut max_seat_id: usize = 0;
max_seat_id=std::cmp::max(max_seat_id,seat_id); for seat in input {
} let pos = seat_to_pos(seat);
return max_seat_id; let seat_id = pos.0 + pos.1 * 8;
} max_seat_id = std::cmp::max(max_seat_id, seat_id);
}
#[aoc(day5, part2)] return max_seat_id;
pub fn solve_part2(input: &Vec<Vec<Direction>>) -> usize { }
let mut seats = [[false;8];128];
for seat in input { #[aoc(day5, part2)]
let pos = seat_to_pos(seat); pub fn solve_part2(input: &Vec<Vec<Direction>>) -> usize {
seats[pos.1][pos.0]=true; let mut seats = [[false; 8]; 128];
} for seat in input {
for id in 0..(8*128) { let pos = seat_to_pos(seat);
let mut id=id; seats[pos.1][pos.0] = true;
let mut v=(false,false,false); }
v.0=seats[id/8][id%8]; for id in 0..(8 * 128) {
id+=1; let mut id = id;
v.1=seats[id/8][id%8]; let mut v = (false, false, false);
id+=1; v.0 = seats[id / 8][id % 8];
v.2=seats[id/8][id%8]; id += 1;
if v==(true,false,true) { v.1 = seats[id / 8][id % 8];
return id-1; id += 1;
} v.2 = seats[id / 8][id % 8];
} if v == (true, false, true) {
panic!("Something went wrong!"); return id - 1;
} }
}
panic!("Something went wrong!");
}