Browse Source

Initial commit - better late than never

main
Anthony J. Martinez 7 months ago
commit
8025db3ae0
  1. 3
      .gitmodules
  2. 12
      README.md
  3. 2
      day1/.gitignore
  4. 9
      day1/Cargo.toml
  5. 200
      day1/input_day1
  6. 57
      day1/src/main.rs

3
.gitmodules

@ -0,0 +1,3 @@
[submodule "day1"]
path = day1
url = ./day1/

12
README.md

@ -0,0 +1,12 @@
# Advent of Code - 2020
## Rust
My attempts at the 2020 Advent of Code in Rust. My solutions will almost certainly not be the most
efficient in terms of cycles or memory use. Advice on how a given solution could be improved are
of course welcome. Keep in mind my background is not software, but Mechanical Engineering. While
I can tell you exactly why a particular machining tolerance or weldment should be called out for a
mechanical design, I have no idea what the big-O notation of any particular solution is or could be.
These attempts are in Rust because I find the best way to gain proficiency in anything is to do that
thing a lot.

2
day1/.gitignore

@ -0,0 +1,2 @@
/target
Cargo.lock

9
day1/Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["Anthony J. Martinez <anthony@ajmartinez.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

200
day1/input_day1

@ -0,0 +1,200 @@
1028
1987
1938
1136
1503
1456
1107
1535
1946
1986
855
1587
1632
1548
1384
1894
1092
1876
1914
1974
1662
1608
2004
1464
1557
1485
1267
1582
1307
1903
1102
1578
1421
1184
1290
1786
1295
1930
1131
1802
1685
1735
1498
1052
1688
990
1805
1768
1922
1781
1897
1545
1591
1393
1186
149
1619
1813
1708
1119
1214
1705
1942
1684
1460
1123
1439
1672
1980
1337
1731
1203
1481
2009
1110
1116
1443
1957
1891
1595
1951
1883
1733
1697
1321
1689
1103
1300
1262
1190
1667
1843
1544
1877
1718
1866
1929
1169
1693
1518
1375
1477
1222
1791
1612
1373
1253
1087
1959
1970
1112
1778
1412
1127
1767
1091
1653
1609
1810
1912
1917
935
1499
1878
1452
1935
1937
968
1905
1077
1701
1789
1506
1451
1125
1686
1117
1991
1215
1776
1976
846
1923
1945
1888
1193
1146
1583
1315
1372
1963
1491
1777
1799
1363
1579
1367
1863
1983
1679
1944
1654
1953
1297
530
1502
1738
1934
1185
1998
1764
1856
1207
1181
1494
1676
1900
1057
339
1994
2006
1536
2007
644
1173
1692
1493
1756
1916
1890
1908
1887
1241
1447
1997
1967
1098
1287
1392
1932

57
day1/src/main.rs

@ -0,0 +1,57 @@
use std::env;
use std::fs;
fn load_input(f: &str) -> Result<Vec<u32>, Box<dyn std::error::Error>> {
let data_in = fs::read_to_string(f)?;
Ok(data_in
.lines()
.filter_map(|x| x.parse::<u32>().ok())
.collect::<Vec<u32>>())
}
fn part1(d: &Vec<u32>, tgt: &u32) {
let mut found = false;
for i in d.iter() {
if found { break }
for j in d.iter() {
if i + j == *tgt {
println!("{} + {} = 2020", i, j);
println!("Part 1 Answer: {}", i*j);
found = true;
}
}
}
}
fn part2(d: &Vec<u32>, tgt: &u32) {
let mut found = false;
for i in d.iter() {
if found { break }
for j in d.iter() {
if found { break }
for k in d.iter() {
if i + j + k == *tgt {
println!("{} + {} + {} = 2020", i, j, k);
println!("Part 2 Answer: {}", i*j*k);
found=true;
}
}
}
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
const TARGET: u32 = 2020;
let args: Vec<String> = env::args().collect();
if args.len() == 2 {
let input_path = &args[1];
let mut data = load_input(&input_path)?;
data.sort_unstable();
part1(&data, &TARGET);
part2(&data, &TARGET);
} else {
panic!("Too many arguments")
}
Ok(())
}
Loading…
Cancel
Save