more progress.
over 40 stars!
This commit is contained in:
@@ -4,8 +4,6 @@ fn main() {
|
||||
let binding = read_data("2020_01_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut numbers = vec![];
|
||||
let mut left = 0;
|
||||
let mut right = 0;
|
||||
|
||||
for line in lines {
|
||||
numbers.push(line.parse::<u32>().unwrap());
|
||||
@@ -21,4 +19,4 @@ fn main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// 972576
|
||||
// 972576
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
fn line_to_vec(input: &str) -> Vec<bool> {
|
||||
input.chars()
|
||||
.map(|x| {
|
||||
match x { '.' => { false }, '#' => { true }, _ => { unreachable!("Invalid input -> [{x}]")} }
|
||||
}).collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let input = "..##.......\n#...#...#..\n.#....#..#.\n..#.#...#.#\n.#...##..#.\n..#.##.....\n.#.#.#....#\n.#........#\n#.##...#...\n#...##....#\n.#..#...#.#";
|
||||
|
||||
let binding = read_data("2020_03_data.txt");
|
||||
let input = binding.lines();
|
||||
|
||||
let mut world = vec![];
|
||||
|
||||
for line in input {
|
||||
world.push(line_to_vec(line));
|
||||
println!("[[{line}]] became [[{:?}]]", line_to_vec(line));
|
||||
}
|
||||
|
||||
let mut current_x = 0;
|
||||
let mut current_y = 0;
|
||||
let mut num_hits = 0;
|
||||
|
||||
for current_row in world {
|
||||
println!("Starting at {current_x}x{current_y}");
|
||||
|
||||
// <wherever i am horizontally> % <known data>
|
||||
let offset = current_x % current_row.len();
|
||||
if current_row[offset] {
|
||||
println!("hit a tree");
|
||||
num_hits += 1;
|
||||
} else {
|
||||
println!("missed the tree");
|
||||
}
|
||||
|
||||
// finally move for the next row
|
||||
current_x += 3;
|
||||
current_y += 1;
|
||||
}
|
||||
println!("Finished at {current_x}x{current_y} with {num_hits} hits.");
|
||||
}
|
||||
// 280
|
||||
@@ -0,0 +1,50 @@
|
||||
use core::read_data;
|
||||
|
||||
fn line_to_vec(input: &str) -> Vec<bool> {
|
||||
input.chars()
|
||||
.map(|x| {
|
||||
match x { '.' => { false }, '#' => { true }, _ => { unreachable!("Invalid input -> [{x}]")} }
|
||||
}).collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
fn count_hits(world: &Vec<Vec<bool>>, slope: (u32, u32)) -> u32 {
|
||||
let mut num_hits = 0;
|
||||
let mut current_x = 0;
|
||||
let mut current_y = 0;
|
||||
|
||||
for current_row in world {
|
||||
let offset = current_x as usize % current_row.len();
|
||||
if current_row[offset] {
|
||||
num_hits += 1;
|
||||
}
|
||||
|
||||
current_x += slope.0;
|
||||
current_y += slope.1;
|
||||
}
|
||||
|
||||
num_hits
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2020_03_data.txt");
|
||||
let input = binding.lines();
|
||||
let mut world = vec![];
|
||||
|
||||
for line in input {
|
||||
world.push(line_to_vec(line));
|
||||
}
|
||||
|
||||
let params = vec![
|
||||
(1,1), (3,1), (5,1), (7,1), (1,2)
|
||||
];
|
||||
|
||||
let mut running_product = 1u64;
|
||||
|
||||
for (x_slope, y_slope) in params {
|
||||
let num_hits = count_hits(&world, (x_slope, y_slope));
|
||||
println!("Counted {} hits for {}x{}", num_hits, x_slope, y_slope);
|
||||
running_product = running_product * num_hits as u64;
|
||||
}
|
||||
println!("Ended with {running_product}");
|
||||
}
|
||||
// 9582212640 to high
|
||||
@@ -0,0 +1,78 @@
|
||||
use core::read_data;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Passport {
|
||||
pub byr: i32,
|
||||
pub iyr: i32,
|
||||
pub eyr: i32,
|
||||
pub hgt: i32,
|
||||
pub hcl: String,
|
||||
pub ecl: String,
|
||||
pub pid: String,
|
||||
pub cid: String
|
||||
}
|
||||
|
||||
impl Passport {
|
||||
pub fn valid(&self) -> bool {
|
||||
self.byr > 0 &&
|
||||
!self.hcl.is_empty() &&
|
||||
!self.pid.is_empty() &&
|
||||
!self.ecl.is_empty() &&
|
||||
self.iyr > 0 &&
|
||||
self.eyr > 0 &&
|
||||
self.hgt > 0
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Passport {
|
||||
fn default() -> Self {
|
||||
Passport {
|
||||
byr: -1,
|
||||
iyr: -1,
|
||||
eyr: -1,
|
||||
hgt: -1,
|
||||
hcl: "".to_string(),
|
||||
ecl: "".to_string(),
|
||||
pid: "".to_string(),
|
||||
cid: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_record(input: &str) -> Passport {
|
||||
let mut working = Passport::default();
|
||||
|
||||
let parts: Vec<_> = input.split_whitespace().collect();
|
||||
for part in parts {
|
||||
let (key, value) = part.split_once(':').unwrap();
|
||||
match key {
|
||||
"pid" => working.pid = value.parse().unwrap(),
|
||||
"hgt" => working.hgt = value.parse().unwrap_or(1),
|
||||
"byr" => working.byr = value.parse().unwrap(),
|
||||
"hcl" => working.hcl = value.to_string(),
|
||||
"ecl" => working.ecl = value.to_string(),
|
||||
"iyr" => working.iyr = value.parse().unwrap(),
|
||||
"eyr" => working.eyr = value.parse().unwrap(),
|
||||
"cid" => working.cid = value.parse().unwrap(),
|
||||
_ => { unreachable!("Unrecognized key -> [[{key}]] VALUE -> [[{value}]]"); }
|
||||
}
|
||||
}
|
||||
|
||||
working
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2020_04_data.txt");
|
||||
let lines: Vec<_> = binding.split("\n\n").collect();
|
||||
let mut num_valid = 0;
|
||||
|
||||
// println!("Records:");
|
||||
for line in lines {
|
||||
let parsed = parse_record(line);
|
||||
if parsed.valid() { num_valid += 1; }
|
||||
// println!("[[{parsed:?}]]");
|
||||
}
|
||||
println!("Found {num_valid} valid passports.");
|
||||
}
|
||||
// 151 is too low
|
||||
// 254
|
||||
@@ -0,0 +1,123 @@
|
||||
use core::read_data;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Passport {
|
||||
pub byr: i32,
|
||||
pub iyr: i32,
|
||||
pub eyr: i32,
|
||||
pub hgt: i32,
|
||||
pub hcl: String,
|
||||
pub ecl: String,
|
||||
pub pid: String,
|
||||
pub cid: String
|
||||
}
|
||||
|
||||
impl Default for Passport {
|
||||
fn default() -> Self {
|
||||
Passport {
|
||||
byr: -1,
|
||||
iyr: -1,
|
||||
eyr: -1,
|
||||
hgt: -1,
|
||||
hcl: "".to_string(),
|
||||
ecl: "".to_string(),
|
||||
pid: "".to_string(),
|
||||
cid: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Passport {
|
||||
fn byr_valid(&self) -> bool {
|
||||
self.byr <= 2002 && self.byr >= 1920
|
||||
}
|
||||
fn iyr_valid(&self) -> bool {
|
||||
self.iyr <= 2020 && self.iyr >= 2010
|
||||
}
|
||||
|
||||
pub fn valid(&self) -> bool {
|
||||
self.byr_valid() &&
|
||||
!self.hcl.is_empty() &&
|
||||
!self.pid.is_empty() &&
|
||||
!self.ecl.is_empty() &&
|
||||
self.iyr_valid() &&
|
||||
self.eyr > 0 &&
|
||||
self.hgt > 0
|
||||
}
|
||||
|
||||
pub fn from(input: &str) -> Self {
|
||||
let mut working = Passport::default();
|
||||
|
||||
let parts: Vec<_> = input.split_whitespace().collect();
|
||||
for part in parts {
|
||||
let (key, value) = part.split_once(':').unwrap();
|
||||
match key {
|
||||
"ecl" => { Passport::parse_ecl(&mut working, value) }
|
||||
"cid" => { /* ignored */ }
|
||||
"pid" => { Passport::parse_pid(&mut working, value) }
|
||||
_ => { unreachable!("Not handled -> [[{key}]] value -> [[{value}]]"); }
|
||||
}
|
||||
}
|
||||
|
||||
working
|
||||
}
|
||||
|
||||
fn parse_ecl( passport: &mut Passport, input: &str) {
|
||||
passport.ecl = input.to_string()
|
||||
}
|
||||
|
||||
fn ecl_valid(&self) -> bool {
|
||||
self.ecl == "amb" ||
|
||||
self.ecl == "blu" ||
|
||||
self.ecl == "brn" ||
|
||||
self.ecl == "gry" ||
|
||||
self.ecl == "grn" ||
|
||||
self.ecl == "hzl" ||
|
||||
self.ecl == "oth"
|
||||
}
|
||||
|
||||
fn parse_pid( passport: &mut Passport, input: &str) {
|
||||
passport.pid = input.to_string();
|
||||
}
|
||||
|
||||
fn pid_valid(&self) -> bool {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_record(input: &str) -> Passport {
|
||||
let mut working = Passport::default();
|
||||
|
||||
let parts: Vec<_> = input.split_whitespace().collect();
|
||||
for part in parts {
|
||||
let (key, value) = part.split_once(':').unwrap();
|
||||
match key {
|
||||
"pid" => working.pid = value.parse().unwrap(),
|
||||
"hgt" => working.hgt = value.parse().unwrap_or(1),
|
||||
"byr" => working.byr = value.parse().unwrap(),
|
||||
"hcl" => working.hcl = value.to_string(),
|
||||
"ecl" => working.ecl = value.to_string(),
|
||||
"iyr" => working.iyr = value.parse().unwrap(),
|
||||
"eyr" => working.eyr = value.parse().unwrap(),
|
||||
"cid" => working.cid = value.parse().unwrap(),
|
||||
_ => { unreachable!("Unrecognized key -> [[{key}]] VALUE -> [[{value}]]"); }
|
||||
}
|
||||
}
|
||||
|
||||
working
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2020_04_data.txt");
|
||||
let lines: Vec<_> = binding.split("\n\n").collect();
|
||||
let mut num_valid = 0;
|
||||
|
||||
// println!("Records:");
|
||||
for line in lines {
|
||||
let parsed = parse_record(line);
|
||||
if parsed.valid() { num_valid += 1; }
|
||||
// println!("[[{parsed:?}]]");
|
||||
}
|
||||
println!("Found {num_valid} valid passports.");
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
use core::read_data;
|
||||
|
||||
fn main() {
|
||||
let binding = read_data("2020_01_data.txt");
|
||||
let lines = binding.lines();
|
||||
let mut numbers = vec![];
|
||||
|
||||
for line in lines {
|
||||
numbers.push(line.parse::<u32>().unwrap());
|
||||
}
|
||||
|
||||
for first in numbers.clone() {
|
||||
for second in numbers.clone() {
|
||||
for third in numbers.clone() {
|
||||
if first + second + third == 2020 {
|
||||
println!("{} {} {}", first, second, third);
|
||||
println!("{}", first * second * third);
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// 199300880
|
||||
Reference in New Issue
Block a user