Compare commits

..

3 Commits

Author SHA1 Message Date
0b0e9bc0f4 d14p2 2024-03-27 20:02:43 -07:00
c7a6c47f0e d14p1 2024-03-27 19:47:40 -07:00
e39887f0c2 d13p2 2024-03-22 20:46:10 -07:00
8 changed files with 475 additions and 24 deletions

View File

@ -1,4 +1,5 @@
use std::env; use std::env;
use std::cmp::Ordering;
use std::fs::File; use std::fs::File;
use std::io::{BufReader, BufRead}; use std::io::{BufReader, BufRead};
@ -17,40 +18,43 @@ fn parse_num(s: &[u8]) -> (u64, usize) {
} }
// Assuming no malformed input // Assuming no malformed input
fn is_in_order(left: &[u8], right: &[u8]) -> bool { fn compare(left: &[u8], right: &[u8]) -> Ordering {
//println!(">> {:?}, {:?} -- {:?}", str::from_utf8(left), str::from_utf8(right));
if left.len() == 0 { if left.len() == 0 {
return true; return Ordering::Less;
} }
if right.len() == 0 { if right.len() == 0 {
return false; return Ordering::Greater;
} }
let (l, r) = (left[0], right[0]); let (l, r) = (left[0], right[0]);
if l == b',' && r == b',' { if l == b',' && r == b',' {
is_in_order(&left[1..], &right[1..]) compare(&left[1..], &right[1..])
} else if l == b'[' && r == b'[' { } else if l == b'[' && r == b'[' {
is_in_order(&left[1..], &right[1..]) compare(&left[1..], &right[1..])
} else if l == b']' && r == b']' { } else if l == b']' && r == b']' {
is_in_order(&left[1..], &right[1..]) compare(&left[1..], &right[1..])
} else if l == b']' { } else if l == b']' {
true Ordering::Less
} else if r == b']' { } else if r == b']' {
false Ordering::Greater
} else if l == b'[' { } else if l == b'[' {
let (num, len) = parse_num(right); let (num, len) = parse_num(right);
is_in_order(&left, &[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat()) compare(
&left,
&[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat())
} else if r == b'[' { } else if r == b'[' {
let (num, len) = parse_num(left); let (num, len) = parse_num(left);
is_in_order(&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(), &right) compare(
&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(),
&right)
} else { } else {
let (lnum, llen) = parse_num(left); let (lnum, llen) = parse_num(left);
let (rnum, rlen) = parse_num(right); let (rnum, rlen) = parse_num(right);
if lnum < rnum { if lnum < rnum {
true Ordering::Less
} else if lnum > rnum { } else if lnum > rnum {
false Ordering::Greater
} else { } else {
is_in_order(&left[llen..], &right[rlen..]) compare(&left[llen..], &right[rlen..])
} }
} }
@ -58,17 +62,20 @@ fn is_in_order(left: &[u8], right: &[u8]) -> bool {
fn main() { fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err"); let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines: Vec<String> = BufReader::new(file).lines().flatten().collect(); let mut lines: Vec<_> = BufReader::new(file).lines().flatten()
// TODO: array_chunks() for lazy iter? .filter(|line| line != "").collect();
let blocks: Vec<_> = lines.chunks(3).collect(); lines.push(String::from("[[2]]"));
lines.push(String::from("[[6]]"));
lines.sort_unstable_by(|s1, s2| compare(s1.as_bytes(), s2.as_bytes()));
let mut sum = 0; let (mut six, mut two): (usize, usize) = (0, 0);
for (i, block) in blocks.iter().enumerate() { for (i, line) in lines.iter().enumerate() {
// TODO: return error here instead of assert? if line == "[[6]]" {
assert!((block.len() == 3 && block[2] == "") || block.len() == 2); six = i + 1;
if is_in_order(block[0].as_bytes(), block[1].as_bytes()) { }
sum += i + 1; if line == "[[2]]" {
two = i + 1;
} }
} }
println!("Answer: {:?}", sum); println!("{:?}", six*two);
} }

74
day13/src/main1.rs Normal file
View File

@ -0,0 +1,74 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
fn parse_num(s: &[u8]) -> (u64, usize) {
let mut num: u64 = 0;
let mut len = 0;
for i in 0..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as u64 - '0' as u64);
len += 1;
} else {
break;
}
}
(num, len)
}
// Assuming no malformed input
fn is_in_order(left: &[u8], right: &[u8]) -> bool {
//println!(">> {:?}, {:?} -- {:?}", str::from_utf8(left), str::from_utf8(right));
if left.len() == 0 {
return true;
}
if right.len() == 0 {
return false;
}
let (l, r) = (left[0], right[0]);
if l == b',' && r == b',' {
is_in_order(&left[1..], &right[1..])
} else if l == b'[' && r == b'[' {
is_in_order(&left[1..], &right[1..])
} else if l == b']' && r == b']' {
is_in_order(&left[1..], &right[1..])
} else if l == b']' {
true
} else if r == b']' {
false
} else if l == b'[' {
let (num, len) = parse_num(right);
is_in_order(&left, &[b"[", num.to_string().as_bytes(), b"]", &right[len..]].concat())
} else if r == b'[' {
let (num, len) = parse_num(left);
is_in_order(&[b"[", num.to_string().as_bytes(), b"]", &left[len..]].concat(), &right)
} else {
let (lnum, llen) = parse_num(left);
let (rnum, rlen) = parse_num(right);
if lnum < rnum {
true
} else if lnum > rnum {
false
} else {
is_in_order(&left[llen..], &right[rlen..])
}
}
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines: Vec<String> = BufReader::new(file).lines().flatten().collect();
// TODO: array_chunks() for lazy iter?
let blocks: Vec<_> = lines.chunks(3).collect();
let mut sum = 0;
for (i, block) in blocks.iter().enumerate() {
// TODO: return error here instead of assert?
assert!((block.len() == 3 && block[2] == "") || block.len() == 2);
if is_in_order(block[0].as_bytes(), block[1].as_bytes()) {
sum += i + 1;
}
}
println!("Answer: {:?}", sum);
}

7
day14/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day14"
version = "0.1.0"

8
day14/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "day14"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

151
day14/input.txt Normal file
View File

@ -0,0 +1,151 @@
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
495,155 -> 499,155
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
500,138 -> 505,138
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
483,160 -> 487,160
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
491,131 -> 502,131 -> 502,130
475,88 -> 475,89 -> 483,89 -> 483,88
483,164 -> 487,164
498,147 -> 503,147
505,147 -> 510,147
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
516,150 -> 521,150
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
496,13 -> 501,13
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
512,147 -> 517,147
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
497,141 -> 502,141
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
480,162 -> 484,162
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
489,155 -> 493,155
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
498,157 -> 502,157
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
491,147 -> 496,147
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
475,88 -> 475,89 -> 483,89 -> 483,88
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
486,162 -> 490,162
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
508,144 -> 513,144
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
477,164 -> 481,164
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
504,141 -> 509,141
489,164 -> 493,164
480,42 -> 485,42
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
486,157 -> 490,157
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
501,144 -> 506,144
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
492,166 -> 496,166
509,150 -> 514,150
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
480,134 -> 480,135 -> 500,135 -> 500,134
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
486,166 -> 490,166
494,144 -> 499,144
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
480,134 -> 480,135 -> 500,135 -> 500,134
481,46 -> 486,46
477,44 -> 482,44
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
491,131 -> 502,131 -> 502,130
492,157 -> 496,157
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
488,150 -> 493,150
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
474,46 -> 479,46
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
460,73 -> 460,70 -> 460,73 -> 462,73 -> 462,69 -> 462,73 -> 464,73 -> 464,65 -> 464,73 -> 466,73 -> 466,65 -> 466,73 -> 468,73 -> 468,64 -> 468,73 -> 470,73 -> 470,68 -> 470,73 -> 472,73 -> 472,68 -> 472,73
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
488,46 -> 493,46
495,150 -> 500,150
474,166 -> 478,166
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
484,44 -> 489,44
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
484,39 -> 484,38 -> 484,39 -> 486,39 -> 486,33 -> 486,39 -> 488,39 -> 488,30 -> 488,39 -> 490,39 -> 490,35 -> 490,39 -> 492,39 -> 492,38 -> 492,39 -> 494,39 -> 494,30 -> 494,39 -> 496,39 -> 496,36 -> 496,39
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
481,92 -> 481,96 -> 476,96 -> 476,99 -> 487,99 -> 487,96 -> 485,96 -> 485,92
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
489,115 -> 489,119 -> 481,119 -> 481,126 -> 494,126 -> 494,119 -> 493,119 -> 493,115
492,153 -> 496,153
502,150 -> 507,150
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
475,88 -> 475,89 -> 483,89 -> 483,88
480,134 -> 480,135 -> 500,135 -> 500,134
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
492,26 -> 492,17 -> 492,26 -> 494,26 -> 494,16 -> 494,26 -> 496,26 -> 496,22 -> 496,26 -> 498,26 -> 498,17 -> 498,26 -> 500,26 -> 500,20 -> 500,26 -> 502,26 -> 502,25 -> 502,26 -> 504,26 -> 504,23 -> 504,26 -> 506,26 -> 506,21 -> 506,26 -> 508,26 -> 508,16 -> 508,26 -> 510,26 -> 510,24 -> 510,26
480,166 -> 484,166
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49
470,76 -> 470,80 -> 466,80 -> 466,84 -> 481,84 -> 481,80 -> 474,80 -> 474,76
485,112 -> 485,102 -> 485,112 -> 487,112 -> 487,111 -> 487,112 -> 489,112 -> 489,105 -> 489,112
470,49 -> 470,53 -> 468,53 -> 468,60 -> 477,60 -> 477,53 -> 476,53 -> 476,49

105
day14/src/main.rs Normal file
View File

@ -0,0 +1,105 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::cmp::{min, max};
fn parse_num(s: &[u8]) -> (usize, usize) {
let mut num: usize = 0;
let mut len = 0;
for i in 0..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as usize - '0' as usize);
len += 1;
} else {
break;
}
}
(num, len)
}
fn print_map(map: &Vec<Vec<char>>) {
for row in map {
let s: String = row.iter().collect();
println!("{}", s);
}
println!("");
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines = BufReader::new(file).lines();
let (mut minx, mut miny, mut maxx, mut maxy) =
(usize::MAX, usize::MAX, 0, 0);
let mut lists: Vec<Vec<(usize, usize)>> = Vec::new();
for line in lines.flatten() {
let l_bytes = line.as_bytes();
let mut cursor = 0;
let mut list: Vec<(usize, usize)> = Vec::new();
while cursor < l_bytes.len() {
let (x, lenx) = parse_num(&l_bytes[cursor..]);
let (y, leny) = parse_num(&l_bytes[cursor + lenx + 1..]);
list.push((x, y));
minx = if x < minx {x} else {minx};
miny = if y < miny {y} else {miny};
maxx = if x > maxx {x} else {maxx};
maxy = if y > maxy {y} else {maxy};
cursor = cursor + lenx + 1 + leny + 4;
}
lists.push(list);
}
// part2 hack here
minx = 0;
maxx = 1000;
maxy = maxy + 2;
lists.push(vec![(minx, maxy), (maxx, maxy)]);
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
let mut map: Vec<Vec<char>> =
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
for list in lists {
let (mut xprev, mut yprev) = &list[0];
for (x, y) in &list[1..] {
if *x == xprev {
for ty in *min(y, &yprev)..=*max(y, &yprev) {
map[ty][*x - minx + 1] = '#';
}
} else if *y == yprev {
for tx in *min(x, &xprev)..=*max(x, &xprev) {
map[*y][tx - minx + 1] = '#';
}
}
xprev = *x;
yprev = *y;
}
}
print_map(&map);
let mut total = 0;
'map: loop {
let (mut rockx, mut rocky) = (500, 0);
'rock: loop {
//println!("{:?}, {:?}", rockx, rocky);
if map[rocky + 1][rockx + 1 - minx] == '.' {
rocky = rocky + 1;
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
rocky = rocky + 1;
rockx = rockx - 1;
} else if map[rocky + 1][rockx + 1 - minx + 1] == '.' {
rocky = rocky + 1;
rockx = rockx + 1;
} else {
map[rocky][rockx - minx + 1] = 'o';
total += 1;
if rockx == 500 && rocky == 0 {
break 'map;
}
//print_map(&map);
break 'rock;
}
}
}
print_map(&map);
println!("{:?}", total);
}

97
day14/src/main1.rs Normal file
View File

@ -0,0 +1,97 @@
use std::env;
use std::fs::File;
use std::io::{BufReader, BufRead};
use std::cmp::{min, max};
fn parse_num(s: &[u8]) -> (usize, usize) {
let mut num: usize = 0;
let mut len = 0;
for i in 0..s.len() {
if s[i].is_ascii_digit() {
num = num*10 + (s[i] as usize - '0' as usize);
len += 1;
} else {
break;
}
}
(num, len)
}
fn print_map(map: &Vec<Vec<char>>) {
for row in map {
let s: String = row.iter().collect();
println!("{}", s);
}
println!("");
}
fn main() {
let file = File::open(&env::args().nth(1).expect("input")).expect("io err");
let lines = BufReader::new(file).lines();
let (mut minx, mut miny, mut maxx, mut maxy) =
(usize::MAX, usize::MAX, 0, 0);
let mut lists: Vec<Vec<(usize, usize)>> = Vec::new();
for line in lines.flatten() {
let l_bytes = line.as_bytes();
let mut cursor = 0;
let mut list: Vec<(usize, usize)> = Vec::new();
while cursor < l_bytes.len() {
let (x, lenx) = parse_num(&l_bytes[cursor..]);
let (y, leny) = parse_num(&l_bytes[cursor + lenx + 1..]);
list.push((x, y));
minx = if x < minx {x} else {minx};
miny = if y < miny {y} else {miny};
maxx = if x > maxx {x} else {maxx};
maxy = if y > maxy {y} else {maxy};
cursor = cursor + lenx + 1 + leny + 4;
}
lists.push(list);
}
println!("{:?} {:?} {:?} {:?}", minx, miny, maxx, maxy);
let mut map: Vec<Vec<char>> =
vec![vec!['.'; maxx - minx + 3]; maxy + 1];
for list in lists {
let (mut xprev, mut yprev) = &list[0];
for (x, y) in &list[1..] {
if *x == xprev {
for ty in *min(y, &yprev)..=*max(y, &yprev) {
map[ty][*x - minx + 1] = '#';
}
} else if *y == yprev {
for tx in *min(x, &xprev)..=*max(x, &xprev) {
map[*y][tx - minx + 1] = '#';
}
}
xprev = *x;
yprev = *y;
}
}
print_map(&map);
let mut total = 0;
'map: loop {
let (mut rockx, mut rocky) = (500, 0);
'rock: loop {
//println!("{:?}, {:?}", rockx, rocky);
if rockx < minx || rockx > maxx || rocky == maxy {
break 'map;
} else if map[rocky + 1][rockx + 1 - minx] == '.' {
rocky = rocky + 1;
} else if map[rocky + 1][rockx + 1 - minx - 1] == '.' {
rocky = rocky + 1;
rockx = rockx - 1;
} else if map[rocky + 1][rockx + 1 - minx + 1] == '.' {
rocky = rocky + 1;
rockx = rockx + 1;
} else {
map[rocky][rockx - minx + 1] = 'o';
total += 1;
//print_map(&map);
break 'rock;
}
}
}
print_map(&map);
println!("{:?}", total);
}

2
day14/test.txt Normal file
View File

@ -0,0 +1,2 @@
498,4 -> 498,6 -> 496,6
503,4 -> 502,4 -> 502,9 -> 494,9