Add day 9 solutions
This commit is contained in:
		
							parent
							
								
									2b88faf388
								
							
						
					
					
						commit
						874716448e
					
				
							
								
								
									
										39
									
								
								day9.chpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								day9.chpl
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,39 @@
 | 
				
			|||||||
 | 
					use IO, Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const moveList = [ "L" => (-1, 0), "R" => (1, 0), "U" => (0, 1), "D" => (0, -1) ];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					iter moves() {
 | 
				
			||||||
 | 
					  for line in stdin.lines().strip() {
 | 
				
			||||||
 | 
					    const (dir, _, n) = line.partition(" ");
 | 
				
			||||||
 | 
					    yield (moveList[dir], n : int);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					config const length = 2;
 | 
				
			||||||
 | 
					var rope: [0..<length] (int, int) = (0, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					proc move((hx, hy), (tx, ty)): (int, int) {
 | 
				
			||||||
 | 
					  const (dx, dy) = (hx - tx, hy - ty);
 | 
				
			||||||
 | 
					  if abs(dx) > 1 && dy == 0 {
 | 
				
			||||||
 | 
					    tx += sgn(dx);
 | 
				
			||||||
 | 
					  } else if abs(dy) > 1 && dx == 0 {
 | 
				
			||||||
 | 
					    ty += sgn(dy);
 | 
				
			||||||
 | 
					  } else if abs(dx) + abs(dy) > 2 {
 | 
				
			||||||
 | 
					    tx += sgn(dx);
 | 
				
			||||||
 | 
					    ty += sgn(dy);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return (tx, ty);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var visited = new set((int, int));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for (delta, n) in moves() {
 | 
				
			||||||
 | 
					  for 1..n {
 | 
				
			||||||
 | 
					    rope[0] += delta;
 | 
				
			||||||
 | 
					    for idx in 1..<length {
 | 
				
			||||||
 | 
					      rope[idx] = move(rope[idx-1], rope[idx]);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    visited.add(rope.last);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					writeln(visited.size);
 | 
				
			||||||
							
								
								
									
										56
									
								
								day9.cr
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								day9.cr
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,56 @@
 | 
				
			|||||||
 | 
					require "advent"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					INPUT = input(2022, 9).lines.map do |line|
 | 
				
			||||||
 | 
					  dir, n = line.split(" ");
 | 
				
			||||||
 | 
					  {dir, n.to_i64}
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					positions = Set(Tuple(Int32, Int32)).new
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					diff = { "L" => {-1, 0}, "R" => {1, 0}, "U" => {0, 1}, "D" => {0, -1} }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def move(h, d)
 | 
				
			||||||
 | 
					  {h[0] + d[0], h[1] + d[1]}
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def follow(h, t)
 | 
				
			||||||
 | 
					  hx, hy = h
 | 
				
			||||||
 | 
					  tx, ty = t
 | 
				
			||||||
 | 
					  dx = hx-tx
 | 
				
			||||||
 | 
					  dy = hy-ty
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if dx.abs > 1 && dy.abs == 0
 | 
				
			||||||
 | 
					    tx += dx.sign
 | 
				
			||||||
 | 
					  elsif dy.abs > 1 && dx.abs == 0
 | 
				
			||||||
 | 
					    ty += dy.sign
 | 
				
			||||||
 | 
					  elsif dx.abs + dy.abs > 2
 | 
				
			||||||
 | 
					    tx += dx.sign
 | 
				
			||||||
 | 
					    ty += dy.sign
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  h = {hx, hy}
 | 
				
			||||||
 | 
					  t = {tx, ty}
 | 
				
			||||||
 | 
					  return {h, t}
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def simulate(knots, d)
 | 
				
			||||||
 | 
					  knots[0] = move(knots[0], d)
 | 
				
			||||||
 | 
					  (knots.size-1).times do |i|
 | 
				
			||||||
 | 
					    knots[i], knots[i+1] = follow(knots[i], knots[i+1])
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					knots = [{0, 0}] * 10
 | 
				
			||||||
 | 
					INPUT.each do |cmd|
 | 
				
			||||||
 | 
					  dir, n = cmd
 | 
				
			||||||
 | 
					  d = diff[dir]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  n.times do
 | 
				
			||||||
 | 
					    simulate(knots, d)
 | 
				
			||||||
 | 
					    positions << knots.last
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					puts positions.size
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user