Slide 1

Slide 1 text

CodeGolf

Slide 2

Slide 2 text

Create something in 1024 characters

Slide 3

Slide 3 text

Create something in 140 characters

Slide 4

Slide 4 text

Create something

Slide 5

Slide 5 text

Constraints stimulate Creativity

Slide 6

Slide 6 text

https://www.youtube.com/watch?v=Ugz_oOWOzlM&feature=youtu.be

Slide 7

Slide 7 text

(f=proc{|x,y|sleep 1;puts "\033c",x;f[x.gsub(/\/\ \./,'~/\\').gsub(/.o./,y ? '\o/':'|o|'),!y]})['~/\ \'+'~'*12+'o~~']

Slide 8

Slide 8 text

116 characters …but what do they mean?

Slide 9

Slide 9 text

☺ cat lucas.md # Lucas Dohmen * Consultant @ innoQ GmbH * From Cologne, Germany * Open Source Developer * Podcaster * CoderDojo Cologne Organizer * Code Golfer

Slide 10

Slide 10 text

(f=proc{|x,y|sleep 1;puts "\033c",x;f[x.gsub(/\/\ \./,'~/\\').gsub(/.o./,y ? '\o/':'|o|'),!y]})['~/\ \'+'~'*12+'o~~']

Slide 11

Slide 11 text

Recursion using a Proc (f=proc{|x,y| sleep 1 system 'clear' puts x f[…] })[…]

Slide 12

Slide 12 text

>> f = proc { |a,b| puts a } => # >> f['hello'] hello => nil Calling a Proc

Slide 13

Slide 13 text

>> f = proc { |a,b| puts a } => # >> f['hello'] hello => nil Arguments that were not provided are initialised as nil (not the case for a lambda) Calling a Proc

Slide 14

Slide 14 text

>> f = proc { |a,b| puts a } => # >> f['hello'] hello => nil Arguments that were not provided are initialised as nil (not the case for a lambda) Calling a Proc

Slide 15

Slide 15 text

First time we call it… '~/\\'+'~'*12+'o~~'

Slide 16

Slide 16 text

'~/\\'+'~'*12+'o~~' => '~/\~~~~~~~~~~~~o~~' First time we call it…

Slide 17

Slide 17 text

On every call from now on we will do two things 1. Move the shark 2. Change the arms

Slide 18

Slide 18 text

Moving the shark x.gsub(/\/\\./,'~/\\')

Slide 19

Slide 19 text

Moving the shark x.gsub(/\/\\./,'~/\\')

Slide 20

Slide 20 text

Moving the shark x.gsub(/\/\\./,'~/\\') /\

Slide 21

Slide 21 text

Moving the shark x.gsub(/\/\\./,'~/\\')

Slide 22

Slide 22 text

Moving the shark x.gsub(/\/\\./,'~/\\') remove one character from the right

Slide 23

Slide 23 text

Moving the shark x.gsub(/\/\\./,'~/\\')

Slide 24

Slide 24 text

Moving the shark x.gsub(/\/\\./,'~/\\') add one wave to the left

Slide 25

Slide 25 text

~/\~~~~~~~~~~~~~~~ ~~/\~~~~~~~~~~~~~~ ~~~/\~~~~~~~~~~~~~ ~~~~/\~~~~~~~~~~~~ Moving the shark

Slide 26

Slide 26 text

Changing the arms .gsub(/.o./,y ? '\o/':'|o|')

Slide 27

Slide 27 text

matches all stages ~o~ \o/ |o| of the animation Changing the arms .gsub(/.o./,y ? '\o/':'|o|')

Slide 28

Slide 28 text

depending on y we change it to either \o/ or |o| Changing the arms .gsub(/.o./,y ? '\o/':'|o|')

Slide 29

Slide 29 text

Changing the arms (f=proc{|x,y| sleep 1 system 'clear' puts x f[…,!y] })[…]

Slide 30

Slide 30 text

(f=proc{|x,y|sleep 1;puts "\033c",x;f[x.gsub(/\/\ \./,'~/\\').gsub(/.o./,y ? '\o/':'|o|'),!y]})['~/\ \'+'~'*12+'o~~']

Slide 31

Slide 31 text

https://www.youtube.com/watch?v=d68x-RI-yqw&feature=youtu.be

Slide 32

Slide 32 text

This app has one main trick: The list is 1- indexed

Slide 33

Slide 33 text

If you enter a line it will be stored as 1. a string 2. a fixnum

Slide 34

Slide 34 text

If the fixnum is 0 it is a todo item otherwise it is a check-off action

Slide 35

Slide 35 text

'Clean the house'.to_i #=> 0 '12'.to_i #=> 12 '0'.to_i #=> 0

Slide 36

Slide 36 text

There is no item 0, does not matter 'Clean the house'.to_i #=> 0 '12'.to_i #=> 12 '0'.to_i #=> 0

Slide 37

Slide 37 text

todo_list = [] loop do input_as_string = gets input_as_int = input_as_string.to_i system "clear" if input_as_int > 0 todo_list.delete_at(input_as_int - 1) else todo_list << input_as_string end todo_list.each_with_index do |item, index| puts "#{index+1}: #{item}" end end

Slide 38

Slide 38 text

Ok, that’s pretty short. Now let’s golf it.

Slide 39

Slide 39 text

input_as_string=gets;input_as_int=input_as_string.to_i input_as_int=(input_as_string=gets).to_i

Slide 40

Slide 40 text

input_as_string=gets;input_as_int=input_as_string.to_i input_as_int=(input_as_string=gets).to_i -14

Slide 41

Slide 41 text

Use ternary if/else instead of if/else

Slide 42

Slide 42 text

todo_list.each_with_index do |item, index| puts "#{index+1}: #{item}" end

Slide 43

Slide 43 text

Way too long… todo_list.each_with_index do |item, index| puts "#{index+1}: #{item}" end

Slide 44

Slide 44 text

todo_list.each_with_index{|item, index| puts "#{index+1}: #{item}"}

Slide 45

Slide 45 text

todo_list.each_with_index{|item, index| puts "#{index+1}: #{item}"} index=0;todo_list.each{|item|puts "#{index+=1}: #{item}"}

Slide 46

Slide 46 text

todo_list.each_with_index{|item, index| puts "#{index+1}: #{item}"} index=0;todo_list.each{|item|puts "#{index+=1}: #{item}"} index=0;todo_list.map{|item|puts "#{index+=1}: #{item}"}

Slide 47

Slide 47 text

todo_list.each_with_index{|item, index| puts "#{index+1}: #{item}"} index=0;todo_list.each{|item|puts "#{index+=1}: #{item}"} index=0;todo_list.map{|item|puts "#{index+=1}: #{item}"} -11

Slide 48

Slide 48 text

todo_list = [] loop do input_as_int=(input_as_string=gets).to_i system "clear" input_as_int>0?todo_list.delete_at(input_as_int-1):todo_list<

Slide 49

Slide 49 text

Replace names & save whitespace 103 characters t=[] loop{i,j=(s=gets).to_i,0 system "clear" i>0?t.delete_at(i-1):t<

Slide 50

Slide 50 text

bonus round

Slide 51

Slide 51 text

delete_at is way too long t=[] loop{i,j=(s=gets).to_i,0 system "clear" i>0?t.delete_at(i-1):t<

Slide 52

Slide 52 text

t.delete_at(i-1) t=t-[t[i-1]] t-=[t[i-1]]

Slide 53

Slide 53 text

t.delete_at(i-1) t=t-[t[i-1]] t-=[t[i-1]] -5

Slide 54

Slide 54 text

t=[] loop{i,j=(s=gets).to_i,0 system "clear" i>0?t-=[t[i-1]]:t<

Slide 55

Slide 55 text

t=[] loop{i,j=(s=gets).to_i,0 system "clear" i>0?t-=[t[i-1]]:t<

Slide 56

Slide 56 text

system "clear" puts "\033c"

Slide 57

Slide 57 text

system "clear" puts "\033c" -2

Slide 58

Slide 58 text

t=[] loop{i,j=(s=gets).to_i,0 puts "\033c" i>0?t-=[t[i-1]]:t<

Slide 59

Slide 59 text

t=[] loop{i,j=(s=gets).to_i,0 puts "\033c" i>0?t-=[t[i-1]]:t<

Slide 60

Slide 60 text

DRY

Slide 61

Slide 61 text

DRY

Slide 62

Slide 62 text

t=[] loop{i,j=(s=gets).to_i,0 puts "\033c" i>0?t-=[t[i-1]]:t<

Slide 63

Slide 63 text

t=[] loop{i,j=(s=gets).to_i,0 i>0?t-=[t[i-1]]:t<

Slide 64

Slide 64 text

puts "\033c";t.map{|g|puts "#{j+=1}: #{g}"} puts "\033c",t.map{|g|"#{j+=1}: #{g}"}

Slide 65

Slide 65 text

puts "\033c";t.map{|g|puts "#{j+=1}: #{g}"} puts "\033c",t.map{|g|"#{j+=1}: #{g}"} -5

Slide 66

Slide 66 text

t=[] loop{i,j=(s=gets).to_i,0 i>0?t-=[t[i-1]]:t<

Slide 67

Slide 67 text

$_

Slide 68

Slide 68 text

t=[] loop{i,j=gets.to_i,0 i>0?t-=[t[i-1]]:t<<$_ puts "\033c",t.map{|g|"#{j+=1}: #{g}"}}

Slide 69

Slide 69 text

t=[] loop{i,j=gets.to_i,0 i>0?t-=[t[i-1]]:t<<$_ puts "\033c",t.map{|g|"#{j+=1}: #{g}"}} -3

Slide 70

Slide 70 text

88 characters t=[] loop{i,j=gets.to_i,0 i>0?t-=[t[i-1]]:t<<$_ puts "\033c",t.map{|g|"#{j+=1}: #{g}"}}

Slide 71

Slide 71 text

88 characters? Now we have space again.

Slide 72

Slide 72 text

88 characters? Now we have space again. A persistent todo app in 129 characters.

Slide 73

Slide 73 text

loop{t=IO.readlines(?a)rescue[] j=0 puts "\033c",t.map{|g|"#{j+=1}: #{g}"} i=gets.to_i i>0?t-=[t[i-1]]:t<<$_ IO.write ?a,t.join}

Slide 74

Slide 74 text

loop{t=IO.readlines(?a)rescue[] j=0 puts "\033c",t.map{|g|"#{j+=1}: #{g}"} i=gets.to_i i>0?t-=[t[i-1]]:t<<$_ IO.write ?a,t.join} +41

Slide 75

Slide 75 text

https://www.youtube.com/watch?v=q5kSdZ7Rb3A&feature=youtu.be

Slide 76

Slide 76 text

Thank you @moonbeamlabs