Adding the first 3 days and readme.

master
Dustin Swan 4 years ago
parent 8a64323207
commit 1dfbb13f25

@ -0,0 +1,3 @@
# AOC 2020
Doing it in OCaml because Jay is a masochist

@ -0,0 +1,29 @@
let get_one_line file =
try Some (input_line file) with
End_of_file -> None
let get_lines file =
let rec input lines =
match get_one_line file with
Some line -> input (line :: lines)
| None -> List.rev lines
in input []
let find_pair_and_prod items total =
let len = List.length items in
let rec check idx =
let left = List.nth items (idx mod len) in
let right = List.nth items (idx / len) in
if left + right == total
then left * right
else check (idx+1)
in check 0
let file = "day1_1.txt"
let channel = open_in(file)
let lines = get_lines channel
let ints = List.map int_of_string lines
let prod = find_pair_and_prod ints 2020;;
Printf.printf "%i" prod

@ -0,0 +1,200 @@
1749
1897
881
1736
1161
1720
1676
305
264
1904
1880
1173
483
1978
1428
1635
1386
1858
1602
1916
1906
1212
1730
1777
1698
1845
1812
1922
1729
1803
1761
1901
1748
1188
1964
1935
1919
1810
1567
1849
1417
1452
54
1722
1784
1261
1744
1594
1526
1771
1762
1894
1717
1716
51
1955
1143
1741
1999
1775
1944
1983
1962
1198
1553
1835
1867
1662
1461
1811
1764
1726
1927
1179
1468
1948
1813
1213
1905
1371
1751
1215
1392
1798
1823
1815
1923
1942
1987
1887
1838
1395
2007
1479
1752
1945
1621
1538
1937
565
1969
1493
1291
1438
1578
1770
2005
1703
1712
1943
2003
1499
1903
1760
1950
1990
1185
1809
1337
1358
1743
1707
1671
1788
1785
1972
1863
1690
1512
1963
1825
1460
1828
1902
1874
1755
1951
1830
1767
1787
1373
1709
1514
1807
1791
1724
1859
1590
1976
1572
1947
1913
1995
1728
1624
1731
1706
1782
1994
1851
1843
1773
1982
1685
2001
1346
1200
1746
1520
972
1834
1909
2008
1733
1960
1280
1879
1203
1979
1133
1647
1282
1684
860
1444
1780
1989
1795
1819
1797
1842
1796
1457
1839
1853
1711
1883
1146
1734
1389

@ -0,0 +1,8 @@
main = do
contents <- readFile "day1_1.txt"
let xs = map read $ lines contents
let [(i, j, k)] = [(i, j, k) | i <- [0..(length xs)-1],
j <- [0..i],
k <- [0..j],
(xs !! i) + (xs !! j) + (xs !! k) == 2020]
print $ (xs !! i) * (xs !! j) * (xs !! k)

@ -0,0 +1,30 @@
let get_one_line file =
try Some (input_line file) with
End_of_file -> None
let get_lines file =
let rec input lines =
match get_one_line file with
Some line -> input (line :: lines)
| None -> List.rev lines
in input []
let find_pair_and_prod items total =
let len = List.length items in
let rec check idx =
let one = List.nth items (idx mod len) in
let two = List.nth items ((idx / len) mod len) in
let three = List.nth items (idx / (len * len)) in
if one + two + three == total
then one * two * three
else check (idx + 1)
in check 0
let file = "day1_1.txt"
let channel = open_in(file)
let lines = get_lines channel
let ints = List.map int_of_string lines
let prod = find_pair_and_prod ints 202000;;
Printf.printf "%i" prod

@ -0,0 +1,50 @@
let get_one_line file =
try Some (input_line file) with
End_of_file -> None
let get_lines file =
let rec input lines =
match get_one_line file with
Some line -> input (line :: lines)
| None -> List.rev lines
in input []
(* --- *)
type password_set = { min : int; max : int; chr : char; pw : string };;
let line_to_set line = (* convert a line from the file into a password_set *)
let [rng; chr; pw] = String.split_on_char ' ' line in (* split on spaces *)
let chr = String.get chr 0 in (* strip colon and convert to char *)
let [min; max] = List.map int_of_string (String.split_on_char '-' rng) in (* pull out the min and max from the range, map to Int *)
{ min; max; chr; pw }
(* stolen from SO.. *)
let explode s = (* convert a string to a list of chars *)
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) [];;
let char_count el str =
List.length (List.filter (fun c -> c == el) (explode str))
let is_valid_day1_password set =
let count = char_count set.chr set.pw in
(*Printf.printf "%s %c count: %i min: %i, max: %i, in range: %b\n" set.pw set.chr count set.min set.max (count >= set.min && count <= set.max);*)
count >= set.min && count <= set.max
let is_valid_day2_password set =
let is_valid = ((String.get set.pw (set.min-1)) == set.chr) <> ((String.get set.pw (set.max-1)) == set.chr) in
(*Printf.printf "%s %c min: %i, max: %i, char-at-min: %c, char-at-max: %c, valid: %b\n" set.pw set.chr set.min set.max (String.get set.pw (set.min-1)) (String.get set.pw (set.max-1)) is_valid;*)
is_valid
let file = "day2.txt"
let channel = open_in(file)
let lines = get_lines channel
let sets = List.map line_to_set lines
let valid_day1_passwords = List.filter is_valid_day1_password sets;;
Printf.printf "valid day1 password count: %i\n" (List.length valid_day1_passwords)
let valid_day2_passwords = List.filter is_valid_day2_password sets;;
Printf.printf "valid day2 password count: %i\n" (List.length valid_day2_passwords)

1000
day2.txt

File diff suppressed because it is too large Load Diff

@ -0,0 +1,47 @@
(* Common util. Should probably put this in a module or something.. *)
let get_one_line file =
try Some (input_line file) with
End_of_file -> None
let get_lines file =
let rec input lines =
match get_one_line file with
Some line -> input (line :: lines)
| None -> List.rev lines
in input []
let read_file file =
let channel = open_in(file) in
get_lines channel
let explode s = (* convert a string to a list of chars *)
let rec exp i l =
if i < 0 then l else exp (i - 1) (s.[i] :: l) in
exp (String.length s - 1) [];;
(* --- *)
let wrapped_y y map = (* wrap the y (horizontal) coordinate when off the map *)
y mod (List.length (List.nth map 1))
let read_cell map x y = (* return Some char at these coordinates, or None if below the map *)
if (x < List.length map)
then Some (List.nth (List.nth map x) (wrapped_y y map))
else None
let rec go map position slope count = (* move across the map, counting trees *)
let (x, y) = position in
let (dy, dx) = slope in (* suddenly regretting having x be the vertical axis.. *)
match read_cell map x y with
| None -> count (* off the map. stop counting *)
| Some '#' -> go map (x+dx, y+dy) slope (count+1)
| Some a -> go map (x+dx, y+dy) slope count
let map = read_file "day3.txt" |> List.map explode
let slopes = [(1, 1); (3, 1); (5, 1); (7, 1); (1, 2)]
let tree_counts = slopes |> List.map (fun slope -> go map (0, 0) slope 0);;
List.iter (fun count -> Printf.printf "Number of trees: %i\n" count) tree_counts;;
let product = List.fold_left ( * ) 1 tree_counts;;
Printf.printf "Product: %i\n" product

@ -0,0 +1,323 @@
....#...............##...#...#.
#...#..#.....##.##.#.##....#...
...#.....#...#.................
#..#..#.......#...#.#..........
...##..#.#..........##...#.....
........###.#.##..#............
...###......##.#..#.#...#.#....
......##..#.#....#...#.........
.................#......#......
..............##....#..........
#.....................#...#.#.#
.##..#............##...##.##..#
.....#.####...#..##......#.#..#
#.......#.#..#......##.#.#....#
.....##...###.#..........##....
#...........#.##....##.....#..#
..###..##.##.....#....#........
...#.#.#............#.#..#....#
#......#....#...##.#.#.#.#..#..
.......#.#...#..#..#..##......#
.....#..#.............#..#...#.
##..#.##.....#........#........
....##....#..#...........#...#.
.......#........##.......##....
..##...#.......#........##.#...
..........#..#.....##........#.
..#..##..#............#........
.#.#...#...#.......#......#....
....#....#.....#.#.........###.
.............#...#....#..#.#...
##.#...#..#......#.#.##.....#..
#...##.#..........#..#.#...#...
#####.......#.#.....#..#.......
#...#...#........#....#...#....
......##.#..#..#............#..
....#....#.......#...###.......
.#......##...#.##....#...#.....
..#....#...##.....#.#...##.#...
#.......#........#.####........
#.##..#..#.........#.#........#
.#...#.#.#.#......#....#.#..#..
#...####...##.##.#....#......##
..#...#......##........#.....#.
...#.#....##...................
...##................#.........
...##.....##........#....#..#..
.........#..#.....#............
.#..#.......................#..
.#.........#..##........#.#.#.#
......#.....##..#.##...#..#.##.
..#..............##.......#....
...............##....#...##..#.
###...#..###.........#...#.....
...#..#...#....#.....##........
....#..##...#........#.........
..#......#.......#.....#..#....
.#...........##.....###....#...
.#..#.....##.........##.....#..
....##.#.....#................#
..#..#......#.#..#....#..##....
#.....#...##............#......
.#.............#....#.......#..
#.........#..#...##.#...#.#.##.
...#......#..####....#.#.....#.
......#........#..........##.##
......##.#..##.##.....#........
##.....#..##.##..#.......##....
.##.........#...........#...#..
.....#...###..#...#...........#
..........#.#......#.###.....#.
...#.............#.##......##..
#.##.........#..###...........#
....#..##....#..#..#........###
...#........##.......##..#..#..
...#......#..#.#...............
#......###....#.#..#.#..#....#.
#.#.####.#.........#..#.#.#....
.....#....#...............#...#
.#........#......#.#...#.......
................#...#.....##...
.............#...####..........
.................##....##.###..
#................#......#......
.###.#........#..##.....#..###.
..#.#..#...#..#.#...#.#.....#.#
.....#............#..##..#..#.#
#........##.#...#.....#........
#.#.#..###......###............
...#..#...........##...#.....#.
......#........#...#.#....#....
....#..........#.#..#.#....##..
...#.....##..#......#.#.##...#.
.........#..#................#.
..#....#.##.....#.......#......
...#.....#.......##.##.....#...
#...#..............#..###..#...
#.#......#.#....#........##..#.
...#...##...##..#.........#....
..#...#......##.#.#.#....##....
#.......#.......#..#..#........
.........#..#.....#....#.....##
.#......#.......#.#..#..#...#.#
..#....#.#..#..................
#.....####..........#.#.....#.#
.#..#.#.#....#.#.....#.#.......
....##......#..#.....#.#.#...#.
...##...#......##.#....##.#....
..#..##....#...#...........#...
.......#........#...##.#.......
#.#..#....##.#....##...........
.......#............#..##..##..
#.#.#.....#....##.#.#.#.....#..
##...#...#.......#..#...#.....#
##..##.##..........#........##.
..............#.....#..#..##...
.......#...#.........#....#.#..
...#..#..#....#.#....##........
..#.......#....#....##.........
#...#.....#..#.#...##....#.....
.....##..#..##..#..............
.....##............#....#.#....
..#.....#....##.#.....#..#.....
#...#..#..#......#.#.#..##.....
.............................##
#...#.#................#....#.#
.#.#.#....##......###..##......
#.....#..#.##.#.#.##...###.....
.........#............##..#....
.#..#...#....#.....#.#........#
...............#......#..#.....
...................###........#
.###..##..##.......#.#.........
#.........#......#....#.#...#..
.#.#....#.......#.#..##...##...
.#.....#....##.......#.#.....#.
.........#...#....#.#..........
....###..#..##.#...##....#..#..
...#.#..##.#.........###.#..#..
#...#...........#....#.........
....##...........#.#.#......###
#....#...........##..#.........
###....#.....#.......#....###..
.#.......#....#.#.#.#......#.#.
........#...............#.#.#..
....#.........#.....#...##.##.#
...#............#.............#
..........#..#.................
........#.....##............#.#
..#...##........#...#.....#.#..
....#........#.#.#..........#..
#.#...#...........#............
....#.#...##...........#.....#.
...........#.#..#.....#........
.....#..#..#..#.....#.#.....#.#
#.....#.......#.......#...#....
#.........#....#.#........#..#.
...#..#.........#.....#..#.....
...#..#.............#..........
.#.......#..........#.....#...#
.....#.#......#.......#....#...
...#.....#..#..##....##....#...
.#.#.#..#...#.....#....#.......
..##.#..........#.....#.#......
..#..#.............#...##..##..
.#.............#..#....##...#..
..#...#.....#.................#
..##.......#.....#...#....#....
.#..#.##.........#...#.#...#...
...##.......##..#.....##.##...#
........####.#.........#.......
..#.#...##.#..#..#.......##.#..
.#..#............###..#..#.....
#.....#.#...#.#.......#........
..........#......#.#...#...#...
..#......#..#..#.#...#.........
..###........#.#....#.#...##...
.#.....#..#.#......#........#..
.#...#..#...#....#.......#..#..
..#....#..#.....#.#........#...
#..#.#.........#..........#..#.
.#.....##....#.........#.#.#.#.
#.#...#.....#.#.#....#.#..#....
.........#...................#.
..#.....#..##...#..........#.#.
..............#....#.........#.
.#....#.....#..............##..
#...#...#.#........##.........#
....###....#.#....#.#.........#
.....#........#.....##.........
.#...##..##..#.........##......
............#.....#........#...
..#....#.......#......#..#.#.#.
#.......#.#...........#..##.#..
......#.##......#....#.......#.
.....#........#...###.....#....
###..........#........#.#.#....
.....#...#.#...#...#...##.....#
.##...#.#........#.#....#......
......#.........#.....#.#......
.....#.##.....###.#...#...##..#
.#.#.......##....#..#..#.##....
.####...###.#.#.#.#............
......#..##...#..........#.##.#
......#............#...........
.....#.#..#.......##...##......
......#........#..#....#.#.#.#.
#..#..#.....#..#.....#.......#.
.#...#.....#..............#....
.#....#..#.##.#............####
..........#....#.##...#.#......
...#.#.#.#.#.......#.........#.
##........#..##..#.........#...
..#......#...#..#.#.....#......
..#.#......#...#...#.#.........
........................##.....
...#.##.#........#...#.......#.
..#.#......#....##........#.#..
#......#.##........#..#......#.
.....#..#..#.............#.....
......#......#........#....#...
...#....###.....#..#.#....#....
#.......................#....#.
..#...#...................#....
....#..#.....##.#..#...#.....#.
...#.........#...#.......#.....
..#....#.....#...#...#.#####...
.....####......#...........#...
......#.#..........#...#.#.#..#
###..#.#....#..#...............
...#...###..#..#.#.#...........
.....#...#.##.#.#.###..##......
.........#...........#....##.#.
....#..#......#................
...........#..#..#...#.#.......
..#.....#......##.###..........
.........#...................#.
..........#...#.#....##........
..#...##....#....#.......#...##
#......#.....#...#...#...#.....
....##...#.#.......#.#...##....
...#.....#....#.....#....#.....
#....##.....##..##..........##.
.....#.....#.#.#...............
.#.##....#.....#.#..#....#..##.
.....#.#.....##....#...........
.........#..#.......##..##.....
..#....##.....###...#....#.#...
............#......#.#...#..#..
#..##......#.#.##....#.#.......
.#.#.....#...#.#.#....#.....#..
#....#..#.#....#...#...........
......#.#.....#...#.#.#......#.
###..#....#.###.............#..
..............#####........###.
..#..#.#.#.#......#......#.....
###.........#.#..........#..#.#
.#.........#...#......####.....
..#.......####..#....#...#..#..
#.#..#.#...............#.#.#.#.
###....#.....##.#....#......##.
..#..#........#....###.#.#.....
...#.#..........#.....#...#....
....#......##.#............#..#
...##...#.....#..##....#..#.#.#
.......#.....#..#....#....##.#.
.#..#....#..#......##....##...#
..#......#...#.#..###..#.##....
#...#.....#......##...#.......#
.....#.#.....#...##............
.#..##.##..#..##.#........#....
....#.#......##...#.#.#.#..##..
.#..............##........#....
.##....#..#..#....#...#......#.
............###....##.......##.
..............####.....#.......
........##..##.#...#.......#...
....#..#.....##.......#####...#
.##..##..#.....#...#..#..#....#
##..#.#.#...........#..........
#..#......#...#....#...........
...#..##.#..........#..#.......
........#.#.....#......##......
.....#....#............#.......
.#.#..#....##......#.......###.
.#..#.#........#......#...##..#
...#....#......#..#........#.##
.........#..#...#..#.#.##......
....###.#...........#...#......
.##............#.......#..##...
##...#.#...............#.#...##
..#..#.....#.#..#..#...........
..#..#.##..#......#.##..#.#....
..#...#......#.#...#....##.#...
...###....####......#....#...#.
.......##........#.....##....#.
.........##..........#...#.....
.....#............#.##.#....#.#
..........#...#....##..........
....................#......#...
#......#..#...#.............##.
...........#...................
..#...#.........#.##.#..##.#...
#.#....#.#.....#............#..
.#..#.....#.....####......#.#..
#....#.......##..#...........#.
............#...#.....#..#.#...
#...........#...#####....#...#.
..........#...###..##.........#
#.....###............#..#..#.#.
...##.....#....#......#.....#..
#....#.......#..#......###...#.
...##.##......##..##..........#
.......#.#..#.#..#.#.#.#..#..#.
..#..###...#....#.....#......#.
...#.........#..#.##.#.....###.
..#.........#.##.#..#..#..###..
..####..#.........#.........#.#
..#.#...#.......#....##........
.#......#.#....................
..........#.......#.#..#..#....
..#........#....#.#..#.........
..#.....#.............#....#...
##...#.........#.....#...#.....
Loading…
Cancel
Save