@miracle173/

ponder_201905

Python

No description

fork
loading
Files
  • main.py
  • solutions.txt

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.8248884041947244","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyBmw7xyIEVsc2kKIyBodHRwczovL3NpbW9uaGVzc25lci5kZS9jYWxjdWxhdGUtcG93ZXItc2V0LXNldC1vZi1hbGwtc3Vic2V0cy1pbi1weXRob24td2l0aG91dC1yZWN1cnNpb24vCnByaW50KCJ2ZXJzaW9uIDIuMSIpCmZyb20gaXRlcnRvb2xzIGltcG9ydCBjb21iaW5hdGlvbnMsIGNvbXByZXNzCmZyb20gbWF0aCBpbXBvcnQgbG9nLCBleHAKZnJvbSByYW5kb20gaW1wb3J0IGNob2ljZSwgcmFuZHJhbmdlLCByYW5kb20sIHNhbXBsZSwgc2VlZApmcm9tIHRpbWUgaW1wb3J0IHRpbWUKZnJvbSBkYXRldGltZSBpbXBvcnQgZGF0ZXRpbWUKaW1wb3J0IGpzb24KI2h0dHBzOi8vcmVwbC5pdC90YWxrL3NoYXJlL05FVy1QeXRob24tTGliLWZvci13d3dqc29uc3RvcmVpby1ET05ULUxPT1NFLVlPVVItRC1BR0FJTi1XSVRILUpTT04tU1RPUkUtTkVXLzEwOTU2ClJVTl9SRVBMPVRydWUKUlVOX1JFUEw9RmFsc2UKCmlmIFJVTl9SRVBMOgogICAgaW1wb3J0IGpzb25fc3RvcmVfY2xpZW50Cgpqc29uU3RvcmVUb2tlbj0naHR0cHM6Ly93d3cuanNvbnN0b3JlLmlvLzZiYTAyMzQyMWRjYWRiZDFhZWVhNGY2YmJkNjU0OTQwMDYwOTgwNGM0MzA5YzFmYTE5ZjAzZDczOTMwMmEyZGYnCgppZiBSVU5fUkVQTDoKICAgIGNsaWVudCA9IGpzb25fc3RvcmVfY2xpZW50LkNsaWVudChqc29uU3RvcmVUb2tlbikKCkZJTEVfTkFNRV8xPSdzdGF0ZWZpbGUxJwpGSUxFX05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ICB0cnk6ICAgIAogICAgICAgIGlmIFJVTl9SRVBMOgogICAgICAgICAgICBzYXZlX2RpY3RfMT1jbGllbnQucmV0cmlldmUoRklMRV9OQU1FXzEpCiAgICAgICAgZWxzZToKICAgICAgICAgICAgd2l0aCBvcGVuKEZJTEVfTkFNRV8xKycudHh0JywncicpIGFzIGY6CiAgICAgICAgICAgICAgICBzYXZlX2RpY3RfMT1qc29uLmxvYWRzKGYucmVhZCgpKQogICAgICAgIHNhdmVfdGltZV8xPXNhdmVfZGljdF8xWyJzYXZldGltZSJdCiAgICAgICAgc3RhdGVfZGljdF8xPXNhdmVfZGljdF8xWyJzdGF0ZSJdCiAgICAgICAgc3RhdGVfcmVhZHlfMT0odHlwZShzYXZlX2RpY3RfMSk9PXR5cGUoe30pKQogICAgZXhjZXB0OgogICAgICAgIHByaW50KCJzdGF0ZV9maWxlXzEgZXJyb3IiKQogICAgICAgIHN0YXRlX3JlYWR5XzE9RmFsc2UKCiAgICB0cnk6CiAgICAgICAgaWYgUlVOX1JFUEw6CiAgICAgICAgICAgIHNhdmVfZGljdF8yPWNsaWVudC5yZXRyaWV2ZShGSUxFX05BTUVfMikKICAgICAgICBlbHNlOgogICAgICAgICAgICB3aXRoIG9wZW4oRklMRV9OQU1FXzIrJy50eHQnLCdyJykgYXMgZjoKICAgICAgICAgICAgICAgIHNhdmVfZGljdF8yPWpzb24ubG9hZHMoZi5yZWFkKCkpCiAgICAgICAgc2F2ZV90aW1lXzI9c2F2ZV9kaWN0XzJbInNhdmV0aW1lIl0KICAgICAgICBzdGF0ZV9kaWN0XzI9c2F2ZV9kaWN0XzJbInN0YXRlIl0KICAgICAgICBzdGF0ZV9yZWFkeV8yPSh0eXBlKHNhdmVfZGljdF8yKT09dHlwZSh7fSkpCiAgICBleGNlcHQ6CiAgICAgICAgcHJpbnQoInN0YXRlX2ZpbGVfMiBlcnJvciIpCiAgICAgICAgc3RhdGVfcmVhZHlfMj1GYWxzZQogICAgICAgIAogICAgaWYgc3Rh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FuZ2UoMioqbildCgoKYmFua19uYW1lID0gWydzdGFydCcsICdlbmQnXQoKCmRlZiBuZXh0X25vZGUoYm91bmRhcnksIHVuaXZlcnNlLCBhZG1pc3NpYmxlKToKICAgIGZvciBwZW9wbGUgaW4gYm91bmRhcnk6CiAgICAgICAgZm9yIHBlb3BsZV9idXRfMSBpbiBjb21iaW5hdGlvbnMocGVvcGxlLCBsZW4ocGVvcGxlKS0xKToKICAgICAgICAgICAgcGVvcGxlX2J1dF8xID0gc2V0KHBlb3BsZV9idXRfMSkKICAgICAgICAgICAgaWYgcGVvcGxlX2J1dF8xIGluIGFkbWlzc2libGU6CiAgICAgICAgICAgICAgICB5aWVsZCB1bml2ZXJzZS5kaWZmZXJlbmNlKHBlb3BsZV9idXRfMSkKICAgICAgICBpZiBsZW4ocGVvcGxlKSA+PSAyOgogICAgICAgICAgICBmb3IgcGVvcGxlX2J1dF8yIGluIGNvbWJpbmF0aW9ucyhwZW9wbGUsbGVuKHBlb3BsZSktMik6CiAgICAgICAgICAgICAgICBwZW9wbGVfYnV0XzI9c2V0KHBlb3BsZV9idXRfMikKICAgICAgICAgICAgICAgIGlmIHBlb3BsZV9idXRfMiBpbiBhZG1pc3NpYmxlOgogICAgICAgICAgICAgICAgICAgIHlpZWxkIHVuaXZlcnNlLmRpZmZlcmVuY2UocGVvcGxlX2J1dF8yKQoKZGVmIG5leHRfbm9kZVgoYm91bmRhcnksIHVuaXZlcnNlLCBhZG1pc3NpYmxlKToKICAgIGZvciBwZW9wbGUgaW4gYm91bmRhcnk6CiAgICAgICAgZm9yIHBlb3BsZV9idXRfMSBpbiBjb21iaW5hdGlvbnMocGVvcGxlLCBsZW4ocGVvcGxlKS0xKToKICAgICAgICAgICAgcGVvcGxlX2J1dF8xID0gc2V0KHBlb3BsZV9idXRfMSkKICAgICAgICAgICAgaWYgcGVvcGxlX2J1dF8xIGluIGFkbWlzc2libGU6CiAgICAgICAgICAgICAgICB5aWVsZCAocGVvcGxlLHVuaXZlcnNlLmRpZmZlcmVuY2UocGVvcGxlX2J1dF8xKSkKICAgICAgICBpZiBsZW4ocGVvcGxlKSA+PSAyOgogICAgICAgICAgICBmb3IgcGVvcGxlX2J1dF8yIGluIGNvbWJpbmF0aW9ucyhwZW9wbGUsbGVuKHBlb3BsZSktMik6CiAgICAgICAgICAgICAgICBwZW9wbGVfYnV0XzI9c2V0KHBlb3BsZV9idXRfMikKICAgICAgICAgICAgICAgIGlmIHBlb3BsZV9idXRfMiBpbiBhZG1pc3NpYmxlOgogICAgICAgICAgICAgICAgICAgIHlpZWxkIChwZW9wbGUsdW5pdmVyc2UuZGlmZmVyZW5jZShwZW9wbGVfYnV0XzIpKQoKCmRlZiBwcmludF9kZWJ1ZyhiYW5rbmFtZSxsZXZlbCk6CiAgICBpZiBQUklOVF9ERUJVRzoKICAgICAgICBwcmludCgnPT0+IGRlYnVnJywgYmFua25hbWUsIGxldmVsKQoKZGVmIHByaW50X2l0ZXIoc3MsZGVzY3IpOgogICAgaWYgUFJJTlRfREVCVUc6CiAgICAgICAgcHJpbnQoZGVzY3IpCiAgICAgICAgaWYgc3MgaXMgTm9uZToKICAgICAgICAgICAgcHJpbnQoTm9uZSkKICAgICAgICBlbHNlOgogICAgICAgICAgICBpZiBzczoKICAgICAgICAgICAgICAgIGZvciBzIGluIHNzOgogICAgICAgICAgICAgICAgICAgIHByaW50KHtjW3RdIGZvciB0IGluIHN9KQogICAgICAgICAgICBlbHNlOgogICAgICAgICAgICAgICAgcHJpbnQoJ2VtcHR5JykKY2xhc3MgdmVydGV4OgogICAgTm9MZXZlbD0tMQogICAgZGVmIF9faW5pdF9fKHNlbGYsIHN1YnNldCwgYmFuayk6CiAgICAgICAgc2VsZi5zdWJzZXQ9c3Vic2V0CiAgICAgICAgc2VsZi5iYW5rPWJhbmsKICAgICAgICBzZWxmLnN1Y2M9c2V0KCkKICAgICAgICBzZWxmLnByZWQ9c2V0KCkKICAgICAgICBzZWxmLmxldmVsPXZlcnRleC5Ob0xldmV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mLCB2MCwgdjEsIHYyLCB2Myk6CiAgICAgICAgYXNzZXJ0KHYwLnN1YnNldD09djEuc3Vic2V0KQogICAgICAgIGFzc2VydCh2Mi5zdWJzZXQ9PXYzLnN1YnNldCkKICAgICAgICBhc3NlcnQodjAuc3Vic2V0PT1zZWxmLnVuaXZlcnNlLmRpZmZlcmVuY2UodjIuc3Vic2V0KSkKICAgICAgICBhc3NlcnQodjAuYmFuaz09MCkKICAgICAgICBhc3NlcnQodjEuYmFuaz09MSkKICAgICAgICBhc3NlcnQodjIuYmFuaz09MCkKICAgICAgICBhc3NlcnQodjMuYmFuaz09MSkKICAgICAgICBmb3IgKHRvX3ZlcnRleF8wLCB0b192ZXJ0ZXhfMSwgY29tcGxlbWVudCkgaW4gWyh2MCwgdjEsIHYyLnN1YnNldCksICh2MiwgdjMsIHYwLnN1YnNldCldOgogICAgICAgICAgICBmb3IgaSBpbiBbMSwyXToKICAgICAgICAgICAgICAgIGlmIGxlbih0b192ZXJ0ZXhfMC5zdWJzZXQpPj1pOgogICAgICAgICAgICAgICAgICAgIGZvciBwcmV2c2V0IGluIGNvbWJpbmF0aW9ucyh0b192ZXJ0ZXhfMC5zdWJzZXQsIGxlbih0b192ZXJ0ZXhfMC5zdWJzZXQpLWkpOgogICAgICAgICAgICAgICAgICAgICAgICBub2RlPXNlbGYuc3Vic2V0cy5nZXQocHJldnNldCkKICAgICAgICAgICAgICAgICAgICAgICAgaWYgbm9kZToKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5vZGVbMF0uY29ubmVjdHN0byh0b192ZXJ0ZXhfMCkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG5vZGVbMV0uY29ubmVjdHN0byh0b192ZXJ0ZXhfMSkKICAgICAgICAgICAgICAgIGlmIGxlbihjb21wbGVtZW50KT49aToKICAgICAgICAgICAgICAgICAgICBmb3IgdHJhbnNmZXIgaW4gY29tYmluYXRpb25zKGNvbXBsZW1lbnQsIGkpOgogICAgICAgICAgICAgICAgICAgICAgICBuZXh0c2V0PXRvX3ZlcnRleF8wLnN1YnNldHxzZXQodHJhbnNmZXIpCiAgICAgICAgICAgICAgICAgICAgICAgIG5vZGU9c2VsZi5zdWJzZXRzLmdldChuZXh0c2V0KQogICAgICAgICAgICAgICAgICAgICAgICBpZiBub2RlOgogICAgICAgICAgICAgICAgICAgICAgICAgICAgdG9fdmVydGV4XzAuY29ubmVjdHN0byhub2RlWzBdKQogICAgICAgICAgICAgICAgICAgICAgICAgICAgdG9fdmVydGV4XzEuY29ubmVjdHN0byhub2RlWzFdKQogICAgICAgIHJldHVybiAodjAsIHYxLCB2MiwgdjMpCgogICAgZGVmIF9kaXNjb25uZWN0X3ZlcnRleGVzKHNlbGYsIHYwLCB2MSwgdjIsIHYzKToKICAgICAgICBhc3NlcnQodjAuc3Vic2V0PT12MS5zdWJzZXQpCiAgICAgICAgYXNzZXJ0KHYyLnN1YnNldD09djMuc3Vic2V0KQogICAgICAgIGFzc2VydCh2MC5zdWJzZXQ9PXNlbGYudW5pdmVyc2UuZGlmZmVyZW5jZSh2Mi5zdWJzZXQpKQogICAgICAgIGFzc2VydCh2MC5iYW5rPT0wKQogICAgICAgIGFzc2VydCh2MS5iYW5rPT0xKQogICAgICAgIGFzc2VydCh2Mi5iYW5rPT0wKQogICAgICAgIGFzc2VydCh2My5iYW5rPT0xKQogICAgICAgIGZvciB2IGluIFt2MCwgdjEsIHYyLCB2M106CiAgICAgICAgICAgIGZvciB3IGluIHYuc3VjYzoKICAgICAgICAgICAgICAgIHcucHJlZC5yZW1vdmUodikKICAgICAgICAgICAgdi5zdWNjPVtdCiAgICAgICAgICAgIGZvciB3IGluIHYucHJlZDoKICAgICAgICAgICAgICAgIHcuc3Vic2V0LnJlbW92ZSh2KQogICAgICAgICAgICB2LnByZWQ9W10KCgpkZWYgc2VhcmNoKHVuaXZlcnNlLCBhZG1pc3NpYmxlLCBib3VuZGFyeT1Ob25lLCB0aGlzX2Jhbms9Tm9uZSwgb3RoZXJfYmFuaz1Ob25lLCBiYW5rPTAsIGxldmVsPTAsIHN0YXJ0PUZhbHNlICk6CiAgICBwcmludF9kZWJ1ZyhiYW5rX25hbWVbYmFua10sIGxldmVsKQogICAgcHJpbnRfaXRlcih0aGlzX2JhbmssJ3RoaXMgYmFuaycpCiAgICBwcmludF9pdGVyKG90aGVyX2JhbmssJ290aGVyX2JhbmsnKQogICAgcHJpbnRfaXRlcihib3VuZGFyeSwnYm91bmRhcnknKQogICAgaWYgc3RhcnQ6CiAgICAgICAgaWYgdW5pdmVyc2UgaW4gYWRtaXNzaWJsZToKICAgICAgICAgICAgcmV0dXJuIHNlYXJjaCh1bml2ZXJzZSwgYWRtaXNzaWJsZSwgW3VuaXZlcnNlXSwgc2V0KFt1bml2ZXJzZV0pLCBzZXQoZnJvemVuc2V0KFtdKSksIDAsICAwKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJldHVybiAtMgogICAgZWxzZToKICAgICAgICBsZXZlbCArPSAxCiAgICAgICAgbmV4dF9ib3VuZGFyeSA9IFtdCiAgICAgICAgZm9yIHBlb3BsZSBpbiBuZXh0X25vZGUoYm91bmRhcnksIHVuaXZlcnNlLCBhZG1pc3NpYmxlKToKICAgICAgICAgICAgaWYgcGVvcGxlIG5vdCBpbiBvdGhlcl9iYW5rOgogICAgICAgICAgICAgICAgaWYgcGVvcGxlID09IHVuaXZlcnNlOgogICAgICAgICAgICAgICAgICAgIHJldHVybiBsZXZlbAogICAgICAgICAgICAgICAgbmV4dF9ib3VuZGFyeS5hcHBlbmQocGVvcGxlKQogICAgICAgICAgICAgICAgb3RoZXJfYmFuay5hZGQocGVvcGxlKQogICAgICAgIHByaW50X2l0ZXIobmV4dF9ib3VuZGFyeSwgJ25leHRfYm91bmRhcnknKQogICAgICAgIGlmIG5leHRfYm91bmRhcnk6CiAgICAgICAgICAgIHJldHVybiBzZWFyY2godW5pdmVyc2UsIGFkbWlzc2libGUsIG5leHRfYm91bmRhcnksIG90aGVyX2JhbmssCiAgICAgICA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dlWzFdKQogICAgICAgICAgICAgICAgb3RoZXJfYmFuay5hZGQoZWRnZVsxXSkKICAgICAgICBwcmludF9pdGVyKG5leHRfYm91bmRhcnksICduZXh0X2JvdW5kYXJ5JykKICAgICAgICBpZiBuZXh0X2JvdW5kYXJ5OgogICAgICAgICAgICByZXR1cm4gc2VhcmNoWCh1bml2ZXJzZSwgYWRtaXNzaWJsZSwgbmV4dF9ib3VuZGFyeSwgb3RoZXJfYmFuaywKICAgICAgICAgICAgdGhpc19iYW5rLCAxLTAsIGxldmVsKQogICAgICAgIGVsc2U6CiAgICAgICAgICAgIHJldHVybiBbXQoKZGVmIGdldF9tYXhfZWxlbWVudChib29sYXJyKToKICAgIHJldHVybiBpbnQobG9nKGxlbihib29sYXJyKSwgMikpKzEKCgpkZWYgZ2V0X3VuaXZlcnNlKGJvb2xhcnIpOgogICAgcmV0dXJuIGZyb3plbnNldChyYW5nZSgxLCBnZXRfbWF4X2VsZW1lbnQoYm9vbGFycikrMSkpCgoKZGVmIGdldF9hZG1pc3NpYmxlKGJvb2xhcnIsIHVuaXZlcnNlKToKICAgIGFkbWlzc2libGUgPSBzZXQoKQogICAgcHMgPSBjb21wcmVzcyhnZXRfc3Vic2V0cyh1bml2ZXJzZSksIFtub3QgdCBmb3IgdCBpbiBib29sYXJyXSkKICAgIGZvciBzIGluIHBzOgogICAgICAgIHMuYXBwZW5kKGdldF9tYXhfZWxlbWVudChib29sYXJyKSkKICAgICAgICBhZG1pc3NpYmxlLmFkZChmcm96ZW5zZXQocykpCiAgICAgICAgYWRtaXNzaWJsZS5hZGQodW5pdmVyc2UuZGlmZmVyZW5jZShzKSkKCiAgICByZXR1cm4oYWRtaXNzaWJsZSkKCgpkZWYgcHJpbnRfYWRtaXNzaWJsZShpZHhzdHIpOgogICAgcHJpbnQoaWR4c3RyKQogICAgYm9vbGFycj1oZXgyYm9vbChpZHhzdHIpCiAgICB1bml2ZXJzZT1nZXRfdW5pdmVyc2UoYm9vbGFycikKICAgIGZv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6cmFuZHJhbmdlKE1BWF9TRUVEKSwKICAgICAgICAgICAgIlRzdGFydCI6VHN0YXJ0LAogICAgICAgICAgICAiVGVuZCI6VGVuZCwKICAgICAgICAgICAgIlQiOlQsCiAgICAgICAgICAgICJwcmV2X2xldmVsIjpwcmV2X2xldmVsLAogICAgICAgICAgICAicHJldl9pbnZhbGlkIjpib29sMmhleChwcmV2X2ludmFsaWQpLAogICAgICAgICAgICAibWF4X2xldmVsIjogbWF4X2xldmVsLAogICAgICAgICAgICAic29sdXRpb24iOiBzb2x1dGlvbgogICAgICAgIH0KICAgIGlmIG15c2VlZCBpcyBOb25lOgogICAgICAgIG15c2VlZCA9IHJhbmRyYW5nZShNQVhfU0VFRCkKICAgIHNlZWQobXlzZWVkKQogICAgcHJpbnQoJ2FubmVhbCcsIFRzdGFydCwgVGVuZCwgZGVsdGEpCiAgICBwcmludChkYXRldGltZS5ub3coKS5zdHJmdGltZSgiJVktJW0tJWQgJUg6JU06JVMiKSkKICAgIHByaW50KCdzZWVkJywgbXlzZWVkKQogICAgcHJldl9pbnZhbGlkID0gaGV4MmJvb2woc3RhcnRzdHIpCiAgICBwcmV2X2xldmVsID0gc3RhcnRfc2VhcmNoKHByZXZfaW52YWxpZCkKICAgIFQgPSBUc3RhcnQKICAgIG1heF9sZXZlbCA9IDAKICAgIGxldmVsID0gMAogICAgc3RhcnR0aW1lID0gdGltZSgpCiAgICBzb2x1dGlvbiA9IE5vbmUKICAgIGNudD0wCiAgICBsYXN0X2NudF9jaGVjaz0wCiAgICBpZiByZWNvdmVyOgogICAgICAgIG15c3RhdGU9cmVhZF9zdGF0ZSgpCiAgICAgICAgaWYgbXlzdGF0ZToKICAgICAgICAgICAgcHJpbnQoInJlY292ZXIgc3RhdGUiKQogICAgICAgICAgICBzZWVkKG15c3RhdGVbJ3NlZWQnXSkKICAgICAgICAgICAgVD1mbG9hdChteXN0YXRlWydUJ10pCiAgICAgICAgICAgIFRlbmQ9ZmxvYXQobXlzdGF0ZVsnVGVuZCddKQogICAgICAgICAgICBwcmV2X2xldmVsPWludChteXN0YXRlWydwcmV2X2xldmVsJ10pCiAgICAgICAgICAgIG1heF9sZXZlbD1pbnQobXlzdGF0ZVsnbWF4X2xldmVsJ10pCiAgICAgICAgICAgIHNvbHV0aW9uPW15c3RhdGVbJ3NvbHV0aW9uJ10KICAgICAgICAgICAgc29sdXRpb249bXlzdGF0ZVsnc29sdXRpb24nXQogICAgICAgICAgICBwcmV2X2ludmFsaWQ9aGV4MmJvb2wobXlzdGF0ZVsncHJldl9pbnZhbGlkJ10pCgogICAgd2hpbGUgVHJ1ZToKICAgICAgICBjbnQrPTEKICAgICAgICBpZiBjbnQtbGFzdF9jbnRfY2hlY2s+Q05UX0RJRkY6CiAgICAgICAgICAgIGxhc3RfY250X2NoZWNrPWNudAogICAgICAgICAgICAjc2VlZCA9IHJhbmRyYW5nZShNQVhfU0VFRCkKICAgICAgICAgICAgbXlzdGF0ZT1nZXRfc3RhdGUoKQogICAgICAgICAgICBzZWVkKG15c3RhdGVbJ3NlZWQnXSkKICAgICAgICAgICAgd3JpdGVfc3RhdGUoZ2V0X3N0YXRlKCkpCgogICAgICAgICAgICBpZiAnUFJJTlRfU1RBVEUnIGluIGdsb2JhbHMoKToKICAgICAgICAgICAgICAgIHByaW50KCdzdGF0ZScsbXlzZWVkLCBULCBib29sMmhleChwcmV2X2ludmFsaWQpKQoKCiAgICAgICAgaW52YWxpZCA9IHByZXZfaW52YWxpZFs6XQogICAgICAgICMgZ2V0IG5leHQgcG9zaXRpb24KICAgICAgICAjIHJlbW92ZSBhIHZhbGlkIHBvc2l0aW9uCiAgICAgICAgaWR4ID0gcmFuZHJhbmdlKGxlbihpbnZhbGlkKS0xKQogICAgICAgIHdoaWxlIGludmFsaWRbaWR4XToKICAgICAgICAgICAgaWR4ID0gcmFuZHJhbmdlKGxlbihpbnZhbGlkKS0xKQogICAgICAgIGludmFsaWRbaWR4XT1UcnVlCiAgICAgICAgIyB3aGlsZSBlbmQgaXMgbm90IHJlYWNoYWJsZSAKICAgICAgICAjIGFkZCB0byB2YWxpZCBwb3NpdGlvbnMKICAgICAgICBsZXZlbD1zdGFydF9zZWFyY2goaW52YWxpZCkKICAgICAgICB3aGlsZSBsZXZlbDwwOgogICAgICAgICAgICBpZHg9cmFuZHJhbmdlKGxlbihpbnZhbGlkKS0xKQogICAgICAgICAgICB3aGlsZSBub3QgaW52YWxpZFtpZHhdOgogICAgICAgICAgICAgICAgaWR4PXJhbmRyYW5nZShsZW4oaW52YWxpZCktMSkKICAgICAgICAgICAgaW52YWxpZFtpZHhdPUZhbHNlCiAgICAgICAgICAgIGxldmVsPXN0YXJ0X3NlYXJjaChpbnZhbGlkKQoKICAgICAgICBpZiBsZXZlbD5tYXhfbGV2ZWw6CiAgICAgICAgICAgIG1heF9sZXZlbD1sZXZlbAogICAgICAgICAgICBzb2x1dGlvbj1ib29sMmhleChpbnZhbGlkKQogICAgICAgICAgICBwcmludChtYXhfbGV2ZWwsZm9ybWF0KHRpbWUoKS1zdGFydHRpbWUsJzguMmYnKSwgZm9ybWF0KGNudCwnMTBkJyksZm9ybWF0KFQsJzEwLjVmJyksZm9ybWF0KGV4cCgoLTIpL1QpLCc0LjJmJyksc29sdXRpb24pCgogICAgICAgIGlmIGxldmVsPj1wcmV2X2xldmVsOgogICAgICAgICAgICBwcmV2X2ludmFsaWQ9aW52YWxpZAogICAgICAgICAgICBwcmV2X2xldmVsPWxldmVsCiAgICAgICAgZWxzZToKICAgICAgICAgICAgcD1leHAoKGxldmVsLXByZXZfbGV2ZWwpL1QpCiAgICAgICAgICAgICNwcmludChwLCBsZXZlbCxwcmV2X2xldmVsKQogICAgICAgICAgICBpZiByYW5kb20oKTxwOgogICAgICAgICAgICAgICAgcHJldl9pbnZhbGlkPWludmFsaWQKICAgICAgICAgICAgICAgIHByZXZfbGV2ZWw9bGV2ZWwKICAgICAgICBpZiAnUFJJTlRfR1JBUEgnIGluIGdsb2JhbHMoKToKICAgICAgICAgICAgZ2w9KChbJyAnXSo5KStbJ3wnXSkqNwogICAgICAgICAgICBnbFtwcmV2X2xldmVsXT0nKicKICAgICAgICAgICAgZ2xbbWF4X2xldmVsXT0nPicKICAgICAgICAgICAgcHJpbnQoJycuam9pbihnbCkrZm9ybWF0KGV4cCgoLTIpL1QpLCc0LjJmJykpCiAgICAgICAgICAgICNwcmludCgoJyAnKnByZXZfbGV2ZWwpKycqJykKICAgICAgICBpZiAnUFJJTlRfQ1VSUkhFWCcgaW4gZ2xvYmFscygpOgogICAgICAgICAgICBwcmludChib29sMmhleChwcmV2X2ludmFsaWQpKQogICAgICAgIFQ9KDEtZGVsdGEpKlQKICAgICAgICAjcHJpbnQobWF4X2xldmVsLFQpCiAgICAgICAgaWYgVDxUZW5kOgogICAgICAgICAgICBwcmludCgnZmluaWhzaGVkIGFmdGVyJywgdGltZSgpLXN0YXJ0dGltZSkKICAgICAgICAgICAgcmV0dXJuIHNvbHV0aW9uCgogICAgcmV0dXJuIHNvbHV0aW9uCgoKCgojcHJpbnQoYW5uZWFsKCcyZWI3ZGI1ZmZkZmI3ZjdkZjdkZmYzZGI3N2Y5NmVmZCcsMC42LCAuMDEsIDAuMDAwMDAxLHJlY292ZXI9RmFsc2UpKQoKdjE9dmVydGV4KHsxLDMsNH0sMCkKdjI9dmVydGV4KHsyLDMsNH0sMSkKdjEuY29ubmVjdHN0byh2MikKCmlkeHN0cj0nNzZhZjQ1ZmYnCmE9aGV4MmJvb2woaWR4c3RyKQp1PWdldF91bml2ZXJzZShhKQphZG09Z2V0X2FkbWlzc2libGUoYSwgdSkKZz1ncmFwaCh1LGEpCnByaW50KGcp"},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# für Elsi
# https://simonhessner.de/calculate-power-set-set-of-all-subsets-in-python-without-recursion/
print("version 2.1")
from itertools import combinations, compress
from math import log, exp
from random import choice, randrange, random, sample, seed
from time import time
from datetime import datetime
import json
#https://repl.it/talk/share/NEW-Python-Lib-for-wwwjsonstoreio-DONT-LOOSE-YOUR-D-AGAIN-WITH-JSON-STORE-NEW/10956
RUN_REPL=True
RUN_REPL=False

if RUN_REPL:
    import json_store_client

jsonStoreToken='https://www.jsonstore.io/6ba023421dcadbd1aeea4f6bbd6549400609804c4309c1fa19f03d739302a2df'

if RUN_REPL:
    client = json_store_client.Client(jsonStoreToken)

FILE_NAME_1='statefile1'
FILE_NAME_2='statefile2'
save_to_first_file=True

PRINT_DEBUG = False
#PRINT_GRAPH=1
MAX_SEED=1000000000
'''
the hex number must start with a hex digit lower or equal to 7,
so the last element of the bool array is False
'''



def write_state(state_dict):
    global save_to_first_file
    if save_to_first_file:
        file_name=FILE_NAME_1
    else:
        file_name=FILE_NAME_2
    save_to_first_file=not save_to_first_file
    save_dict={
        'savetime': datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        'state':state_dict
    }
    if RUN_REPL:
        client.store(file_name,save_dict)
    else:
        with open(file_name+'.txt','w') as f:
            f.write(json.dumps(save_dict))
    #print("write file ",file_name)
    #print(save_dict)

def read_state():
    save_dict_1=None
    save_dict_2=None
    try:    
        if RUN_REPL:
            save_dict_1=client.retrieve(FILE_NAME_1)
        else:
            with open(FILE_NAME_1+'.txt','r') as f:
                save_dict_1=json.loads(f.read())
        save_time_1=save_dict_1["savetime"]
        state_dict_1=save_dict_1["state"]
        state_ready_1=(type(save_dict_1)==type({}))
    except:
        print("state_file_1 error")
        state_ready_1=False

    try:
        if RUN_REPL:
            save_dict_2=client.retrieve(FILE_NAME_2)
        else:
            with open(FILE_NAME_2+'.txt','r') as f:
                save_dict_2=json.loads(f.read())
        save_time_2=save_dict_2["savetime"]
        state_dict_2=save_dict_2["state"]
        state_ready_2=(type(save_dict_2)==type({}))
    except:
        print("state_file_2 error")
        state_ready_2=False
        
    if state_ready_1 and state_ready_2:
        if save_time_2>=save_time_1:
            next_state=2
        else:
            next_state=1
    elif state_ready_1:
        next_state=1
    elif state_ready_2:
        next_state=2
    else:
        next_state=0
    if next_state==0:
        return {}
    elif next_state==1:
        return state_dict_1
    elif next_state==2:
        return state_dict_2

def hex2bool(hexstr):
    return [i == '1' for i in list(reversed(format(int(hexstr, 16), 
            '0'+str(4*len(hexstr))+'b')))]


def bool2hex(boolarr):
    s = 0
    for t in reversed(boolarr):
        s *= 2
        if t:
            s += 1
    return format(s, '0'+str(len(boolarr)//4)+'x')


def get_subsets(fullset):
    listrep = list(fullset)
    n = len(listrep)
    return [[listrep[k] for k in range(n) if i & 1 << k] for i in range(2**n)]


bank_name = ['start', 'end']


def next_node(boundary, universe, admissible):
    for people in boundary:
        for people_but_1 in combinations(people, len(people)-1):
            people_but_1 = set(people_but_1)
            if people_but_1 in admissible:
                yield universe.difference(people_but_1)
        if len(people) >= 2:
            for people_but_2 in combinations(people,len(people)-2):
                people_but_2=set(people_but_2)
                if people_but_2 in admissible:
                    yield universe.difference(people_but_2)

def next_nodeX(boundary, universe, admissible):
    for people in boundary:
        for people_but_1 in combinations(people, len(people)-1):
            people_but_1 = set(people_but_1)
            if people_but_1 in admissible:
                yield (people,universe.difference(people_but_1))
        if len(people) >= 2:
            for people_but_2 in combinations(people,len(people)-2):
                people_but_2=set(people_but_2)
                if people_but_2 in admissible:
                    yield (people,universe.difference(people_but_2))


def print_debug(bankname,level):
    if PRINT_DEBUG:
        print('==> debug', bankname, level)

def print_iter(ss,descr):
    if PRINT_DEBUG:
        print(descr)
        if ss is None:
            print(None)
        else:
            if ss:
                for s in ss:
                    print({c[t] for t in s})
            else:
                print('empty')
class vertex:
    NoLevel=-1
    def __init__(self, subset, bank):
        self.subset=subset
        self.bank=bank
        self.succ=set()
        self.pred=set()
        self.level=vertex.NoLevel

    def connectsto(self, other):
        self.succ.add(other)
        other.pred.add(self)

class graph:
    def __init__(self, universe, excludebase):
        self.subsets={}
        self.vertexes=set()
        self.excludebase=excludebase
        self.startnode=None
        self.endnode=None
        self.universe=frozenset(universe)
        for s in compress(get_subsets(universe), [not t for t in excludebase]):
            self.add_node(frozenset(s))

    def add_node(self, subset):
        [v0, v1,v2, v3]=self._add_vertexes(subset)
        self._connect_vertexes(v0,v1,v2,v3)
        


    def del_node(self, subset):
        pass

        

    def _add_vertexes(self, subset):
        s=frozenset(subset)
        c=self.universe.difference(s)
        v0=vertex(s, 0)
        v1=vertex(s, 1)
        v2=vertex(c,0)
        v3=vertex(c,1)
        if subset==set():
            self.startnode=v2
            self.endnode=v1
        self.subsets[s]=[v0,v1]
        self.subsets[c]=[v2,v3]
        self.vertexes.add(v0)
        self.vertexes.add(v1)
        self.vertexes.add(v2)
        self.vertexes.add(v3)
        return (v0,v1,v2,v3)

    def _remove_vertexes(self, v0, v1, v2, v3):
        assert(v0.subset==v1.subset)
        assert(v2.subset==v3.subset)
        assert(v0.subset==self.universe.difference(v2.subset))
        assert(v0.bank==0)
        assert(v1.bank==1)
        assert(v2.bank==0)
        assert(v3.bank==1)
        del self.subsets[v0.subset]
        del self.subsets[v2.subset]
        del self.vertexes[v0]
        del self.vertexes[v1]
        del self.vertexes[v2]
        del self.vertexes[v3]
        

    def _connect_vertexes(self, v0, v1, v2, v3):
        assert(v0.subset==v1.subset)
        assert(v2.subset==v3.subset)
        assert(v0.subset==self.universe.difference(v2.subset))
        assert(v0.bank==0)
        assert(v1.bank==1)
        assert(v2.bank==0)
        assert(v3.bank==1)
        for (to_vertex_0, to_vertex_1, complement) in [(v0, v1, v2.subset), (v2, v3, v0.subset)]:
            for i in [1,2]:
                if len(to_vertex_0.subset)>=i:
                    for prevset in combinations(to_vertex_0.subset, len(to_vertex_0.subset)-i):
                        node=self.subsets.get(prevset)
                        if node:
                            node[0].connectsto(to_vertex_0)
                            node[1].connectsto(to_vertex_1)
                if len(complement)>=i:
                    for transfer in combinations(complement, i):
                        nextset=to_vertex_0.subset|set(transfer)
                        node=self.subsets.get(nextset)
                        if node:
                            to_vertex_0.connectsto(node[0])
                            to_vertex_1.connectsto(node[1])
        return (v0, v1, v2, v3)

    def _disconnect_vertexes(self, v0, v1, v2, v3):
        assert(v0.subset==v1.subset)
        assert(v2.subset==v3.subset)
        assert(v0.subset==self.universe.difference(v2.subset))
        assert(v0.bank==0)
        assert(v1.bank==1)
        assert(v2.bank==0)
        assert(v3.bank==1)
        for v in [v0, v1, v2, v3]:
            for w in v.succ:
                w.pred.remove(v)
            v.succ=[]
            for w in v.pred:
                w.subset.remove(v)
            v.pred=[]


def search(universe, admissible, boundary=None, this_bank=None, other_bank=None, bank=0, level=0, start=False ):
    print_debug(bank_name[bank], level)
    print_iter(this_bank,'this bank')
    print_iter(other_bank,'other_bank')
    print_iter(boundary,'boundary')
    if start:
        if universe in admissible:
            return search(universe, admissible, [universe], set([universe]), set(frozenset([])), 0,  0)
        else:
            return -2
    else:
        level += 1
        next_boundary = []
        for people in next_node(boundary, universe, admissible):
            if people not in other_bank:
                if people == universe:
                    return level
                next_boundary.append(people)
                other_bank.add(people)
        print_iter(next_boundary, 'next_boundary')
        if next_boundary:
            return search(universe, admissible, next_boundary, other_bank,
            this_bank, 1-0, level)
        else:
            return -1

def searchX(universe, admissible, boundary=None, this_bank=None, other_bank=None, bank=0, level=0, start=False ):
    print_debug(bank_name[bank], level)
    print_iter(this_bank,'this bank')
    print_iter(other_bank,'other_bank')
    print_iter(boundary,'boundary')
    if start:
        if universe in admissible:
            return searchX(universe, admissible, [universe], set([universe]), set(frozenset([])), 0,  [])
        else:
            return -2
    else:
        level.append([])
        next_boundary = []
        for edge in next_nodeX(boundary, universe, admissible):
            level[-1].append(edge)
            if edge[1] not in other_bank:
                if edge[1] == universe:
                    return level
                next_boundary.append(edge[1])
                other_bank.add(edge[1])
        print_iter(next_boundary, 'next_boundary')
        if next_boundary:
            return searchX(universe, admissible, next_boundary, other_bank,
            this_bank, 1-0, level)
        else:
            return []

def get_max_element(boolarr):
    return int(log(len(boolarr), 2))+1


def get_universe(boolarr):
    return frozenset(range(1, get_max_element(boolarr)+1))


def get_admissible(boolarr, universe):
    admissible = set()
    ps = compress(get_subsets(universe), [not t for t in boolarr])
    for s in ps:
        s.append(get_max_element(boolarr))
        admissible.add(frozenset(s))
        admissible.add(universe.difference(s))

    return(admissible)


def print_admissible(idxstr):
    print(idxstr)
    boolarr=hex2bool(idxstr)
    universe=get_universe(boolarr)
    for s in sorted(get_admissible(boolarr, universe)):
        print(sorted([t for t in s]))

def get_path(idxstr):
    ll=start_searchX(idxstr)
    ll.reverse()
    path=[get_universe(hex2bool(idxstr))]
    for edgelist in ll:
        for edge in edgelist:
            if edge[1]==path[-1]:
                path.append(edge[0])
                break
    path.reverse()
    return [set(p) for p in path]

def print_path(idxstr):
    universe=get_universe(hex2bool(idxstr))
    fullen=3*len(universe)
    startbank=True
    ll=get_path(idxstr)
    for k,p in enumerate(ll[:-1]):
        l1=list(p)
        l1.sort()
        l2=list(universe-p)
        l2.sort()
        l3=list(p-(universe-ll[k+1]))
        l3.sort()
        if startbank:
            line=(format(k+1,'2d')+'. '+format(str(l1),str(fullen)+'s')
            +format(' >-'+str(l3)+'-> ','12s')
            +format(str(l2),'s'))
        else:
            line=(format(k+1,'2d')+'. '+format(str(l2),str(fullen)+'s')
            +format(' <-'+str(l3)+'-< ','12s')
            +format(str(l1),'s'))
            #line=str(set(universe-p))+' <- '+str(p)
        startbank=not startbank
        print(line)
    

def start_search(boolarr):
    universe = get_universe(boolarr)
    admissible = get_admissible(boolarr, universe)
    return search(universe, admissible, start=True )

def start_searchX(idxstr):
    boolarr=hex2bool(idxstr)
    universe = get_universe(boolarr)
    admissible = get_admissible(boolarr, universe)
    return searchX(universe, admissible, start=True )



def anneal(startstr, Tstart, Tend, delta, myseed=None, recover=None):
    CNT_DIFF=3000
    def get_state():
        return {
            "seed":randrange(MAX_SEED),
            "Tstart":Tstart,
            "Tend":Tend,
            "T":T,
            "prev_level":prev_level,
            "prev_invalid":bool2hex(prev_invalid),
            "max_level": max_level,
            "solution": solution
        }
    if myseed is None:
        myseed = randrange(MAX_SEED)
    seed(myseed)
    print('anneal', Tstart, Tend, delta)
    print(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
    print('seed', myseed)
    prev_invalid = hex2bool(startstr)
    prev_level = start_search(prev_invalid)
    T = Tstart
    max_level = 0
    level = 0
    starttime = time()
    solution = None
    cnt=0
    last_cnt_check=0
    if recover:
        mystate=read_state()
        if mystate:
            print("recover state")
            seed(mystate['seed'])
            T=float(mystate['T'])
            Tend=float(mystate['Tend'])
            prev_level=int(mystate['prev_level'])
            max_level=int(mystate['max_level'])
            solution=mystate['solution']
            solution=mystate['solution']
            prev_invalid=hex2bool(mystate['prev_invalid'])

    while True:
        cnt+=1
        if cnt-last_cnt_check>CNT_DIFF:
            last_cnt_check=cnt
            #seed = randrange(MAX_SEED)
            mystate=get_state()
            seed(mystate['seed'])
            write_state(get_state())

            if 'PRINT_STATE' in globals():
                print('state',myseed, T, bool2hex(prev_invalid))


        invalid = prev_invalid[:]
        # get next position
        # remove a valid position
        idx = randrange(len(invalid)-1)
        while invalid[idx]:
            idx = randrange(len(invalid)-1)
        invalid[idx]=True
        # while end is not reachable 
        # add to valid positions
        level=start_search(invalid)
        while level<0:
            idx=randrange(len(invalid)-1)
            while not invalid[idx]:
                idx=randrange(len(invalid)-1)
            invalid[idx]=False
            level=start_search(invalid)

        if level>max_level:
            max_level=level
            solution=bool2hex(invalid)
            print(max_level,format(time()-starttime,'8.2f'), format(cnt,'10d'),format(T,'10.5f'),format(exp((-2)/T),'4.2f'),solution)

        if level>=prev_level:
            prev_invalid=invalid
            prev_level=level
        else:
            p=exp((level-prev_level)/T)
            #print(p, level,prev_level)
            if random()<p:
                prev_invalid=invalid
                prev_level=level
        if 'PRINT_GRAPH' in globals():
            gl=(([' ']*9)+['|'])*7
            gl[prev_level]='*'
            gl[max_level]='>'
            print(''.join(gl)+format(exp((-2)/T),'4.2f'))
            #print((' '*prev_level)+'*')
        if 'PRINT_CURRHEX' in globals():
            print(bool2hex(prev_invalid))
        T=(1-delta)*T
        #print(max_level,T)
        if T<Tend:
            print('finihshed after', time()-starttime)
            return solution

    return solution




#print(anneal('2eb7db5ffdfb7f7df7dff3db77f96efd',0.6, .01, 0.000001,recover=False))

v1=vertex({1,3,4},0)
v2=vertex({2,3,4},1)
v1.connectsto(v2)

idxstr='76af45ff'
a=hex2bool(idxstr)
u=get_universe(a)
adm=get_admissible(a, u)
g=graph(u,a)
print(g)