Compare commits

..

No commits in common. "05c89e5f1154c76823863c4ad5d7a19a9defa446" and "0ffbddb7db10561b52efb23740222f80662272ad" have entirely different histories.

4 changed files with 90 additions and 95 deletions

View file

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