Browse Source

Add '2018/' from commit '4f35fa85150c24c3c606851be3a8cd5efd6f5500'

git-subtree-dir: 2018
git-subtree-mainline: 5ccd921b23
git-subtree-split: 4f35fa8515
main
Nick Thomas 4 months ago
parent
commit
cb0c5fa868
  1. 2
      2018/.gitignore
  2. 30
      2018/01/part1.rs
  3. 45
      2018/01/part2.rs
  4. 48
      2018/02/part1.rs
  5. 33
      2018/02/part2.rs
  6. 104
      2018/03/part1/Cargo.lock
  7. 8
      2018/03/part1/Cargo.toml
  8. 98
      2018/03/part1/src/main.rs
  9. 104
      2018/03/part2/Cargo.lock
  10. 8
      2018/03/part2/Cargo.toml
  11. 104
      2018/03/part2/src/main.rs

2
2018/.gitignore vendored

@ -0,0 +1,2 @@
*/input

30
2018/01/part1.rs

@ -0,0 +1,30 @@
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let file = match File::open("input") {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
Ok(file) => file,
};
let reader = BufReader::new(file);
let lines = reader.lines();
let mut total: i128 = 0;
for result in lines {
let l = result.unwrap();
let offset:i128 = i128::from_str_radix(&l, 10).unwrap();
println!("{} => {}", l, offset);
total = total + offset;
}
println!("{}", total);
Ok(())
}

45
2018/01/part2.rs

@ -0,0 +1,45 @@
use std::collections::HashSet;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::SeekFrom;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut file = match File::open("input") {
// The `description` method of `io::Error` returns a string that describes the error
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
Ok(file) => file,
};
let mut repeat = true;
let mut total: i128 = 0;
let mut seen = HashSet::<i128>::new();
seen.insert(0);
while repeat {
file.seek(SeekFrom::Start(0));
let reader = BufReader::new(&file);
let lines = reader.lines();
for result in lines {
let l = result.unwrap();
let offset:i128 = i128::from_str_radix(&l, 10).unwrap();
total = total + offset;
println!("{} => {}", offset, total);
if seen.contains(&total) {
println!("seen {} before", total);
repeat = false;
break;
}
seen.insert(total);
}
}
println!("{}", total);
Ok(())
}

48
2018/02/part1.rs

@ -0,0 +1,48 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let file = match File::open("input") {
Err(why) => panic!("couldn't open input: {}", Error::description(&why)),
Ok(file) => file,
};
let reader = BufReader::new(file);
let lines = reader.lines();
for result in lines {
let l = result.unwrap();
let mut freq = HashMap::new();
let mut is2 = false;
let mut is3 = false;
for ch in l.chars() {
let count = freq.entry(ch).or_insert(0);
*count += 1;
}
for (_ch, count) in freq {
if count == 2 {
is2 = true;
}
if count == 3 {
is3 = true;
}
}
if is2 {
count2+=1;
}
if is3 {
count3+=1;
}
}
println!("2: {} 3: {} 2*3: {}", count2, count3, count2 * count3);
Ok(())
}

33
2018/02/part2.rs

@ -0,0 +1,33 @@
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
fn main() -> std::io::Result<()> {
let mut f = File::open("input")?;
let mut data = String::new();
f.read_to_string(&mut data)?;
for lineA in data.lines() {
for lineB in data.lines() {
let mut a = lineA.chars();
let mut b = lineB.chars();
let mut diffCount = 0;
let mut pairs = a.zip(b);
for (aChar, bChar) in pairs {
if aChar != bChar {
diffCount += 1;
}
}
if diffCount == 1 {
println!("lineA: {} lineB: {}", lineA, lineB);
}
}
}
Ok(())
}

104
2018/03/part1/Cargo.lock generated

@ -0,0 +1,104 @@
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "part1"
version = "0.1.0"
dependencies = [
"scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scan_fmt"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
"checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

8
2018/03/part1/Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "part1"
version = "0.1.0"
authors = ["Nick Thomas <me@ur.gs>"]
edition = "2018"
[dependencies]
scan_fmt = "0.1.3"

98
2018/03/part1/src/main.rs

@ -0,0 +1,98 @@
#[macro_use] extern crate scan_fmt;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
#[derive(Hash, Eq, PartialEq, Debug)]
struct Point {
x: usize,
y: usize,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Rect {
a: Point,
b: Point,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Claim {
id: u32,
area: Rect,
}
impl Claim {
fn new(from: &str) -> Claim {
let (id,x,y,w,h) = scan_fmt!(
from,
"#{} @ {},{}: {}x{}",
u32, usize, usize, usize, usize
);
return Claim{
id: id.unwrap(),
area: Rect {
a: Point {x: x.unwrap(), y: y.unwrap()},
b: Point {x: x.unwrap()+w.unwrap(), y: y.unwrap()+h.unwrap()},
},
}
}
}
fn main() -> std::io::Result<()> {
let f = File::open("../input")?;
let reader = BufReader::new(f);
let mut claims: Vec<Claim> = Vec::new();
let mut max_x = 0;
let mut max_y = 0;
for line in reader.lines() {
let claim = Claim::new(&line.unwrap());
if claim.area.b.x > max_x {
max_x = claim.area.b.x;
}
if claim.area.b.y > max_y {
max_y = claim.area.b.y;
}
claims.push(claim);
}
println!("{} {}", max_x, max_y);
let mut field: Vec<Vec<usize>> = Vec::new();
for i in 0..max_y {
let mut x_row: Vec<usize> = Vec::new();
for _ in 0..max_x {
x_row.push(0);
}
field.push(x_row);
}
for claim in claims {
for x in claim.area.a.x..claim.area.b.x {
for y in claim.area.a.y..claim.area.b.y {
field[y as usize][x as usize] += 1;
}
}
}
let mut count = 0;
for y in field {
for val in y {
if val > 1 {
count += 1;
}
}
}
println!("{}", count);
Ok(())
}

104
2018/03/part2/Cargo.lock generated

@ -0,0 +1,104 @@
[[package]]
name = "aho-corasick"
version = "0.6.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cfg-if"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "lazy_static"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "libc"
version = "0.2.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)",
"version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "part2"
version = "0.1.0"
dependencies = [
"scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "scan_fmt"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utf8-ranges"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum cfg-if 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "082bb9b28e00d3c9d39cc03e64ce4cea0f1bb9b3fde493f0cbc008472d22bdf4"
"checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1"
"checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311"
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384"
"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7"
"checksum scan_fmt 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8b87497427f9fbe539ee6b9626f5a5e899331fdf1c1d62f14c637a462969db30"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum version_check 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"

8
2018/03/part2/Cargo.toml

@ -0,0 +1,8 @@
[package]
name = "part2"
version = "0.1.0"
authors = ["Nick Thomas <me@ur.gs>"]
edition = "2018"
[dependencies]
scan_fmt = "0.1.3"

104
2018/03/part2/src/main.rs

@ -0,0 +1,104 @@
#[macro_use] extern crate scan_fmt;
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
use std::io::prelude::*;
#[derive(Hash, Eq, PartialEq, Debug)]
struct Point {
x: usize,
y: usize,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Rect {
a: Point,
b: Point,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct Claim {
id: u32,
area: Rect,
}
impl Claim {
fn new(from: &str) -> Claim {
let (id,x,y,w,h) = scan_fmt!(
from,
"#{} @ {},{}: {}x{}",
u32, usize, usize, usize, usize
);
return Claim{
id: id.unwrap(),
area: Rect {
a: Point {x: x.unwrap(), y: y.unwrap()},
b: Point {x: x.unwrap()+w.unwrap(), y: y.unwrap()+h.unwrap()},
},
}
}
}
fn main() -> std::io::Result<()> {
let f = File::open("../input")?;
let reader = BufReader::new(f);
let mut claims: Vec<Claim> = Vec::new();
let mut max_x = 0;
let mut max_y = 0;
for line in reader.lines() {
let claim = Claim::new(&line.unwrap());
if claim.area.b.x > max_x {
max_x = claim.area.b.x;
}
if claim.area.b.y > max_y {
max_y = claim.area.b.y;
}
claims.push(claim);
}
let mut field: Vec<Vec<Vec<u32>>> = Vec::new();
for _ in 0..max_y {
let mut x_row: Vec<Vec<u32>> = Vec::new();
for _ in 0..max_x {
x_row.push(Vec::new());
}
field.push(x_row);
}
for claim in &claims {
for x in claim.area.a.x..claim.area.b.x {
for y in claim.area.a.y..claim.area.b.y {
field[y][x].push(claim.id);
}
}
}
let mut bad = HashSet::new();
for y in field {
for x in y {
if x.len() > 1 {
for id in x {
bad.insert(id);
}
}
}
}
for claim in &claims {
if !bad.contains(&claim.id) {
println!("{} is good", claim.id)
}
}
Ok(())
}
Loading…
Cancel
Save