Day 1 in ruby
authorNeil Smith <neil.git@njae.me.uk>
Sun, 16 Jul 2017 17:56:31 +0000 (18:56 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 16 Jul 2017 17:56:31 +0000 (18:56 +0100)
01-ticket-prices/day1-ruby.ipynb [new file with mode: 0644]
01-ticket-prices/day1.rb [new file with mode: 0644]

diff --git a/01-ticket-prices/day1-ruby.ipynb b/01-ticket-prices/day1-ruby.ipynb
new file mode 100644 (file)
index 0000000..2c2d07b
--- /dev/null
@@ -0,0 +1,239 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {
+    "scrolled": true
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[[\"dda7d369\", \"1546\", \"Uzupis\", \"21\"], [\"68022753\", \"1239\", \"Mamula\", \"21\"], [\"b261dbd1cef\", \"996\", \"Holmegaard\", \"21\"], [\"5b23fa704734\", \"1194\", \"Giessenmestia\", \"14\"], [\"7b049264\", \"1077\", \"Hajnowski\", \"10\"], [\"ce8b3fcd44b1\", \"849\", \"Sofia\", \"14\"], [\"68660362\", \"1568\", \"Morgantown\", \"21\"], [\"ea6c84938e2\", \"1310\", \"Nullarbor\", \"7\"], [\"1385d067d40b\", \"1365\", \"Zhangye-Shi\", \"7\"], [\"4ebc5e0b\", \"1227\", \"Uzupis\", \"7\"], [\"ee064e1e2ea\", \"785\", \"Nullarbor\", \"14\"], [\"34f7247adaf\", \"1413\", \"Nordkapp\", \"10\"], [\"ec5f5691\", \"1150\", \"Luoyang\", \"14\"], [\"b1aa6c5d8a\", \"821\", \"Estacada\", \"21\"], [\"71573a6513\", \"1259\", \"Giessenmestia\", \"7\"], [\"a87ad38a7\", \"879\", \"Aalborg\", \"21\"], [\"67985d6aa\", \"1525\", \"Stonington-Island\", \"14\"], [\"303db888d\", \"1316\", \"Tubakuba\", \"7\"], [\"6f2f9ff6b7bd\", \"903\", \"Zhangye-Shi\", \"3\"], [\"54670c3136d\", \"1254\", \"Luoyang\", \"7\"], [\"e1f3bd201\", \"1379\", \"Uzupis\", \"14\"], [\"a2dd4aec\", \"1160\", \"Nullarbor\", \"3\"], [\"398d3e972f49\", \"1194\", \"Estacada\", \"14\"], [\"cd227c32d4e\", \"984\", \"Estacada\", \"14\"], [\"16935ed587bf\", \"1312\", \"Grimsey-Island\", \"14\"], [\"ceb26b792\", \"1352\", \"Sofia\", \"21\"], [\"952801ceb36\", \"1126\", \"Stonington-Island\", \"7\"], [\"7f1a85bc\", \"1140\", \"Luoyang\", \"7\"], [\"d6b03403\", \"1207\", \"Morgantown\", \"14\"], [\"3251b683b9f\", \"1466\", \"Zhangye-Shi\", \"21\"], [\"134d5f5b3f\", \"1399\", \"Tambon-Pa-Fa\", \"14\"], [\"732cbc2c276b\", \"947\", \"Sukhumi\", \"14\"], [\"60fdf85cf570\", \"1002\", \"Almaty\", \"14\"], [\"303965365c4\", \"1594\", \"Sofia\", \"14\"], [\"1fc012fcb7c5\", \"1081\", \"Jayuya\", \"21\"], [\"579302498bdc\", \"1076\", \"Parowan\", \"14\"], [\"27861346\", \"1046\", \"Grimsey-Island\", \"14\"], [\"9a226b7b33c\", \"906\", \"Hajnowski\", \"7\"], [\"943fa28d\", \"1541\", \"Karlukovo\", \"7\"], [\"c3cac7d3bc\", \"813\", \"Ijsseloog\", \"7\"], [\"41c837263600\", \"1083\", \"Puente-Laguna-Garzonkuala-Penyu\", \"14\"], [\"4a642c384ea\", \"1146\", \"Sukhumi\", \"14\"], [\"9997e977978\", \"1170\", \"Geoje-Si\", \"14\"], [\"eaa91671\", \"943\", \"Stonington-Island\", \"14\"], [\"26ae2d0b7\", \"1188\", \"Aalborg\", \"7\"], [\"1634fe98c256\", \"1058\", \"Jayuya\", \"7\"], [\"1892f12d6\", \"948\", \"Stonington-Island\", \"14\"], [\"2407d5e3\", \"1234\", \"Sofia\", \"7\"], [\"49e8530b61a0\", \"1061\", \"Nullarbor\", \"3\"], [\"c1254c8fc9a\", \"909\", \"Puente-Laguna-Garzonkuala-Penyu\", \"7\"], [\"a3fec18095\", \"1256\", \"Geoje-Si\", \"7\"], [\"650a2b292e5c\", \"1623\", \"Puente-Laguna-Garzonkuala-Penyu\", \"14\"], [\"bf75e25a6b8\", \"1448\", \"Puente-Laguna-Garzonkuala-Penyu\", \"7\"], [\"44dadca0cd9\", \"1345\", \"Aalborg\", \"7\"], [\"4caefcacded0\", \"1644\", \"Gorongosa\", \"21\"], [\"de5d04b2c3a\", \"867\", \"Tubakuba\", \"7\"], [\"aea2a458023\", \"727\", \"Brorfelde\", \"21\"], [\"c5f99dca95e\", \"1727\", \"Morgantown\", \"14\"], [\"2ee77108\", \"1549\", \"Mora\", \"7\"], [\"4809881ff7\", \"1380\", \"Holmegaard\", \"7\"], [\"5d2c9d24\", \"1263\", \"Giessenmestia\", \"21\"], [\"67c14f2635\", \"1157\", \"Stonington-Island\", \"7\"], [\"3962815e6\", \"1497\", \"Ijsseloog\", \"7\"], [\"757f3c9f5\", \"1208\", \"Hajnowski\", \"14\"], [\"e1cd5acf5\", \"1044\", \"Stonington-Island\", \"14\"], [\"0ee0ff115\", \"1181\", \"Jayuya\", \"21\"], [\"06af8642ff\", \"1389\", \"Sofia\", \"7\"], [\"46c6b6fa76\", \"1184\", \"Hajnowski\", \"14\"], [\"435b4e82\", \"1429\", \"Morgantown\", \"21\"], [\"35ea596f\", \"1613\", \"Tubakuba\", \"7\"], [\"f7cbb3da7e6\", \"731\", \"Aalborg\", \"14\"], [\"e1df7ea96302\", \"1483\", \"Almaty\", \"7\"], [\"6fb97c1a3\", \"993\", \"Almaty\", \"7\"], [\"c86e2e5826\", \"1331\", \"Morgantown\", \"21\"], [\"995f57877aac\", \"1155\", \"Tambon-Pa-Fa\", \"14\"], [\"6ffab38e7b\", \"1228\", \"Mamula\", \"14\"], [\"f60e203aaaf9\", \"623\", \"Lemnos\", \"21\"], [\"1942931f2\", \"1219\", \"Parowan\", \"14\"], [\"da926535df\", \"1383\", \"Jayuya\", \"7\"], [\"b1dc6df3240\", \"1261\", \"Holmegaard\", \"7\"], [\"df7bd3ee5\", \"1209\", \"Parowan\", \"7\"], [\"b4e498b9ed\", \"1060\", \"Geoje-Si\", \"14\"], [\"bce7bdf7c\", \"878\", \"Lemnos\", \"10\"], [\"1ab9eb3d5422\", \"1376\", \"Tambon-Pa-Fa\", \"14\"], [\"839240114\", \"1546\", \"Tubakuba\", \"7\"], [\"04feb63602\", \"1050\", \"Ijsseloog\", \"14\"], [\"cb89673b727\", \"1681\", \"Ijsseloog\", \"14\"], [\"1e38291d\", \"1438\", \"Luoyang\", \"14\"], [\"f806fc9d\", \"1553\", \"Nordkapp\", \"21\"], [\"a1342d1ec\", \"1368\", \"Ijsseloog\", \"7\"], [\"4a67a59a\", \"1055\", \"Almaty\", \"7\"], [\"c11e991e22ea\", \"1453\", \"Nullarbor\", \"3\"], [\"d168673f959\", \"1330\", \"Karlukovo\", \"7\"], [\"3b1a78218bb5\", \"1083\", \"Tambon-Pa-Fa\", \"10\"], [\"4435ea0d0c5d\", \"1205\", \"Tambon-Pa-Fa\", \"7\"], [\"7a9b44f3bcc\", \"1277\", \"Mora\", \"14\"], [\"0ba9dcee27f3\", \"1261\", \"Karlukovo\", \"14\"], [\"705baebce\", \"1287\", \"Tubakuba\", \"3\"], [\"353e6dc9\", \"1039\", \"Sukhumi\", \"10\"], [\"c9c6d041\", \"1194\", \"Mora\", \"14\"], [\"33123a00\", \"1438\", \"Grimsey-Island\", \"14\"], [\"83f28647\", \"973\", \"Jayuya\", \"7\"], [\"b8521145\", \"1338\", \"Ijsseloog\", \"7\"], [\"b70e605889\", \"834\", \"Puente-Laguna-Garzonkuala-Penyu\", \"10\"], [\"11896bedc4\", \"999\", \"Morgantown\", \"7\"], [\"6ed9a88972\", \"1295\", \"Holmegaard\", \"14\"], [\"166b77df\", \"979\", \"Zhangye-Shi\", \"14\"], [\"fc05fce8d\", \"1551\", \"Lemnos\", \"14\"], [\"4ce3111c66\", \"1106\", \"Karlukovo\", \"10\"], [\"b96caebcfd6\", \"1155\", \"Geoje-Si\", \"21\"], [\"17d3decd842\", \"1041\", \"Ijsseloog\", \"7\"], [\"b6d482b92\", \"1497\", \"Estacada\", \"14\"], [\"1fb40d6b\", \"1568\", \"Stonington-Island\", \"7\"], [\"b4db7910\", \"1300\", \"Ijsseloog\", \"10\"], [\"153da27bcb75\", \"1030\", \"Ijsseloog\", \"3\"], [\"1afd05d3ea88\", \"1072\", \"Parowan\", \"7\"], [\"1541ba11\", \"1047\", \"Almaty\", \"7\"], [\"38b01883fd\", \"1309\", \"Mamula\", \"10\"], [\"f22b8e717c\", \"1401\", \"Tambon-Pa-Fa\", \"21\"], [\"4ad6523f\", \"1085\", \"Luoyang\", \"7\"], [\"38b9690d981\", \"1204\", \"Jayuya\", \"14\"], [\"bfc72b962\", \"1153\", \"Tambon-Pa-Fa\", \"14\"], [\"b116bf9bd\", \"1307\", \"Zhangye-Shi\", \"21\"], [\"7b1c64c74\", \"1361\", \"Tambon-Pa-Fa\", \"10\"]]"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "holidays = File.readlines('01-holidays.txt').map {|l| l.chomp.split}\n",
+    "holidays"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       ":affordable_holidays"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "def affordable_holidays(hs)\n",
+    "  hs.select {|h| h[1].to_i < 1200}\n",
+    "end"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "59"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "affordable_holidays(holidays).length"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "59"
+      ]
+     },
+     "execution_count": 10,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "File.readlines('01-holidays.txt').select {|l| l.split[1].to_i <= 1200}.length"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 28,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1.0"
+      ]
+     },
+     "execution_count": 28,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "$destination_values = {'Almaty' => 2.0, 'Brorfelde' => 0.9, 'Estacada' => 0.4, \n",
+    "  'Jayuya' => 0.6, 'Karlukovo' => 2.2, \n",
+    "                      'Morgantown' => 2.9,'Nordkapp' => 1.5, 'Nullarbor' => 2.2, \n",
+    "                      'Puente-Laguna-Garzonkuala-Penyu' => 0.4, 'Uzupis' => 0.9}\n",
+    "$destination_values.default = 1.0"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 29,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "2.0"
+      ]
+     },
+     "execution_count": 29,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "$destination_values['Almaty']"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 30,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1.0"
+      ]
+     },
+     "execution_count": 30,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "$destination_values['Aardvark']"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 31,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       ":value"
+      ]
+     },
+     "execution_count": 31,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "def value(holiday)\n",
+    "  $destination_values[holiday[2]] * holiday[3].to_f / holiday[1].to_i \n",
+    "end"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 32,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "0.012225097024579561"
+      ]
+     },
+     "execution_count": 32,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "value(holidays[0])"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 34,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "\"ee064e1e2ea\""
+      ]
+     },
+     "execution_count": 34,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "affordable_holidays(holidays).max_by {|h| value h}[0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {
+    "collapsed": true
+   },
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Ruby 2.4.0",
+   "language": "ruby",
+   "name": "ruby"
+  },
+  "language_info": {
+   "file_extension": ".rb",
+   "mimetype": "application/x-ruby",
+   "name": "ruby",
+   "version": "2.4.0"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/01-ticket-prices/day1.rb b/01-ticket-prices/day1.rb
new file mode 100644 (file)
index 0000000..4be4935
--- /dev/null
@@ -0,0 +1,20 @@
+
+$destination_values = {'Almaty' => 2.0, 'Brorfelde' => 0.9, 'Estacada' => 0.4, 
+  'Jayuya' => 0.6, 'Karlukovo' => 2.2, 'Morgantown' => 2.9,'Nordkapp' => 1.5, 
+  'Nullarbor' => 2.2, 'Puente-Laguna-Garzonkuala-Penyu' => 0.4, 'Uzupis' => 0.9}
+$destination_values.default = 1.0
+
+holidays = File.readlines('01-holidays.txt').map {|l| l.chomp.split}
+
+def affordable_holidays(hs)
+  hs.select {|h| h[1].to_i <= 1200}
+end
+
+def value(holiday)
+  $destination_values[holiday[2]] * holiday[3].to_f / holiday[1].to_i 
+end
+
+part1 = affordable_holidays(holidays).length
+part2 = affordable_holidays(holidays).max_by {|h| value h}[0]
+
+puts "Part 1: #{part1}\nPart 2: #{part2}"