Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 080c565

Browse files
committed
day22 and day3 2019
1 parent 6baec9b commit 080c565

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed

‎2019/day3/data.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
R1000,D722,L887,D371,R430,D952,R168,D541,L972,D594,R377,U890,R544,U505,L629,U839,L680,D863,L315,D10,L482,U874,L291,U100,R770,D717,L749,U776,L869,D155,R250,U672,L195,D991,L556,D925,R358,U296,R647,D652,L790,D780,L865,U405,L400,D160,L460,U50,R515,D666,R306,U746,R754,U854,L332,U254,R673,U795,R560,U69,L507,U332,L328,U547,L717,U291,R626,U868,L583,D256,R371,U462,R793,U559,L571,U270,R738,U425,L231,U549,L465,U21,L647,U43,R847,U104,L699,U378,L549,D975,R13,D306,R532,D730,L566,U846,L903,D224,R448,D424,L727,D199,L626,D872,L541,D786,L304,U462,R347,U379,R29,D556,L775,D768,L284,D480,R654,D659,R818,D57,L77,U140,R619,D148,R686,D461,L910,U244,R115,D769,R968,U802,L737,U868,R399,D150,L791,U579,L856,D11,R115,U522,L443,D575,L133,U750,R437,U718,L79,D119,L97,U471,R817,U438,R157,U105,L219,U777,L965,U687,L906,D744,L983,D350,R664,D917,R431,D721,L153,U757,L665,U526,L49,U166,L59,D293,R962,D764,R538,U519,L24,U91,R11,U574,L647,U891,R44,D897,L715,U498,L624,D573,R287,U762,L613,D79,R122,U148,L849,D385,R792,D20,L512,D431,R818,U428,L10,D800,R773,D936,L594,D38,R824,D216,L220,U358,L463,U550,R968,D346,L658,U113,R813,U411,L730,D84,R479,U877,L730,D961,L839,D792,R424,U321,L105,D862,L815,D243,L521,D913,L1,D513,L269,U495,L27,U16,R904,D926,R640,U948,R346,D240,L273,U131,L296,U556,R347,D640,L261,D43,R136,U824,R126,U583,R736,U530,L734,U717,L256,U362,L86,U48,R851,U519,L610,D134,L554,D766,L179,U637,R71,D895,L21,D908,R486,D863,R31,U85,R420,D718,R466,D861,R655,D304,L701,D403,L860,D208,L595,U64,R999
2+
L992,D463,R10,D791,R312,D146,R865,D244,L364,D189,R35,U328,R857,D683,L660,D707,L908,D277,R356,U369,R197,D35,R625,D862,L769,U705,L728,U999,R938,U233,L595,U266,L697,U966,L536,D543,L669,D829,R910,U693,R753,D389,L647,U603,L660,D787,L138,D119,L131,D266,R268,D917,R776,U290,R920,U904,L46,D139,L341,D19,R982,U790,L981,U791,L147,U30,L246,U677,R343,D492,R398,D234,R76,D423,L709,D392,R741,U408,R878,U29,R446,U36,R806,U78,L76,D813,R584,U682,L187,U666,L340,D301,L694,U15,R800,U276,L755,U558,R366,D309,R571,U976,L286,D833,R318,U365,L864,U408,L352,D61,R284,D272,R240,D845,L206,U721,R367,D541,R628,U581,L750,D680,R695,D30,R849,U743,L214,U605,R533,U493,R803,D783,R168,U877,L61,D726,L794,D116,R717,U44,R964,U674,L291,D372,L381,D523,L644,U438,R983,D390,R520,D471,R556,D693,L919,D863,R84,D629,L264,D429,R82,U64,R835,D801,R93,U770,R441,D152,L718,D788,L797,U699,L82,U206,L40,U952,R902,U570,L759,D655,L131,D901,L470,D804,L407,U458,L922,D21,L171,U841,L237,D301,R192,D293,R984,U243,R846,U139,L413,U162,R925,D235,L115,U443,L884,D910,R335,U274,L338,U160,R125,D775,R824,D821,R531,D761,L189,U822,L602,D732,R473,U149,L128,U30,R77,D957,R811,D154,L988,D237,R425,D855,R482,D571,R134,D731,L905,U869,R916,D689,L17,U24,R353,D996,R832,U855,L76,U659,R581,D483,R821,D145,R199,D344,R487,D436,L92,U491,R365,D909,L17,D148,R307,U57,R666,U660,R195,D767,R612,D902,L594,D299,R670,D881,L583,D793,R58,U89,L99,D355,R394,D350,R920,U544,R887,U564,L238,U979,L565,D914,L95,U150,R292,U495,R506,U475,R813,D308,L797,D484,R9

‎2019/day3/data_test.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
R75,D30,R83,U83,L12,D49,R71,U7,L72
2+
U62,R66,U55,R34,D71,R55,D58,R83

‎2019/day3/main.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/ruby
2+
require 'matrix'
3+
require 'test/unit'
4+
require 'pry'
5+
6+
DIRECTIONS = { R: [1, 0], L: [-1, 0], D: [0, 1], U: [0, -1] }
7+
8+
def cells(str)
9+
cells = [{ pos: [0,0], step: 0 }]
10+
str.split(',').map { |action|
11+
direction = DIRECTIONS[action[0].to_sym]
12+
action[1..-1].to_i.times { |i|
13+
pos = cells[-1][:pos].zip(direction).map{ |e| e.sum }
14+
cells << { pos: pos, step: cells[-1][:step] + 1 }
15+
}
16+
}
17+
cells
18+
end
19+
20+
def file_cells(name)
21+
File.read(name).split("\n").map {|c| cells(c)}
22+
end
23+
24+
def resolve_1(name)
25+
cells = file_cells(name).map{ |c| c.map {|l| l[:pos] }}
26+
interset = (cells[1] & cells[0]) - [[0,0]]
27+
interset.map{|pos| pos.map(&:abs).sum }.min
28+
end
29+
30+
def resolve_2(name)
31+
obj = file_cells(name)
32+
cells = obj.map{ |c| c.map {|l| l[:pos] }}
33+
interset = (cells[1] & cells[0]) - [[0,0]]
34+
interset.map do |interset|
35+
obj[0].find {|o| o[:pos] == interset }[:step] + obj[1].find {|o| o[:pos] == interset }[:step]
36+
end.min
37+
end
38+
39+
class Day3Test < Test::Unit::TestCase
40+
def test_cells
41+
assert_equal [[0, 0], [1, 0], [2, 0], [2, 1], [2, 2]], cells('R2,D2').map{ |cell| cell[:pos] }
42+
end
43+
44+
def test_1
45+
assert_equal 159, resolve_1('data_test.txt')
46+
end
47+
48+
def test_1_final
49+
assert_equal 860, resolve_1('data.txt')
50+
end
51+
52+
def test_2
53+
assert_equal 610, resolve_2('data_test.txt')
54+
end
55+
56+
def test_2_final
57+
assert_equal 9238, resolve_2('data.txt')
58+
end
59+
end

‎2019/day4/main.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/ruby
2+
require 'matrix'
3+
require 'test/unit'
4+
require 'pry'
5+
6+
def match?(num)
7+
num.to_s.split(//).each_cons(2).all? {|a,b| a.to_i <= b.to_i } &&
8+
num.to_s.split(//).uniq.count != num.to_s.length
9+
end
10+
11+
def match2?(num)
12+
num.to_s.split(//).each_cons(2).all? {|a,b| a.to_i <= b.to_i } &&
13+
num.to_s.split(//).group_by(&:to_i).values.map(&:count).find {|v| v == 2}
14+
end
15+
16+
class Day4Test < Test::Unit::TestCase
17+
def test_1
18+
refute match?('123789')
19+
refute match?('132789')
20+
assert match?('113789')
21+
assert_equal 1686, (168630..718098).select { |num| match?(num) }.count
22+
end
23+
24+
def test_2
25+
assert match2?('112233')
26+
refute match2?('123444')
27+
assert match2?('111122')
28+
assert_equal 1145, (168630..718098).select { |num| match2?(num) }.count
29+
end
30+
end

‎2020/day22/data.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Player 1:
2+
6
3+
25
4+
8
5+
24
6+
30
7+
46
8+
42
9+
32
10+
27
11+
48
12+
5
13+
2
14+
14
15+
28
16+
37
17+
17
18+
9
19+
22
20+
40
21+
33
22+
3
23+
50
24+
47
25+
19
26+
41
27+
28+
Player 2:
29+
1
30+
18
31+
31
32+
39
33+
16
34+
10
35+
35
36+
29
37+
26
38+
44
39+
21
40+
7
41+
45
42+
4
43+
20
44+
38
45+
15
46+
11
47+
34
48+
36
49+
49
50+
13
51+
23
52+
43
53+
12

‎2020/day22/data_test.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Player 1:
2+
9
3+
2
4+
6
5+
3
6+
1
7+
8+
Player 2:
9+
5
10+
8
11+
4
12+
7
13+
10

‎2020/day22/main.rb

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/ruby
2+
require 'matrix'
3+
require 'test/unit'
4+
require 'pry'
5+
require 'digest'
6+
7+
class Deck
8+
attr_accessor :current, :cards
9+
def initialize(name, cards)
10+
@name = name
11+
@cards = cards
12+
end
13+
14+
def deals
15+
@current = @cards.shift
16+
#puts "#{@name} play #{current}"
17+
@current
18+
end
19+
20+
def win(cards)
21+
#puts "#{@name} win #{cards}"
22+
@cards += cards
23+
end
24+
25+
def debug
26+
#puts "#{@name} decks #{@cards}"
27+
end
28+
29+
def sub_deck
30+
Deck.new(@name, @cards[0...@current])
31+
end
32+
33+
def self.file(data)
34+
name = data.split("\n")[0]
35+
cards = data.split("\n")[1..-1].map(&:to_i)
36+
Deck.new(name, cards)
37+
end
38+
end
39+
40+
class Game
41+
attr_accessor :id, :locked, :lines, :player1, :player2
42+
def initialize(player1, player2)
43+
@footprints = []
44+
@round = 1
45+
@player1 = player1
46+
@player2 = player2
47+
@players = [@player1, @player2]
48+
end
49+
50+
def play_game_1
51+
play_round while can_play?
52+
end
53+
54+
def play_round
55+
player1.debug
56+
player2.debug
57+
deals
58+
player1_win(player1_higthest?)
59+
@round += 1
60+
end
61+
62+
def play_game_2
63+
play_round_2 while can_play?
64+
return !player1.cards.empty?
65+
end
66+
67+
def play_round_2
68+
player1.debug
69+
player2.debug
70+
return if in_a_loop!
71+
deals
72+
if need_sub_game?
73+
game = Game.new(player1.sub_deck, player2.sub_deck)
74+
player1_win(game.play_game_2)
75+
else
76+
player1_win(player1_higthest?)
77+
end
78+
@round += 1
79+
end
80+
81+
def need_sub_game?
82+
player1.current <= player1.cards.count && player2.current <= player2.cards.count
83+
end
84+
85+
def player1_win(bool)
86+
if bool
87+
player1.win([player1.current, player2.current])
88+
else
89+
player2.win([player2.current, player1.current])
90+
end
91+
end
92+
93+
def in_a_loop!
94+
footprint = Digest::MD5.hexdigest "#{player1.cards.to_s} - #{player2.cards.to_s}"
95+
player2.cards.clear if @footprints.include? footprint
96+
@footprints << footprint
97+
return player2.cards.empty?
98+
end
99+
100+
def score
101+
game_winner.cards.reverse.map.with_index {|v, i| v * (i+1)}.sum
102+
end
103+
104+
def deals
105+
player1.deals
106+
player2.deals
107+
end
108+
109+
def player1_higthest?
110+
player1.current > player2.current
111+
end
112+
113+
def can_play?
114+
@players.all? {|d| !d.cards.empty? }
115+
end
116+
117+
def game_winner
118+
@players.find {|d| !d.cards.empty? }
119+
end
120+
121+
def self.file(name)
122+
File.read(name).split("\n\n").map { |data| Deck.file(data) }
123+
end
124+
end
125+
126+
class Day22Test < Test::Unit::TestCase
127+
def test_star_1
128+
game = Game.new('data_test.txt')
129+
game.play_game_1
130+
assert_equal 306, game.score
131+
end
132+
133+
def test_star_1_final
134+
game = Game.new('data.txt')
135+
assert_equal 33559, game.score
136+
end
137+
138+
def test_star_2_test
139+
game = Game.new(*Game.file('data_test.txt'))
140+
game.play_game_2
141+
assert_equal 291, game.score
142+
end
143+
144+
def test_star_2_final
145+
game = Game.new(*Game.file('data.txt'))
146+
game.play_game_2
147+
assert_equal 32789, game.score
148+
end
149+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /