File tree 4 files changed +207
-0
lines changed
4 files changed +207
-0
lines changed Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ import re
4
+ import sys
5
+
6
+ instructions = [insn .strip () for insn in open ('2.input' ).readlines ()]
7
+
8
+ instruction_format = re .compile ("(?P<inst>forward|down|up) (?P<oper>[0-9]+)" )
9
+
10
+
11
+ class Submarine :
12
+ def __init__ (self ):
13
+ self .horiz_pos = 0
14
+ self .depth = 0
15
+
16
+ def forward (self , steps ):
17
+ self .horiz_pos += steps
18
+
19
+ def down (self , steps ):
20
+ self .depth += steps
21
+
22
+ def up (self , steps ):
23
+ self .depth -= steps
24
+ if self .depth < 0 :
25
+ print (f"Illegal direction: up { steps } " )
26
+ sys .exit ()
27
+
28
+ def __str__ (self ):
29
+ return f"<{ self .__class__ .__name__ } : ({ self .horiz_pos } , { self .depth } )>"
30
+
31
+
32
+ sub = Submarine ()
33
+
34
+ for insn in instructions :
35
+ m = instruction_format .match (insn )
36
+ if m ['inst' ] == 'forward' :
37
+ sub .forward (int (m ['oper' ]))
38
+ elif m ['inst' ] == 'down' :
39
+ sub .down (int (m ['oper' ]))
40
+ elif m ['inst' ] == 'up' :
41
+ sub .up (int (m ['oper' ]))
42
+ else :
43
+ print (f"Illegal instruction: { insn } " )
44
+ sys .exit ()
45
+
46
+ print (f"{ sub .horiz_pos * sub .depth } " )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ instructions = File . read ( '2.input' ) . lines . map ( &:strip )
4
+
5
+ instruction_format = /(?<inst>forward|down|up) (?<oper>[[:digit:]]+)/
6
+
7
+ class Submarine
8
+ attr_accessor :horiz_pos
9
+ attr_accessor :depth
10
+
11
+ def initialize
12
+ @horiz_pos = 0
13
+ @depth = 0
14
+ end
15
+
16
+ def forward ( steps )
17
+ @horiz_pos += steps
18
+ end
19
+
20
+ def down ( steps )
21
+ @depth += steps
22
+ end
23
+
24
+ def up ( steps )
25
+ @depth -= steps
26
+ if @depth . negative?
27
+ print "Illegal instruction: up #{ steps } "
28
+ exit
29
+ end
30
+ end
31
+
32
+ def to_s
33
+ "<#{ self . class } : (#{ @horiz_pos } , #{ @depth } )>"
34
+ end
35
+ end
36
+
37
+ sub = Submarine . new
38
+
39
+ instructions . each do |insn |
40
+ instruction_format . match ( insn ) do |m |
41
+ case m [ 'inst' ]
42
+ when 'forward'
43
+ sub . forward ( m [ 'oper' ] . to_i )
44
+ when 'down'
45
+ sub . down ( m [ 'oper' ] . to_i )
46
+ when 'up'
47
+ sub . up ( m [ 'oper' ] . to_i )
48
+ else
49
+ print "Illegal instruction: #{ insn } \n "
50
+ exit
51
+ end
52
+ end
53
+ end
54
+
55
+ print "#{ sub . horiz_pos * sub . depth } \n "
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env python
2
+
3
+ import re
4
+ import sys
5
+
6
+ instructions = [insn .strip () for insn in open ('2.input' ).readlines ()]
7
+
8
+ instruction_format = re .compile ("(?P<inst>forward|down|up) (?P<oper>[0-9]+)" )
9
+
10
+
11
+ class Submarine :
12
+ def __init__ (self ):
13
+ self .horiz_pos = 0
14
+ self .depth = 0
15
+ self .aim = 0
16
+
17
+ def forward (self , steps ):
18
+ self .horiz_pos += steps
19
+ self .depth += self .aim * steps
20
+ if self .depth < 0 :
21
+ print (f"Illegal direction: forward { steps } " )
22
+ sys .exit ()
23
+
24
+ def down (self , steps ):
25
+ self .aim += steps
26
+
27
+ def up (self , steps ):
28
+ self .aim -= steps
29
+
30
+ def __str__ (self ):
31
+ return f"<{ self .__class__ .__name__ } : ({ self .horiz_pos } , { self .depth } , { self .aim } )>"
32
+
33
+
34
+ sub = Submarine ()
35
+
36
+ for insn in instructions :
37
+ m = instruction_format .match (insn )
38
+ if m ['inst' ] == 'forward' :
39
+ sub .forward (int (m ['oper' ]))
40
+ elif m ['inst' ] == 'down' :
41
+ sub .down (int (m ['oper' ]))
42
+ elif m ['inst' ] == 'up' :
43
+ sub .up (int (m ['oper' ]))
44
+ else :
45
+ print (f"Illegal instruction: { insn } " )
46
+ sys .exit ()
47
+
48
+ print (f"{ sub .horiz_pos * sub .depth } " )
Original file line number Diff line number Diff line change
1
+ #!/usr/bin/env ruby
2
+
3
+ instructions = File . read ( '2.input' ) . lines . map ( &:strip )
4
+
5
+ instruction_format = /(?<inst>forward|down|up) (?<oper>[[:digit:]]+)/
6
+
7
+ class Submarine
8
+ attr_accessor :horiz_pos
9
+ attr_accessor :depth
10
+ attr_accessor :aim
11
+
12
+ def initialize
13
+ @horiz_pos = 0
14
+ @depth = 0
15
+ @aim = 0
16
+ end
17
+
18
+ def forward ( steps )
19
+ @horiz_pos += steps
20
+ @depth += @aim * steps
21
+ if @depth . negative?
22
+ print "Illegal instruction: forward #{ steps } "
23
+ exit
24
+ end
25
+ end
26
+
27
+ def down ( steps )
28
+ @aim += steps
29
+ end
30
+
31
+ def up ( steps )
32
+ @aim -= steps
33
+ end
34
+
35
+ def to_s
36
+ "<#{ self . class } : (#{ @horiz_pos } , #{ @depth } , #{ @aim } )>"
37
+ end
38
+ end
39
+
40
+ sub = Submarine . new
41
+
42
+ instructions . each do |insn |
43
+ instruction_format . match ( insn ) do |m |
44
+ case m [ 'inst' ]
45
+ when 'forward'
46
+ sub . forward ( m [ 'oper' ] . to_i )
47
+ when 'down'
48
+ sub . down ( m [ 'oper' ] . to_i )
49
+ when 'up'
50
+ sub . up ( m [ 'oper' ] . to_i )
51
+ else
52
+ print "Illegal instruction: #{ insn } \n "
53
+ exit
54
+ end
55
+ end
56
+ end
57
+
58
+ print "#{ sub . horiz_pos * sub . depth } \n "
You can’t perform that action at this time.
0 commit comments