Rust Day 1

Signed-off-by: Tobias Berger <tobi.berger13@gmail.com>
This commit is contained in:
Tobias Berger 2020-12-01 20:37:05 +00:00 committed by Tobias Berger
parent c266d7c8ce
commit 91e29e57f1
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
5 changed files with 289 additions and 0 deletions

7
Rust/.gitignore vendored Normal file
View file

@ -0,0 +1,7 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/
# These are backup files generated by rustfmt
**/*.rs.bk

5
Rust/day1/Cargo.lock generated Normal file
View file

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

13
Rust/day1/Cargo.toml Normal file
View file

@ -0,0 +1,13 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["GitHub <noreply@github.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[[bin]]
name = "day1"
path = "main.rs"

200
Rust/day1/input Normal file
View file

@ -0,0 +1,200 @@
1822
1917
1642
1617
1941
1740
1529
1896
1880
568
1897
1521
1832
1936
611
1475
1950
1895
1532
1721
1498
1905
1770
1845
2003
1854
1705
1916
1913
1956
1798
1823
1955
1713
1942
1710
1696
1590
1966
1476
1800
1672
1533
1524
1957
1923
1545
534
1707
1760
1104
1471
1947
1802
1525
1931
1653
1608
1937
1977
1598
1470
1794
1488
1786
1652
1482
1603
1667
1245
1478
667
1948
1885
547
1971
1795
1910
1571
1711
1727
1987
1597
1586
1661
1893
1873
1827
1561
2006
1782
1813
2000
1592
1714
1849
1501
1809
1751
1935
1692
1697
1878
1502
1738
1731
1682
1690
1499
1641
1925
1996
1972
1886
1836
1747
1841
1668
715
1698
1859
1637
1477
1785
1695
1702
1944
1631
1771
1623
1892
1466
1834
1899
201
1801
1978
1830
1591
1673
1949
1846
1677
1657
1576
1817
1851
1894
1754
1604
1568
1730
1985
1614
1980
1554
1997
1960
1983
1848
1883
1968
1729
1716
628
1472
1676
1943
1821
1681
1619
1644
842
1492
1633
1921
775
1861
1584
1974
585
1898
1560
1708
1927
1563
1872
1876
1865
1535
1994
1756
1662
1621
1993
1825
1679
1959
1691
1875

64
Rust/day1/main.rs Normal file
View file

@ -0,0 +1,64 @@
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;
fn parse_input<B: BufRead>(buf: B) -> Vec<u64> {
buf.lines()
.map(|line| {
line.expect("Failed to read line.")
.parse()
.expect("Failed to parse line input.")
})
.collect()
}
fn puzzle_a(nums: &[u64]) -> Option<u64> {
for a in nums {
for b in nums {
if a + b == 2020 {
return Some(a * b);
}
}
}
None
}
fn puzzle_b(nums: &[u64]) -> Option<u64> {
for a in nums {
for b in nums {
for c in nums {
if a + b + c == 2020 {
return Some(a * b * c);
}
}
}
}
None
}
fn open_file<P>(filename: P) -> io::Result<io::BufReader<File>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file))
}
fn main() -> Result<(), &'static str> {
let buffer = open_file("input").unwrap();
let puzzle_input = parse_input(buffer);
println!(
"Part A:\n{}",
puzzle_a(&puzzle_input).expect("No solution for A found.")
);
println!("--------");
println!(
"Part B:\n{}",
puzzle_b(&puzzle_input).expect("No solution for B found.")
);
Ok(())
}